blasphem/pkg/server/server.go
2022-09-25 14:07:47 -04:00

58 lines
852 B
Go

package server
import (
"io/fs"
"log"
"net/http"
"sync"
"dynatron.me/x/blasphem/pkg/bus"
"dynatron.me/x/blasphem/pkg/config"
"dynatron.me/x/blasphem/pkg/frontend"
)
type Server struct {
*bus.Bus
*http.Server
wg sync.WaitGroup
cfg *config.Config
}
func New(cfg *config.Config) (*Server, error) {
mux := http.NewServeMux()
s := &Server{
Bus: bus.New(),
Server: &http.Server{
Addr: cfg.Server.Bind,
Handler: mux,
},
cfg: cfg,
}
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)))
return s, nil
}
func (s *Server) Go() error {
s.wg.Add(1)
go func() {
err := s.ListenAndServe()
if err != nil {
log.Fatal(err)
}
s.wg.Done()
}()
s.wg.Wait()
return nil
}