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 go 1.18
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=

View File

@ -11,9 +11,17 @@ const (
defaultPort = 7099 defaultPort = 7099
defaultInstanceName = "BBJ" defaultInstanceName = "BBJ"
defaultHost = "127.0.0.1" 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) { func parseConfig(configPath string) (*Config, error) {
cfgBytes, err := os.ReadFile(configPath) cfgBytes, err := os.ReadFile(configPath)
if err != nil { if err != nil {
@ -39,9 +47,5 @@ func parseConfig(configPath string) (*Config, error) {
cfg.Host = defaultHost cfg.Host = defaultHost
} }
if cfg.DBPath == "" {
cfg.DBPath = defaultDBPath
}
return &cfg, nil return &cfg, nil
} }

View File

@ -1,26 +1,12 @@
package main package main
import ( import (
"database/sql"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "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 { type iostreams struct {
Err io.Writer Err io.Writer
Out io.Writer Out io.Writer
@ -29,28 +15,16 @@ type iostreams struct {
type Opts struct { type Opts struct {
ConfigPath string ConfigPath string
IO iostreams IO iostreams
Log func(string)
Logf func(string, ...interface{})
Config Config
DB *sql.DB
} }
func main() { func main() {
var configFlag = flag.String("config", "config.yml", "A path to a config file.") var configFlag = flag.String("config", "config.yml", "A path to a config file.")
flag.Parse() flag.Parse()
io := iostreams{ opts := Opts{
Err: os.Stderr,
Out: os.Stdout,
}
opts := &Opts{
ConfigPath: *configFlag, ConfigPath: *configFlag,
IO: io, IO: iostreams{
// TODO use real logger Err: os.Stderr,
Log: func(s string) { Out: os.Stdout,
fmt.Fprintln(io.Out, s)
},
Logf: func(s string, args ...interface{}) {
fmt.Fprintf(io.Out, s, args...)
}, },
} }
@ -60,53 +34,14 @@ func main() {
} }
} }
type Teardown func() func _main(opts Opts) error {
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) cfg, err := parseConfig(opts.ConfigPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not read config file '%s'", opts.ConfigPath) fmt.Fprintf(os.Stderr, "could not read config file '%s'", opts.ConfigPath)
os.Exit(1) os.Exit(1)
} }
opts.Config = *cfg fmt.Printf("DBG %#v\n", cfg.InstanceName)
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 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)
}))
}