Feat: styles and session
This commit is contained in:
10
handlers/actions.go
Normal file
10
handlers/actions.go
Normal file
@ -0,0 +1,10 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"golias/models"
|
||||
)
|
||||
|
||||
func createRoom(ctx context.Context, req *models.RoomReq) (*models.RoomPublic, error) {
|
||||
return nil, nil
|
||||
}
|
@ -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
|
||||
}
|
||||
|
15
handlers/elements.go
Normal file
15
handlers/elements.go
Normal file
@ -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)
|
||||
}
|
44
handlers/game.go
Normal file
44
handlers/game.go
Normal file
@ -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)
|
||||
}
|
@ -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{
|
||||
|
@ -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"
|
||||
|
Reference in New Issue
Block a user