2022-04-11 02:32:02 +00:00
|
|
|
package main
|
|
|
|
|
2022-04-11 03:45:33 +00:00
|
|
|
import (
|
2022-04-12 20:33:13 +00:00
|
|
|
"database/sql"
|
2022-04-11 03:45:33 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-04-12 00:16:06 +00:00
|
|
|
"net/http"
|
2022-04-11 03:45:33 +00:00
|
|
|
"os"
|
2022-04-12 20:33:13 +00:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-04-11 03:45:33 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 20:33:13 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-04-11 03:45:33 +00:00
|
|
|
type iostreams struct {
|
|
|
|
Err io.Writer
|
|
|
|
Out io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
type Opts struct {
|
|
|
|
ConfigPath string
|
|
|
|
IO iostreams
|
2022-04-12 00:16:06 +00:00
|
|
|
Log func(string)
|
|
|
|
Logf func(string, ...interface{})
|
2022-04-12 20:33:13 +00:00
|
|
|
Config Config
|
|
|
|
DB *sql.DB
|
2022-04-11 03:45:33 +00:00
|
|
|
}
|
2022-04-11 02:32:02 +00:00
|
|
|
|
|
|
|
func main() {
|
2022-04-11 03:45:33 +00:00
|
|
|
var configFlag = flag.String("config", "config.yml", "A path to a config file.")
|
|
|
|
flag.Parse()
|
2022-04-12 00:16:06 +00:00
|
|
|
io := iostreams{
|
|
|
|
Err: os.Stderr,
|
|
|
|
Out: os.Stdout,
|
|
|
|
}
|
2022-04-12 20:33:13 +00:00
|
|
|
opts := &Opts{
|
2022-04-11 03:45:33 +00:00
|
|
|
ConfigPath: *configFlag,
|
2022-04-12 00:16:06 +00:00
|
|
|
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...)
|
2022-04-11 03:45:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := _main(opts)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 20:33:13 +00:00
|
|
|
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 {
|
2022-04-11 03:45:33 +00:00
|
|
|
cfg, err := parseConfig(opts.ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "could not read config file '%s'", opts.ConfigPath)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-04-12 20:33:13 +00:00
|
|
|
opts.Config = *cfg
|
|
|
|
|
|
|
|
teardown, err := setupDB(opts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not initialize DB: %w", err)
|
|
|
|
}
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
setupAPI(*opts)
|
2022-04-12 00:16:06 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2022-04-11 03:45:33 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-12 00:16:06 +00:00
|
|
|
|
2022-04-12 20:33:13 +00:00
|
|
|
func handler(opts Opts, f http.HandlerFunc) http.HandlerFunc {
|
2022-04-12 00:16:06 +00:00
|
|
|
// TODO make this more real
|
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
opts.Log(req.URL.Path)
|
|
|
|
f(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 20:33:13 +00:00
|
|
|
func setupAPI(opts Opts) {
|
|
|
|
http.HandleFunc("/instance", handler(opts, func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
io.WriteString(w, opts.Config.InstanceName)
|
2022-04-12 00:16:06 +00:00
|
|
|
}))
|
|
|
|
}
|