diff --git a/components/createroomform.html b/components/createroomform.html
new file mode 100644
index 0000000..77d2200
--- /dev/null
+++ b/components/createroomform.html
@@ -0,0 +1,24 @@
+{{define "createform"}}
+
{{template "login"}}
+
Word Color Cards
{{range $word, $color := .}}
diff --git a/handlers/actions.go b/handlers/actions.go
new file mode 100644
index 0000000..bd4706d
--- /dev/null
+++ b/handlers/actions.go
@@ -0,0 +1,10 @@
+package handlers
+
+import (
+ "context"
+ "golias/models"
+)
+
+func createRoom(ctx context.Context, req *models.RoomReq) (*models.RoomPublic, error) {
+ return nil, nil
+}
diff --git a/handlers/auth.go b/handlers/auth.go
index 62d9c2e..3274e68 100644
--- a/handlers/auth.go
+++ b/handlers/auth.go
@@ -1,10 +1,12 @@
package handlers
import (
+ "context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
+ "errors"
"golias/models"
"golias/utils"
"html/template"
@@ -112,3 +114,12 @@ func cacheSetSession(key string, session *models.Session) error {
memcache.Expire(key, 10*60)
return nil
}
+
+func updateRoomInSession(ctx context.Context, roomID string) (context.Context, error) {
+ s, ok := ctx.Value("session").(models.Session)
+ if !ok {
+ return context.TODO(), errors.New("failed to extract session from ctx")
+ }
+ s.CurrentRoom = roomID
+ return context.WithValue(ctx, "session", s), nil
+}
diff --git a/handlers/elements.go b/handlers/elements.go
new file mode 100644
index 0000000..0ed89eb
--- /dev/null
+++ b/handlers/elements.go
@@ -0,0 +1,15 @@
+package handlers
+
+import (
+ "html/template"
+ "net/http"
+)
+
+func HandleShowCreateForm(w http.ResponseWriter, r *http.Request) {
+ tmpl, err := template.ParseGlob("components/*.html")
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ tmpl.ExecuteTemplate(w, "createform", nil)
+}
diff --git a/handlers/game.go b/handlers/game.go
new file mode 100644
index 0000000..b84f725
--- /dev/null
+++ b/handlers/game.go
@@ -0,0 +1,44 @@
+package handlers
+
+import (
+ "context"
+ "golias/models"
+ "html/template"
+ "net/http"
+)
+
+func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
+ // parse payload
+ payload := &models.RoomReq{
+ RoomPass: r.PostFormValue("room_pass"),
+ RoomName: r.PostFormValue("room_name"),
+ }
+ // create a room
+ room, err := createRoom(r.Context(), payload)
+ if err != nil {
+ msg := "failed to create a room"
+ log.Error(msg, "error", err)
+ abortWithError(w, msg)
+ return
+ }
+ ctx := context.WithValue(r.Context(), "current_room", room.ID)
+ ctx, err = updateRoomInSession(ctx, room.ID)
+ if err != nil {
+ msg := "failed to set current room to session"
+ log.Error(msg, "error", err)
+ abortWithError(w, msg)
+ return
+ }
+ // send msg of created room
+ // h.Broker.Notifier <- broker.NotificationEvent{
+ // EventName: models.MsgRoomListUpdate,
+ // Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
+ // }
+ // return html
+ tmpl, err := template.ParseGlob("components/*.html")
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ tmpl.ExecuteTemplate(w, "main", nil)
+}
diff --git a/handlers/handlers.go b/handlers/handlers.go
index 2fb0550..b6053d7 100644
--- a/handlers/handlers.go
+++ b/handlers/handlers.go
@@ -1,19 +1,27 @@
package handlers
import (
+ "golias/config"
+ "golias/pkg/cache"
"html/template"
"log/slog"
"net/http"
"os"
)
-var log *slog.Logger
+var (
+ log *slog.Logger
+ cfg *config.Config
+ memcache cache.Cache
+)
func init() {
log = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
}))
+ memcache = cache.MemCache
+ cfg = config.LoadConfigOrDefault("")
}
var roundWords = map[string]string{
diff --git a/handlers/middleware.go b/handlers/middleware.go
index 11b8df5..c5ca97e 100644
--- a/handlers/middleware.go
+++ b/handlers/middleware.go
@@ -6,17 +6,10 @@ import (
"crypto/sha256"
"encoding/base64"
"errors"
- "golias/config"
- "golias/pkg/cache"
"net/http"
"time"
)
-var (
- cfg config.Config
- memcache cache.Cache
-)
-
// responseWriterWrapper wraps http.ResponseWriter to capture status code
type responseWriterWrapper struct {
http.ResponseWriter
@@ -99,6 +92,8 @@ func GetSession(next http.Handler) http.Handler {
}
ctx := context.WithValue(r.Context(),
"username", userSession.Username)
+ ctx = context.WithValue(r.Context(),
+ "session", userSession)
if err := cacheSetSession(sessionToken,
userSession); err != nil {
msg := "failed to marshal user session"
diff --git a/main.go b/main.go
index 5f104e0..50e31eb 100644
--- a/main.go
+++ b/main.go
@@ -1,8 +1,8 @@
package main
import (
- "fmt"
"golias/handlers"
+ "log/slog"
"net/http"
"time"
)
@@ -22,13 +22,14 @@ func ListenToRequests(port string) error {
mux.HandleFunc("GET /ping", handlers.HandlePing)
mux.HandleFunc("GET /", handlers.HandleHome)
- fmt.Println("Listening", "addr", port)
+ mux.HandleFunc("POST /login", handlers.HandleFrontLogin)
+ mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm)
+ slog.Info("Listening", "addr", port)
return server.ListenAndServe()
}
func main() {
port := ":3000"
- fmt.Printf("Starting server on %s\n", port)
err := ListenToRequests(port)
if err != nil {
panic(err)
diff --git a/models/main.go b/models/main.go
index 81b4105..2d97863 100644
--- a/models/main.go
+++ b/models/main.go
@@ -67,3 +67,12 @@ type GameSettings struct {
ProgressPct uint32 `json:"progress_pct"`
IsOver bool
}
+
+// =====
+
+type RoomReq struct {
+ // is not user or not unique
+ RoomPass string `json:"room_pass" form:"room_pass"`
+ RoomName string `json:"room_name" form:"room_name"`
+ // GameSettings
+}