2024-01-27 14:02:10 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-01 08:26:27 -06:00
|
|
|
"context"
|
2024-02-02 12:57:18 -06:00
|
|
|
"log/slog"
|
2024-01-27 14:02:10 -06:00
|
|
|
"net/http"
|
2024-02-02 12:51:28 -06:00
|
|
|
"os"
|
2024-01-27 14:02:10 -06:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
2024-02-01 08:26:27 -06:00
|
|
|
"gitlab.com/zephyrtronium/sq"
|
2024-01-27 14:02:10 -06:00
|
|
|
|
|
|
|
"git.sunturtle.xyz/studio/shotgun/lobby"
|
2024-02-01 21:02:30 -06:00
|
|
|
"git.sunturtle.xyz/studio/shotgun/player"
|
2024-01-27 14:02:10 -06:00
|
|
|
"git.sunturtle.xyz/studio/shotgun/serve"
|
2024-02-01 08:26:27 -06:00
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
2024-01-27 14:02:10 -06:00
|
|
|
)
|
|
|
|
|
2024-02-02 12:51:28 -06:00
|
|
|
var (
|
|
|
|
addr = os.Getenv("SHOTGUN_HTTP")
|
|
|
|
public = os.Getenv("SHOTGUN_PUBLIC")
|
|
|
|
)
|
|
|
|
|
2024-01-27 14:02:10 -06:00
|
|
|
func main() {
|
2024-02-01 08:26:27 -06:00
|
|
|
sessiondb, err := sq.Open("sqlite", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
sessions, err := sessiondb.Conn(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-02-01 21:02:30 -06:00
|
|
|
if err := player.InitSessions(context.Background(), sessions); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := player.InitUsers(context.Background(), sessions); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s := Server{
|
|
|
|
l: lobby.New(),
|
|
|
|
creds: sessions,
|
|
|
|
sessions: sessions,
|
|
|
|
}
|
|
|
|
|
2024-01-27 14:02:10 -06:00
|
|
|
r := chi.NewRouter()
|
2024-02-01 21:02:30 -06:00
|
|
|
r.Post("/user/register", s.Register)
|
2024-02-01 20:40:59 -06:00
|
|
|
r.Post("/user/login", s.Login)
|
2024-02-01 21:38:44 -06:00
|
|
|
r.With(serve.WithSession(sessions)).Get("/user/me", s.Me)
|
|
|
|
r.With(serve.WithSession(sessions)).Get("/queue", s.Queue)
|
2024-02-02 12:57:18 -06:00
|
|
|
r.Method("GET", "/*", http.FileServer(http.Dir(public)))
|
|
|
|
slog.Info("listening", "addr", addr, "public", public)
|
2024-02-02 12:51:28 -06:00
|
|
|
http.ListenAndServe(addr, r)
|
2024-01-27 14:02:10 -06:00
|
|
|
}
|