diff --git a/config/pkg.go b/config/pkg.go index 9f224d8..71dd467 100644 --- a/config/pkg.go +++ b/config/pkg.go @@ -33,6 +33,8 @@ type Config struct { Session struct { DaysValid int } + + Application any } func New(filename string) (config Config, err error) { diff --git a/pkg.go b/pkg.go index f61e658..ebac69d 100644 --- a/pkg.go +++ b/pkg.go @@ -1,6 +1,10 @@ /* The webservice package is used to provide a webservice with sessions: + const VERSION = "v1" + + var logger *slog.Logger + func sqlProvider(dbname string, version int) (sql []byte, found bool) { var err error sql, err = embeddedSQL.ReadFile(fmt.Sprintf("sql/%05d.sql", version)) @@ -11,21 +15,29 @@ The webservice package is used to provide a webservice with sessions: return } - service, err := webservice.New("/etc/some/webservice.yaml") - if err != nil { - logger.Error("application", "error", err) - os.Exit(1) + + func init() { + opts := slog.HandlerOptions{} + logger = slog.New(slog.NewJSONHandler(os.Stdout, &opts)) } - service.SetDatabase(sqlProvider) - service.SetAuthenticationHandler(authenticate) - service.SetAuthorizationHandler(authorize) - service.Register("/foo", true, true, foo) - service.Register("/bar", true, false, bar) - err = service.Start() - if err != nil { - logger.Error("webserver", "error", err) - os.Exit(1) + func main() { + service, err := webservice.New("/etc/some/webservice.yaml", VERSION, logger) + if err != nil { + logger.Error("application", "error", err) + os.Exit(1) + } + + service.SetDatabase(sqlProvider) + service.SetAuthenticationHandler(authenticate) + service.SetAuthorizationHandler(authorize) + service.Register("/foo", true, true, foo) + service.Register("/bar", true, false, bar) + err = service.Start() + if err != nil { + logger.Error("webserver", "error", err) + os.Exit(1) + } } */ package webservice @@ -52,7 +64,7 @@ import ( "strings" ) -const VERSION = "v0.2.11" +const VERSION = "v0.2.12" type HttpHandler func(http.ResponseWriter, *http.Request) @@ -66,7 +78,7 @@ type ServiceError struct { type Service struct { logger *slog.Logger sessions map[string]*session.T - config config.Config + Config config.Config Db *database.T Version string WsConnectionManager ws_conn_manager.ConnectionManager @@ -87,11 +99,11 @@ type ServiceHandler func(http.ResponseWriter, *http.Request, *session.T) func New(configFilename, version string, logger *slog.Logger) (service *Service, err error) { // {{{ service = new(Service) - service.config, err = config.New(configFilename) + service.Config, err = config.New(configFilename) if err != nil { return } - logger.Debug("config", "config", service.config) + logger.Debug("config", "config", service.Config) service.Version = version service.logger = logger @@ -99,7 +111,7 @@ func New(configFilename, version string, logger *slog.Logger) (service *Service, service.errorHandler = service.defaultErrorHandler service.authenticationHandler = service.defaultAuthenticationHandler service.authorizationHandler = service.defaultAuthorizationHandler - service.WsConnectionManager = ws_conn_manager.NewConnectionManager(service.logger, service.config.Websocket.Domains) + service.WsConnectionManager = ws_conn_manager.NewConnectionManager(service.logger, service.Config.Websocket.Domains) service.Register("/_session/new", false, false, service.sessionNew) service.Register("/_session/authenticate", true, false, service.sessionAuthenticate) @@ -160,7 +172,7 @@ func (service *Service) SetStaticDirectory(directory string, useDirectory bool) } // }}} func (service *Service) SetDatabase(sqlProv database.SqlProvider) { // {{{ - service.Db = database.New(service.config.Database) + service.Db = database.New(service.Config.Database) service.Db.SetLogger(service.logger) service.Db.SetSQLProvider(sqlProv) return @@ -267,7 +279,7 @@ func (service *Service) Start() (err error) { // {{{ go service.WsConnectionManager.BroadcastLoop() - listen := fmt.Sprintf("%s:%d", service.config.Network.Address, service.config.Network.Port) + listen := fmt.Sprintf("%s:%d", service.Config.Network.Address, service.Config.Network.Port) service.logger.Info("webserver", "listen", listen) err = http.ListenAndServe(listen, nil) return