bds/routers/web.go
Matheus Sampaio Queiroga 84b1639649 add more template
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2025-01-28 00:20:20 -03:00

66 lines
1.9 KiB
Go

package routers
import (
"errors"
"net/http"
"sirherobrine23.com.br/go-bds/bds/routers/utils"
webTemplates "sirherobrine23.com.br/go-bds/bds/templates"
)
var WebRoute *http.ServeMux = http.NewServeMux()
func init() {
WebRoute.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/img/logo.ico", http.StatusMovedPermanently)
})
WebRoute.HandleFunc("GET /status", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
webTemplates.StatusTemplate(w, false, errors.New("example error"))
})
WebRoute.HandleFunc("POST /login", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Write([]byte("not implemented"))
})
WebRoute.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) {
info := webTemplates.LoadTemplate("users/auth/signin.tmpl")
if info == nil {
utils.JsonResponse(w, 500, map[string]string{"error": "not found"})
return
}
if err := info.Execute(w, map[string]any{"Title": "test"}); err != nil {
utils.JsonResponse(w, 500, map[string]string{"error": err.Error()})
return
}
})
WebRoute.HandleFunc("GET /server/{id}", func(w http.ResponseWriter, r *http.Request) {
info := webTemplates.LoadTemplate("server/server/control.tmpl")
if info == nil {
utils.JsonResponse(w, 500, map[string]string{"error": "not found"})
return
}
if err := info.Execute(w, map[string]any{"Title": "test"}); err != nil {
utils.JsonResponse(w, 500, map[string]string{"error": err.Error()})
return
}
})
WebRoute.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
info := webTemplates.LoadTemplate("public/home.tmpl")
if info == nil {
utils.JsonResponse(w, 500, map[string]string{"error": "not found"})
return
}
if err := info.Execute(w, map[string]any{"Title": "test"}); err != nil {
utils.JsonResponse(w, 500, map[string]string{"error": err.Error()})
return
}
})
}