package server import ( "context" "crypto/tls" "io/fs" "forge.cadoles.com/arcad/arcast/pkg/browser" "github.com/gorilla/websocket" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" ) type Server struct { browser browser.Browser instanceID string address string port int tlsAddress string tlsPort int tlsCert *tls.Certificate serviceDiscoveryEnabled bool appsEnabled bool defaultApp string allowedOrigins []string apps []App ctx context.Context cancel context.CancelFunc upgrader websocket.Upgrader layeredFS fs.FS upperLayerDir string } func (s *Server) Start() error { ctx := context.Background() if err := s.initLayeredFS(); err != nil { return errors.WithStack(err) } serverCtx, cancelServer := context.WithCancel(ctx) s.cancel = cancelServer s.ctx = serverCtx webServersCtx, cancelWebServers := context.WithCancel(serverCtx) if err := s.startWebServers(webServersCtx); err != nil { cancelWebServers() return errors.WithStack(err) } if s.serviceDiscoveryEnabled { mdnsServerCtx, cancelMDNSServer := context.WithCancel(serverCtx) if err := s.startMDNServer(mdnsServerCtx); err != nil { cancelWebServers() cancelMDNSServer() return errors.WithStack(err) } } else { logger.Info(serverCtx, "service discovery disabled") } return nil } func (s *Server) Stop() error { if s.cancel != nil { s.cancel() } return nil } func (s *Server) Wait() error { <-s.ctx.Done() if err := s.ctx.Err(); err != nil { return errors.WithStack(err) } return nil } func New(browser browser.Browser, funcs ...OptionFunc) *Server { opts := NewOptions(funcs...) server := &Server{ browser: browser, instanceID: opts.InstanceID, address: opts.Address, tlsAddress: opts.TLSAddress, tlsCert: opts.TLSCertificate, appsEnabled: opts.EnableApps, allowedOrigins: opts.AllowedOrigins, defaultApp: opts.DefaultApp, apps: opts.Apps, serviceDiscoveryEnabled: opts.EnableServiceDiscovery, upperLayerDir: opts.UpperLayerDir, } server.upgrader = websocket.Upgrader{ CheckOrigin: server.checkOrigin, } return server }