summaryrefslogtreecommitdiff
path: root/pkg/web/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/web/server.go')
-rw-r--r--pkg/web/server.go35
1 files changed, 13 insertions, 22 deletions
diff --git a/pkg/web/server.go b/pkg/web/server.go
index cde347c..cccb4e1 100644
--- a/pkg/web/server.go
+++ b/pkg/web/server.go
@@ -3,35 +3,26 @@ package web
import (
"net/http"
- "github.com/xlgmokha/x/pkg/serde"
+ "gitlab.com/mokhax/sparkled/app/controllers/health"
+ "gitlab.com/mokhax/sparkled/app/controllers/sparkles"
"gitlab.com/mokhax/sparkled/pkg/db"
)
type Server struct {
- db db.Repository
- fileserver http.Handler
+ mux *http.ServeMux
}
-func NewServer(storage db.Repository) *Server {
- http.NewServeMux()
- return &Server{
- db: storage,
- fileserver: http.FileServer(http.Dir("public")),
- }
+func New(storage db.Repository) *Server {
+ mux := http.NewServeMux()
+ c := sparkles.New(storage)
+ mux.HandleFunc("GET /sparkles", c.Index)
+ mux.HandleFunc("POST /sparkles", c.Create)
+ mux.HandleFunc("GET /health", health.New().Index)
+ mux.Handle("GET /", http.FileServer(http.Dir("public")))
+
+ return &Server{mux: mux}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case "GET":
- switch r.URL.String() {
- case "/health":
- w.WriteHeader(http.StatusOK)
- case "/sparkles":
- serde.ToHTTP(w, r, s.db.All())
- }
- break
- default:
- s.fileserver.ServeHTTP(w, r)
- break
- }
+ s.mux.ServeHTTP(w, r)
}