Add beginning auth flow
This commit is contained in:
parent
a0a6bd1075
commit
cb347fa0a2
2 changed files with 32 additions and 4 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue