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