diff --git a/.gitignore b/.gitignore index f7865fc..06441d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -bin* -.env +bin/ +*.env* settings.go *.log *.sqlite diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index 1a4370c..a8ed15f 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -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", + } +}*/ diff --git a/server/config.go b/server/config.go index f2874d2..9cf41d6 100644 --- a/server/config.go +++ b/server/config.go @@ -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) diff --git a/server/server.go b/server/server.go index e3ff824..dcbeb52 100644 --- a/server/server.go +++ b/server/server.go @@ -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) diff --git a/storage/msgstore/msgstore.go b/storage/msgstore/msgstore.go index 0523723..ee0e6f6 100644 --- a/storage/msgstore/msgstore.go +++ b/storage/msgstore/msgstore.go @@ -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()) }