Updated doc, exposed Config and bumped to v0.2.12

This commit is contained in:
Magnus Åhall 2024-04-27 11:54:33 +02:00
parent 57c1cfb412
commit 9ce19b94b4
2 changed files with 34 additions and 20 deletions

View File

@ -33,6 +33,8 @@ type Config struct {
Session struct {
DaysValid int
}
Application any
}
func New(filename string) (config Config, err error) {

28
pkg.go
View File

@ -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,7 +15,14 @@ The webservice package is used to provide a webservice with sessions:
return
}
service, err := webservice.New("/etc/some/webservice.yaml")
func init() {
opts := slog.HandlerOptions{}
logger = slog.New(slog.NewJSONHandler(os.Stdout, &opts))
}
func main() {
service, err := webservice.New("/etc/some/webservice.yaml", VERSION, logger)
if err != nil {
logger.Error("application", "error", err)
os.Exit(1)
@ -27,6 +38,7 @@ The webservice package is used to provide a webservice with sessions:
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