82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"forge.cadoles.com/arcad/arcast/pkg/browser"
|
|
"github.com/pkg/errors"
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
)
|
|
|
|
type Server struct {
|
|
browser browser.Browser
|
|
|
|
instanceID string
|
|
address string
|
|
port int
|
|
disableServiceDiscovery bool
|
|
|
|
webApps bool
|
|
defaultWebApp string
|
|
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
func (s *Server) Start() error {
|
|
serverCtx, cancelServer := context.WithCancel(context.Background())
|
|
|
|
s.cancel = cancelServer
|
|
s.ctx = serverCtx
|
|
|
|
httpServerCtx, cancelHTTPServer := context.WithCancel(serverCtx)
|
|
if err := s.startHTTPServer(httpServerCtx); err != nil {
|
|
cancelHTTPServer()
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if !s.disableServiceDiscovery {
|
|
mdnsServerCtx, cancelMDNSServer := context.WithCancel(serverCtx)
|
|
if err := s.startMDNServer(mdnsServerCtx); err != nil {
|
|
cancelHTTPServer()
|
|
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...)
|
|
|
|
return &Server{
|
|
browser: browser,
|
|
instanceID: opts.InstanceID,
|
|
address: opts.Address,
|
|
webApps: opts.WebApps,
|
|
defaultWebApp: opts.DefaultWebApp,
|
|
disableServiceDiscovery: opts.DisableServiceDiscovery,
|
|
}
|
|
}
|