configurable sqlite dsn

This commit is contained in:
Branden J Brown 2024-02-02 18:29:07 -06:00
parent f7f3e67417
commit 2088a33b87
1 changed files with 15 additions and 12 deletions

21
main.go
View File

@ -20,35 +20,38 @@ import (
var ( var (
addr = os.Getenv("SHOTGUN_HTTP") addr = os.Getenv("SHOTGUN_HTTP")
public = os.Getenv("SHOTGUN_PUBLIC") public = os.Getenv("SHOTGUN_PUBLIC")
dsn = os.Getenv("SHOTGUN_DB")
) )
func main() { func main() {
sessiondb, err := sq.Open("sqlite", ":memory:") db, err := sq.Open("sqlite", dsn)
if err != nil { if err != nil {
panic(err) panic(err)
} }
sessions, err := sessiondb.Conn(context.Background()) if err := db.Ping(context.Background()); err != nil {
if err != nil {
panic(err) panic(err)
} }
if err := player.InitSessions(context.Background(), sessions); err != nil { if len(os.Args) > 1 && os.Args[1] == "init" {
if err := player.InitSessions(context.Background(), db); err != nil {
panic(err) panic(err)
} }
if err := player.InitUsers(context.Background(), sessions); err != nil { if err := player.InitUsers(context.Background(), db); err != nil {
panic(err) panic(err)
} }
return
}
s := Server{ s := Server{
l: lobby.New(), l: lobby.New(),
creds: sessions, creds: db,
sessions: sessions, sessions: db,
pp: map[player.ID]*websocket.Conn{}, pp: map[player.ID]*websocket.Conn{},
} }
r := chi.NewRouter() r := chi.NewRouter()
r.Post("/user/register", s.Register) r.Post("/user/register", s.Register)
r.Post("/user/login", s.Login) r.Post("/user/login", s.Login)
r.With(serve.WithSession(sessions)).Get("/user/me", s.Me) r.With(serve.WithSession(db)).Get("/user/me", s.Me)
r.With(serve.WithSession(sessions)).Get("/queue", s.Queue) r.With(serve.WithSession(db)).Get("/queue", s.Queue)
r.Method("GET", "/*", http.FileServer(http.Dir(public))) r.Method("GET", "/*", http.FileServer(http.Dir(public)))
slog.Info("listening", "addr", addr, "public", public) slog.Info("listening", "addr", addr, "public", public)
http.ListenAndServe(addr, r) http.ListenAndServe(addr, r)