From 2088a33b8787ae39a49fcca08ce7fed7381ba666 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Fri, 2 Feb 2024 18:29:07 -0600 Subject: [PATCH] configurable sqlite dsn --- main.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index afb491f..627f12a 100644 --- a/main.go +++ b/main.go @@ -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 { - panic(err) - } - if err := player.InitUsers(context.Background(), sessions); err != nil { - panic(err) + 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(), 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)