Compare commits

..

No commits in common. "09bd315096a15c38b3ce6b518f754fe74aa9585a" and "a87c941bb31fe3fb9cd2b10eddeabd7be487126f" have entirely different histories.

4 changed files with 15 additions and 80 deletions

2
go.mod
View File

@ -3,5 +3,3 @@ 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,5 +1,3 @@
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,9 +11,17 @@ 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 {
@ -39,9 +47,5 @@ func parseConfig(configPath string) (*Config, error) {
cfg.Host = defaultHost
}
if cfg.DBPath == "" {
cfg.DBPath = defaultDBPath
}
return &cfg, nil
}

View File

@ -1,26 +1,12 @@
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
@ -29,28 +15,16 @@ 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()
io := iostreams{
Err: os.Stderr,
Out: os.Stdout,
}
opts := &Opts{
opts := Opts{
ConfigPath: *configFlag,
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...)
IO: iostreams{
Err: os.Stderr,
Out: os.Stdout,
},
}
@ -60,53 +34,14 @@ func main() {
}
}
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 {
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)
}
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)
}
fmt.Printf("DBG %#v\n", cfg.InstanceName)
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)
}))
}