work in progress: storage (SQL database) service with msgstore functionality

This commit is contained in:
Helmut Merz 2023-11-19 12:53:56 +01:00
parent 520244fb19
commit 257167f0f6
7 changed files with 30 additions and 12 deletions

View file

@ -4,8 +4,10 @@ import (
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core/action"
"git.sr.ht/~cco/go-scopes/logging"
"git.sr.ht/~cco/go-scopes/server"
"git.sr.ht/~cco/go-scopes/storage/msgstore"
"git.sr.ht/~cco/go-scopes/storage/sql"
)
@ -26,21 +28,20 @@ func Config() lib.Config {
(&server.Cfg{Port: ovr("8123", SERVER_PORT)}).
AddRoute("/docs", server.FileServer(ovr("html", DOCROOT))).
AddRoute("/api", server.MsgHandler().
AddActionProc("demo", server.Async, "test-receiver"))))
AddActionProc("scopes|data", server.Async, "msgstore"))))
app_c.Add(server_c)
msgstore_c := b("msgstore", sql.Start(
&sql.Cfg{
Driver: "postgres",
Connstr: ovr("user=cco password=dummy dbname=scps", PGSQL_CONNSTR),
}))
msgstore_c.AddAction("scopes|data", action.Base(msgstore.Store))
app_c.Add(server_c, msgstore_c)
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 (

View file

@ -19,6 +19,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

View file

@ -36,6 +36,8 @@ github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZX
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=

View file

@ -4,6 +4,7 @@ import (
"demo/etc"
"git.sr.ht/~cco/go-scopes/app"
_ "git.sr.ht/~cco/go-scopes/storage/sql/pgsql"
)
func main() {

View file

@ -5,8 +5,14 @@ import (
"git.sr.ht/~cco/go-scopes/storage/sql"
)
func Store(act lib.Action) bool {
db := lib.GetState[*sql.Storage](act.Context())
StoreDB(db, act.Message())
return true
}
// var Store = func(db *sql.Storage, msg lib.Message) {
func Store(db *sql.Storage, msg lib.Message) {
func StoreDB(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())
}

View file

@ -7,6 +7,7 @@ import (
"strings"
"text/template"
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/logging/log"
)
@ -21,6 +22,12 @@ type Storage struct {
Errors []error
}
func Start(cfg *Cfg) lib.Proc {
return func(ctx lib.Context) {
ctx.WithState(Open(cfg))
}
}
type Rows = sql.Rows
type rowsProc = func(*sql.Rows) error

View file

@ -62,7 +62,7 @@ func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
func MsgstoreTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
msg := message.SimpleMessage("data")
msgstore.Store(db, msg)
msgstore.StoreDB(db, msg)
}
func resetSqlite(db *sql.Storage) {