craban/server/server.go
2021-09-12 18:27:49 +02:00

66 lines
1.4 KiB
Go

package server
import (
"fmt"
"git.ctrlz.es/mgdelacroix/rmsn/model"
"git.ctrlz.es/mgdelacroix/rmsn/services/store"
"git.ctrlz.es/mgdelacroix/rmsn/web"
)
type Server struct {
Config *model.Config
Store *store.Store
WebServer *web.WebServer
}
func NewServer(config *model.Config) (*Server, error) {
if err := config.IsValid(); err != nil {
return nil, fmt.Errorf("config is not valid: %w", err)
}
webserver, err := web.NewWebServer(*config.Port)
if err != nil {
return nil, fmt.Errorf("cannot create webserver: %w", err)
}
store, err := store.NewStore(*config.DatabasePath)
if err != nil {
return nil, fmt.Errorf("cannot create store: %w", err)
}
return &Server{
Config: config,
WebServer: webserver,
Store: store,
}, err
}
func NewServerWithConfigPath(path string) (*Server, error) {
config, err := model.ReadConfig(path)
if err != nil {
return nil, fmt.Errorf("cannot read config from path %q: %w", path, err)
}
return NewServer(config)
}
func (s *Server) Start() error {
if err := s.WebServer.Start(); err != nil {
return fmt.Errorf("error starting webserver: %w", err)
}
return nil
}
func (s *Server) Close() error {
if err := s.WebServer.Close(); err != nil {
return fmt.Errorf("error stopping webserver: %w", err)
}
if err := s.Store.Close(); err != nil {
return fmt.Errorf("error stopping store: %w", err)
}
return nil
}