trunk
vilmibm 2022-04-12 15:33:13 -05:00
parent 9b3ef7dc34
commit 09bd315096
4 changed files with 50 additions and 16 deletions

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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)
}))
}