diff --git a/pkg/server/server.go b/pkg/server/server.go index 0660d91..4363a75 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -14,14 +14,38 @@ import ( type Server struct { *bus.Bus *http.Server + rootFS fs.FS wg sync.WaitGroup cfg *config.Config } -func New(cfg *config.Config) (*Server, error) { +type StatusWriter struct { + http.ResponseWriter + status int +} + +func (w *StatusWriter) WriteHeader(status int) { + w.status = status + w.ResponseWriter.WriteHeader(status) +} + +func logHandler(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rw := &StatusWriter{ResponseWriter: w} + h.ServeHTTP(rw, r) + logRequest(rw.status, r) + }) +} + +func logRequest(status int, r *http.Request) { + log.Println(r.Method, status, r.URL.Path) +} + +func New(cfg *config.Config) (s *Server, err error) { + mux := http.NewServeMux() - s := &Server{ + s = &Server{ Bus: bus.New(), Server: &http.Server{ Addr: cfg.Server.Bind, @@ -30,13 +54,15 @@ func New(cfg *config.Config) (*Server, error) { cfg: cfg, } - rootFS, err := fs.Sub(frontend.Root, "frontend/hass_frontend") + s.rootFS, err = fs.Sub(frontend.Root, "frontend/hass_frontend") if err != nil { return nil, err } mux.HandleFunc("/api/websocket", s.wsHandler) - mux.Handle("/", http.FileServer(http.FS(rootFS))) + mux.Handle("/", logHandler(http.FileServer(http.FS(s.rootFS)))) + mux.HandleFunc("/auth/authorize", s.authorizeHandler) + mux.HandleFunc("/auth/providers", s.providersHandler) return s, nil } diff --git a/pkg/server/websocket.go b/pkg/server/websocket.go index be86d10..21cc0d6 100644 --- a/pkg/server/websocket.go +++ b/pkg/server/websocket.go @@ -1,6 +1,7 @@ package server import ( + "log" "net/http" "github.com/gorilla/websocket" @@ -12,6 +13,7 @@ var upgrader = websocket.Upgrader{ } func (s *Server) wsHandler(w http.ResponseWriter, req *http.Request) { + log.Println("WebSocket") //conn, err := upgrader.Upgrade(w, req, nil) panic("not implemented") }