2021-11-19 18:37:42 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
|
2021-11-23 10:34:02 +00:00
|
|
|
"github.com/strimertul/strimertul/modules"
|
|
|
|
"github.com/strimertul/strimertul/modules/database"
|
2021-11-19 18:37:42 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-23 10:34:02 +00:00
|
|
|
kv "github.com/strimertul/kilovolt/v6"
|
2021-11-19 18:37:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-11-19 18:46:48 +00:00
|
|
|
Config ServerConfig
|
|
|
|
db *database.DB
|
|
|
|
logger logrus.FieldLogger
|
|
|
|
server *http.Server
|
|
|
|
frontend fs.FS
|
|
|
|
hub *kv.Hub
|
|
|
|
mux *http.ServeMux
|
2021-11-19 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:34:02 +00:00
|
|
|
func NewServer(manager *modules.Manager) (*Server, error) {
|
|
|
|
db, ok := manager.Modules["db"].(*database.DB)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("db module not found")
|
2021-11-19 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:34:02 +00:00
|
|
|
log := manager.Logger(modules.ModuleHTTP)
|
|
|
|
|
2021-11-19 18:37:42 +00:00
|
|
|
server := &Server{
|
|
|
|
logger: log,
|
|
|
|
db: db,
|
|
|
|
server: &http.Server{},
|
|
|
|
}
|
|
|
|
err := db.GetJSON(ServerConfigKey, &server.Config)
|
2021-11-21 21:36:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server.hub, err = kv.NewHub(db.Client(), kv.HubOptions{
|
|
|
|
Password: server.Config.KVPassword,
|
|
|
|
}, log.WithField("module", "kv"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go server.hub.Run()
|
2021-11-19 18:37:42 +00:00
|
|
|
|
2021-11-23 10:34:02 +00:00
|
|
|
// Register module
|
|
|
|
manager.Modules[modules.ModuleHTTP] = server
|
|
|
|
|
2021-11-21 21:36:48 +00:00
|
|
|
return server, nil
|
2021-11-19 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:34:02 +00:00
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.server.Close()
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:37:42 +00:00
|
|
|
func (s *Server) SetFrontend(files fs.FS) {
|
|
|
|
s.frontend = files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) makeMux() *http.ServeMux {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
if s.frontend != nil {
|
|
|
|
mux.Handle("/ui/", http.StripPrefix("/ui/", FileServerWithDefault(http.FS(s.frontend))))
|
|
|
|
}
|
|
|
|
if s.hub != nil {
|
|
|
|
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
kv.ServeWs(s.hub, w, r)
|
|
|
|
})
|
|
|
|
}
|
2021-11-19 18:46:48 +00:00
|
|
|
if s.Config.EnableStaticServer {
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(s.Config.Path))))
|
2021-11-19 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Listen() error {
|
|
|
|
// Start HTTP server
|
|
|
|
restart := newSafeBool(false)
|
|
|
|
exit := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := s.db.Subscribe(context.Background(), func(changed []database.ModifiedKV) error {
|
|
|
|
for _, pair := range changed {
|
|
|
|
if pair.Key == ServerConfigKey {
|
|
|
|
oldBind := s.Config.Bind
|
2021-11-21 21:36:48 +00:00
|
|
|
oldPassword := s.Config.KVPassword
|
2021-11-19 18:37:42 +00:00
|
|
|
err := s.db.GetJSON(ServerConfigKey, &s.Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.mux = s.makeMux()
|
2021-11-21 21:36:48 +00:00
|
|
|
// Restart hub if password changed
|
|
|
|
if oldPassword != s.Config.KVPassword {
|
|
|
|
s.hub.SetOptions(kv.HubOptions{
|
|
|
|
Password: s.Config.KVPassword,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Restart server if bind changed
|
2021-11-19 18:37:42 +00:00
|
|
|
if oldBind != s.Config.Bind {
|
|
|
|
restart.Set(true)
|
|
|
|
err = s.server.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to shutdown server")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, ServerConfigKey)
|
|
|
|
if err != nil {
|
|
|
|
exit <- fmt.Errorf("error while handling subscription to HTTP config changes: %w", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
s.logger.WithField("bind", s.Config.Bind).Info("Starting HTTP server")
|
|
|
|
s.mux = s.makeMux()
|
|
|
|
s.server = &http.Server{
|
|
|
|
Handler: s,
|
|
|
|
Addr: s.Config.Bind,
|
|
|
|
}
|
|
|
|
err := s.server.ListenAndServe()
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
exit <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Are we trying to close or restart?
|
|
|
|
s.logger.WithField("restart", restart).Debug("HTTP server stopped")
|
|
|
|
if restart.Get() {
|
|
|
|
restart.Set(false)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s.logger.Debug("HTTP server stalled")
|
|
|
|
exit <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
return <-exit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Redirect to /ui/ if root
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.Redirect(w, r, "/ui/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.mux.ServeHTTP(w, r)
|
|
|
|
}
|