allow callable (returning data) in request action handler

This commit is contained in:
Helmut Merz 2023-11-19 09:45:43 +01:00
parent ce4d67d33a
commit 520244fb19
5 changed files with 42 additions and 10 deletions

4
.gitignore vendored
View file

@ -1,5 +1,5 @@
bin*
.env
bin/
*.env*
settings.go
*.log
*.sqlite

View file

@ -6,6 +6,7 @@ import (
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/logging"
"git.sr.ht/~cco/go-scopes/server"
"git.sr.ht/~cco/go-scopes/storage/sql"
)
func Config() lib.Config {
@ -32,12 +33,29 @@ func Config() lib.Config {
return app_c
}
func ConfigDB() *sql.Cfg {
ovr := Overrides().Use
return &sql.Cfg{
Driver: "postgres",
Connstr: ovr("user=cco password=dummy dbname=scps", PGSQL_CONNSTR),
}
}
// collect here the names of fields that may be overridden via
// explicit Override() or SCOPES_* environment settings.
const (
HOME = "home"
LOGFILE = "logfile"
LOGLEVEL = "loglevel"
SERVER_PORT = "server_port"
DOCROOT = "docroot"
HOME = "home"
LOGFILE = "logfile"
LOGLEVEL = "loglevel"
SERVER_PORT = "server_port"
DOCROOT = "docroot"
PGSQL_CONNSTR = "pgsql_connstr"
)
// put something like this (without comment markup)
// in separate (non-versioned) settings.go file:
/*func Overrides() config.Settings {
return config.Settings{
SERVER_PORT: "8126",
}
}*/

View file

@ -64,5 +64,3 @@ func (spec *mhSpec) AddActionProc(
func MsgHandler() *mhSpec {
return &mhSpec{config.Default("")}
}
type msgProc func(lib.Context, lib.Message) (int, lib.Data)

View file

@ -89,6 +89,17 @@ func Async(ctx lib.Context, msg lib.Message) (int, lib.Data) {
return http.StatusOK, lib.Map{"status": "OK"}
}
func Call(clbl callable) msgProc {
return func(ctx lib.Context, msg lib.Message) (int, lib.Data) {
data, err := clbl(ctx, msg)
if err != nil {
data = lib.Map{"error": err.Error()}
return http.StatusInternalServerError, data
}
return http.StatusOK, data
}
}
func Sync(timeout int) msgProc {
return func(ctx lib.Context, msg lib.Message) (int, lib.Data) {
return sync(ctx, msg, timeout)
@ -107,3 +118,7 @@ func sync(ctx lib.Context, msg lib.Message, to int) (int, lib.Data) {
}
return http.StatusNoContent, nil
}
// message processing function types
type msgProc func(lib.Context, lib.Message) (int, lib.Data)
type callable func(lib.Context, lib.Message) (lib.Data, error)

View file

@ -5,7 +5,8 @@ import (
"git.sr.ht/~cco/go-scopes/storage/sql"
)
var Store = func(db *sql.Storage, msg lib.Message) {
// var Store = func(db *sql.Storage, msg lib.Message) {
func Store(db *sql.Storage, msg lib.Message) {
q := db.BuildQuery("insert_msg", "messages")
db.Exec(q, msg.Domain(), msg.Action(), msg.Class(), msg.Item(), msg.Payload())
}