Compare commits

...

2 Commits

Author SHA1 Message Date
vilmibm 09bd315096 DB pool 2022-04-12 15:33:13 -05:00
vilmibm 9b3ef7dc34 serve HTTP 2022-04-11 19:16:06 -05:00
4 changed files with 80 additions and 15 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,12 +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
@ -15,16 +29,28 @@ type iostreams struct {
type Opts struct {
ConfigPath string
IO iostreams
Log func(string)
Logf func(string, ...interface{})
Config Config
DB *sql.DB
}
func main() {
var configFlag = flag.String("config", "config.yml", "A path to a config file.")
flag.Parse()
opts := Opts{
io := iostreams{
Err: os.Stderr,
Out: os.Stdout,
}
opts := &Opts{
ConfigPath: *configFlag,
IO: iostreams{
Err: os.Stderr,
Out: os.Stdout,
IO: io,
// TODO use real logger
Log: func(s string) {
fmt.Fprintln(io.Out, s)
},
Logf: func(s string, args ...interface{}) {
fmt.Fprintf(io.Out, s, args...)
},
}
@ -34,14 +60,53 @@ 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)
}
fmt.Printf("DBG %#v\n", cfg.InstanceName)
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)
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), nil); err != nil {
return fmt.Errorf("http server exited with error: %w", err)
}
return nil
}
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)
f(w, req)
}
}
func setupAPI(opts Opts) {
http.HandleFunc("/instance", handler(opts, func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, opts.Config.InstanceName)
}))
}