Add beginning auth flow

This commit is contained in:
Daniel Ponte 2022-09-25 21:24:35 -04:00
parent a0a6bd1075
commit cb347fa0a2
2 changed files with 32 additions and 4 deletions

View file

@ -14,14 +14,38 @@ import (
type Server struct { type Server struct {
*bus.Bus *bus.Bus
*http.Server *http.Server
rootFS fs.FS
wg sync.WaitGroup wg sync.WaitGroup
cfg *config.Config 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() mux := http.NewServeMux()
s := &Server{ s = &Server{
Bus: bus.New(), Bus: bus.New(),
Server: &http.Server{ Server: &http.Server{
Addr: cfg.Server.Bind, Addr: cfg.Server.Bind,
@ -30,13 +54,15 @@ func New(cfg *config.Config) (*Server, error) {
cfg: cfg, cfg: cfg,
} }
rootFS, err := fs.Sub(frontend.Root, "frontend/hass_frontend") s.rootFS, err = fs.Sub(frontend.Root, "frontend/hass_frontend")
if err != nil { if err != nil {
return nil, err return nil, err
} }
mux.HandleFunc("/api/websocket", s.wsHandler) 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 return s, nil
} }

View file

@ -1,6 +1,7 @@
package server package server
import ( import (
"log"
"net/http" "net/http"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -12,6 +13,7 @@ var upgrader = websocket.Upgrader{
} }
func (s *Server) wsHandler(w http.ResponseWriter, req *http.Request) { func (s *Server) wsHandler(w http.ResponseWriter, req *http.Request) {
log.Println("WebSocket")
//conn, err := upgrader.Upgrade(w, req, nil) //conn, err := upgrader.Upgrade(w, req, nil)
panic("not implemented") panic("not implemented")
} }