Updated doc, exposed Config and bumped to v0.2.12
This commit is contained in:
parent
57c1cfb412
commit
9ce19b94b4
@ -33,6 +33,8 @@ type Config struct {
|
|||||||
Session struct {
|
Session struct {
|
||||||
DaysValid int
|
DaysValid int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Application any
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(filename string) (config Config, err error) {
|
func New(filename string) (config Config, err error) {
|
||||||
|
52
pkg.go
52
pkg.go
@ -1,6 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
The webservice package is used to provide a webservice with sessions:
|
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) {
|
func sqlProvider(dbname string, version int) (sql []byte, found bool) {
|
||||||
var err error
|
var err error
|
||||||
sql, err = embeddedSQL.ReadFile(fmt.Sprintf("sql/%05d.sql", version))
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
service, err := webservice.New("/etc/some/webservice.yaml")
|
|
||||||
if err != nil {
|
func init() {
|
||||||
logger.Error("application", "error", err)
|
opts := slog.HandlerOptions{}
|
||||||
os.Exit(1)
|
logger = slog.New(slog.NewJSONHandler(os.Stdout, &opts))
|
||||||
}
|
}
|
||||||
|
|
||||||
service.SetDatabase(sqlProvider)
|
func main() {
|
||||||
service.SetAuthenticationHandler(authenticate)
|
service, err := webservice.New("/etc/some/webservice.yaml", VERSION, logger)
|
||||||
service.SetAuthorizationHandler(authorize)
|
if err != nil {
|
||||||
service.Register("/foo", true, true, foo)
|
logger.Error("application", "error", err)
|
||||||
service.Register("/bar", true, false, bar)
|
os.Exit(1)
|
||||||
err = service.Start()
|
}
|
||||||
if err != nil {
|
|
||||||
logger.Error("webserver", "error", err)
|
service.SetDatabase(sqlProvider)
|
||||||
os.Exit(1)
|
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
|
package webservice
|
||||||
@ -52,7 +64,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "v0.2.11"
|
const VERSION = "v0.2.12"
|
||||||
|
|
||||||
type HttpHandler func(http.ResponseWriter, *http.Request)
|
type HttpHandler func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
@ -66,7 +78,7 @@ type ServiceError struct {
|
|||||||
type Service struct {
|
type Service struct {
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
sessions map[string]*session.T
|
sessions map[string]*session.T
|
||||||
config config.Config
|
Config config.Config
|
||||||
Db *database.T
|
Db *database.T
|
||||||
Version string
|
Version string
|
||||||
WsConnectionManager ws_conn_manager.ConnectionManager
|
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) { // {{{
|
func New(configFilename, version string, logger *slog.Logger) (service *Service, err error) { // {{{
|
||||||
service = new(Service)
|
service = new(Service)
|
||||||
|
|
||||||
service.config, err = config.New(configFilename)
|
service.Config, err = config.New(configFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Debug("config", "config", service.config)
|
logger.Debug("config", "config", service.Config)
|
||||||
|
|
||||||
service.Version = version
|
service.Version = version
|
||||||
service.logger = logger
|
service.logger = logger
|
||||||
@ -99,7 +111,7 @@ func New(configFilename, version string, logger *slog.Logger) (service *Service,
|
|||||||
service.errorHandler = service.defaultErrorHandler
|
service.errorHandler = service.defaultErrorHandler
|
||||||
service.authenticationHandler = service.defaultAuthenticationHandler
|
service.authenticationHandler = service.defaultAuthenticationHandler
|
||||||
service.authorizationHandler = service.defaultAuthorizationHandler
|
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/new", false, false, service.sessionNew)
|
||||||
service.Register("/_session/authenticate", true, false, service.sessionAuthenticate)
|
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) { // {{{
|
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.SetLogger(service.logger)
|
||||||
service.Db.SetSQLProvider(sqlProv)
|
service.Db.SetSQLProvider(sqlProv)
|
||||||
return
|
return
|
||||||
@ -267,7 +279,7 @@ func (service *Service) Start() (err error) { // {{{
|
|||||||
|
|
||||||
go service.WsConnectionManager.BroadcastLoop()
|
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)
|
service.logger.Info("webserver", "listen", listen)
|
||||||
err = http.ListenAndServe(listen, nil)
|
err = http.ListenAndServe(listen, nil)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user