From 09bd315096a15c38b3ce6b518f754fe74aa9585a Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 12 Apr 2022 15:33:13 -0500 Subject: [PATCH] DB pool --- go.mod | 2 ++ go.sum | 2 ++ server/cmd/config.go | 14 +++++-------- server/cmd/main.go | 48 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 53a2b2e..b33d17d 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module git.tilde.town/tildetown/bbj2 go 1.18 require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b + +require github.com/mattn/go-sqlite3 v1.14.12 diff --git a/go.sum b/go.sum index e387ff0..4d5935b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= +github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/server/cmd/config.go b/server/cmd/config.go index c086628..8484039 100644 --- a/server/cmd/config.go +++ b/server/cmd/config.go @@ -11,17 +11,9 @@ const ( defaultPort = 7099 defaultInstanceName = "BBJ" defaultHost = "127.0.0.1" + defaultDBPath = "db.sqlite3" ) -type Config struct { - Admins []string - Port int - Host string - InstanceName string `yaml:"instance_name"` - AllowAnon bool `yaml:"allow_anon"` - Debug bool -} - func parseConfig(configPath string) (*Config, error) { cfgBytes, err := os.ReadFile(configPath) if err != nil { @@ -47,5 +39,9 @@ func parseConfig(configPath string) (*Config, error) { cfg.Host = defaultHost } + if cfg.DBPath == "" { + cfg.DBPath = defaultDBPath + } + return &cfg, nil } diff --git a/server/cmd/main.go b/server/cmd/main.go index c8d8a57..fe2053b 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -1,13 +1,26 @@ package main import ( + "database/sql" "flag" "fmt" "io" "net/http" "os" + + _ "github.com/mattn/go-sqlite3" ) +type Config struct { + Admins []string + Port int + Host string + InstanceName string `yaml:"instance_name"` + AllowAnon bool `yaml:"allow_anon"` + Debug bool + DBPath string `yaml:"db_path"` +} + type iostreams struct { Err io.Writer Out io.Writer @@ -18,6 +31,8 @@ type Opts struct { IO iostreams Log func(string) Logf func(string, ...interface{}) + Config Config + DB *sql.DB } func main() { @@ -27,7 +42,7 @@ func main() { Err: os.Stderr, Out: os.Stdout, } - opts := Opts{ + opts := &Opts{ ConfigPath: *configFlag, IO: io, // TODO use real logger @@ -45,14 +60,33 @@ func main() { } } -func _main(opts Opts) error { +type Teardown func() + +func setupDB(opts *Opts) (Teardown, error) { + db, err := sql.Open("sqlite3", opts.Config.DBPath) + fmt.Printf("DBG %#v\n", db) + + opts.DB = db + + return func() { db.Close() }, err +} + +func _main(opts *Opts) error { cfg, err := parseConfig(opts.ConfigPath) if err != nil { fmt.Fprintf(os.Stderr, "could not read config file '%s'", opts.ConfigPath) os.Exit(1) } - setupAPI(opts, *cfg) + opts.Config = *cfg + + teardown, err := setupDB(opts) + if err != nil { + return fmt.Errorf("could not initialize DB: %w", err) + } + defer teardown() + + setupAPI(*opts) // TODO TLS or SSL or something opts.Logf("starting server at %s:%d\n", cfg.Host, cfg.Port) @@ -63,7 +97,7 @@ func _main(opts Opts) error { return nil } -func handler(opts Opts, cfg Config, f http.HandlerFunc) http.HandlerFunc { +func handler(opts Opts, f http.HandlerFunc) http.HandlerFunc { // TODO make this more real return func(w http.ResponseWriter, req *http.Request) { opts.Log(req.URL.Path) @@ -71,8 +105,8 @@ func handler(opts Opts, cfg Config, f http.HandlerFunc) http.HandlerFunc { } } -func setupAPI(opts Opts, cfg Config) { - http.HandleFunc("/instance", handler(opts, cfg, func(w http.ResponseWriter, req *http.Request) { - io.WriteString(w, cfg.InstanceName) +func setupAPI(opts Opts) { + http.HandleFunc("/instance", handler(opts, func(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, opts.Config.InstanceName) })) }