add register endpoint
This commit is contained in:
parent
6aa6996f66
commit
bf2d629708
17
main.go
17
main.go
@ -8,15 +8,13 @@ import (
|
||||
"gitlab.com/zephyrtronium/sq"
|
||||
|
||||
"git.sunturtle.xyz/studio/shotgun/lobby"
|
||||
"git.sunturtle.xyz/studio/shotgun/player"
|
||||
"git.sunturtle.xyz/studio/shotgun/serve"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := Server{
|
||||
l: lobby.New(),
|
||||
}
|
||||
sessiondb, err := sq.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -25,7 +23,20 @@ func main() {
|
||||
if 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)
|
||||
}
|
||||
s := Server{
|
||||
l: lobby.New(),
|
||||
creds: sessions,
|
||||
sessions: sessions,
|
||||
}
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Post("/user/register", s.Register)
|
||||
r.Post("/user/login", s.Login)
|
||||
r.With(serve.WithPlayerID(sessions)).Get("/queue", s.Queue)
|
||||
http.ListenAndServe(":8080", r)
|
||||
|
39
server.go
39
server.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
@ -21,13 +20,18 @@ import (
|
||||
type Server struct {
|
||||
l *lobby.Lobby
|
||||
|
||||
creds player.RowQuerier
|
||||
creds credsDB
|
||||
sessions player.Execer
|
||||
|
||||
mu sync.Mutex
|
||||
pp map[player.ID]*websocket.Conn
|
||||
}
|
||||
|
||||
type credsDB interface {
|
||||
player.RowQuerier
|
||||
player.Execer
|
||||
}
|
||||
|
||||
type person struct {
|
||||
conn *websocket.Conn
|
||||
id player.ID
|
||||
@ -66,6 +70,34 @@ func (s *Server) left(p player.ID) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Register(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
slog := slog.With(
|
||||
slog.String("remote", r.RemoteAddr),
|
||||
slog.String("forwarded-for", r.Header.Get("X-Forwarded-For")),
|
||||
slog.String("user-agent", r.UserAgent()),
|
||||
)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
slog.WarnContext(ctx, "error parsing form on register", "err", err.Error())
|
||||
http.Error(w, "what", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user, pass := r.PostFormValue("user"), r.PostFormValue("pass")
|
||||
if user == "" || pass == "" {
|
||||
slog.WarnContext(ctx, "missing user or pass on register")
|
||||
http.Error(w, "missing credentials", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err := player.Register(ctx, s.creds, user, pass)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "registration failed", "err", err.Error())
|
||||
http.Error(w, "something went wrong, maybe someone already has that username, idk", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
slog.InfoContext(ctx, "registered", "user", user)
|
||||
}
|
||||
|
||||
func (s *Server) Login(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
slog := slog.With(
|
||||
@ -97,7 +129,8 @@ func (s *Server) Login(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
serve.SetSession(w, id)
|
||||
io.WriteString(w, id.String())
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
slog.InfoContext(ctx, "logged in", "player", p, "id", id)
|
||||
}
|
||||
|
||||
// Queue connects players to games. The connection immediately upgrades.
|
||||
|
Loading…
Reference in New Issue
Block a user