2023-07-23 08:49:57 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2023-07-24 20:56:10 +00:00
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/bootoption"
|
2023-07-23 08:54:49 +00:00
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/config"
|
2023-07-24 20:56:10 +00:00
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/controllers/client"
|
2023-08-16 21:33:26 +00:00
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/controllers/ui"
|
2023-07-24 20:56:10 +00:00
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/middlewares"
|
2023-07-24 21:05:50 +00:00
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/services"
|
2023-07-23 08:49:57 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-07-24 20:56:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
httpSrv *http.Server
|
|
|
|
listener net.Listener
|
|
|
|
serverMode config.ListeningMode
|
|
|
|
address string
|
|
|
|
handler *http.ServeMux
|
|
|
|
l *logrus.Logger
|
|
|
|
clients map[string]bootoption.Client
|
|
|
|
controllers map[string]http.Handler
|
2023-07-23 08:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newUnixListener(sockPath string) (net.Listener, error) {
|
|
|
|
if err := os.Remove(sockPath); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, fmt.Errorf("failed to cleanup previously existing socket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sock, err := net.Listen("unix", sockPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create unix socket: %w", err)
|
|
|
|
}
|
|
|
|
if err := os.Chmod(sockPath, 0o777); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to set permissions to unix socket: %w", err)
|
|
|
|
}
|
|
|
|
return sock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(appConf *config.AppConfig, logger *logrus.Logger) (*Server, error) {
|
|
|
|
var listener net.Listener
|
|
|
|
var addr string
|
|
|
|
var err error
|
|
|
|
switch appConf.ServerMode {
|
|
|
|
case config.ModeNet:
|
|
|
|
addr = fmt.Sprintf("%s:%d", appConf.Host, appConf.Port)
|
|
|
|
listener, err = net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init server in net mode: %w", err)
|
|
|
|
}
|
|
|
|
case config.ModeUnix:
|
|
|
|
addr = appConf.SockPath
|
|
|
|
listener, err = newUnixListener(appConf.SockPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init server in unix mode: %w", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected listening mode %v", appConf.ServerMode))
|
|
|
|
}
|
2023-07-30 09:20:45 +00:00
|
|
|
service := services.NewClientHandlerService(appConf.DataFilepath, logger)
|
2023-07-24 20:56:10 +00:00
|
|
|
controllers := map[string]http.Handler{
|
2023-09-16 11:52:05 +00:00
|
|
|
client.EnrollRoute: middlewares.WithLogger(client.NewEnrollController(logger, service, appConf.UDPPort, appConf.UPDMcastGroup), logger),
|
2023-08-19 11:49:13 +00:00
|
|
|
client.ConfigRoute: middlewares.WithLogger(client.NewGetConfigController(logger, service, appConf), logger),
|
2024-04-30 16:43:01 +00:00
|
|
|
client.SetBootRoute: middlewares.WithLogger(client.NewBootController(logger, service, appConf), logger),
|
2024-03-24 12:37:40 +00:00
|
|
|
ui.StaticRoute: middlewares.WithLogger(ui.NewStaticController(appConf.StaticDir), logger),
|
|
|
|
ui.UIRoute: middlewares.WithLogger(ui.NewUIController(logger, service, appConf.StaticDir), logger),
|
2023-07-24 20:56:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-23 08:49:57 +00:00
|
|
|
m := http.NewServeMux()
|
|
|
|
|
|
|
|
return &Server{
|
|
|
|
handler: m,
|
|
|
|
httpSrv: &http.Server{
|
|
|
|
Handler: m,
|
|
|
|
},
|
2023-07-24 20:56:10 +00:00
|
|
|
listener: listener,
|
|
|
|
l: logger,
|
|
|
|
serverMode: appConf.ServerMode,
|
|
|
|
address: addr,
|
|
|
|
clients: make(map[string]bootoption.Client),
|
|
|
|
controllers: controllers,
|
2024-03-24 12:21:56 +00:00
|
|
|
ctx: context.TODO(),
|
2023-07-23 08:49:57 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initMux() {
|
2023-07-24 20:56:10 +00:00
|
|
|
for r, c := range s.controllers {
|
|
|
|
s.handler.Handle(r, c)
|
|
|
|
}
|
2023-07-23 08:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run(ctx context.Context) {
|
|
|
|
s.ctx, s.cancel = context.WithCancel(ctx)
|
|
|
|
s.initMux()
|
|
|
|
switch s.serverMode {
|
|
|
|
case config.ModeNet:
|
|
|
|
s.l.Infof("Server listening on host %q", s.address)
|
|
|
|
case config.ModeUnix:
|
|
|
|
s.l.Infof("Server listening on unix socket %q", s.address)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err := s.httpSrv.Serve(s.listener); err != nil {
|
|
|
|
s.l.Errorf("failed to serve HTTP server: %s", err.Error())
|
|
|
|
}
|
|
|
|
s.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Done() <-chan struct{} {
|
|
|
|
return s.ctx.Done()
|
|
|
|
}
|