Fix: pass username in ctx; more test words
This commit is contained in:
		| @@ -29,20 +29,17 @@ func HandleHideCreateForm(w http.ResponseWriter, r *http.Request) { | |||||||
| func HandleShowColor(w http.ResponseWriter, r *http.Request) { | func HandleShowColor(w http.ResponseWriter, r *http.Request) { | ||||||
| 	word := r.URL.Query().Get("word") | 	word := r.URL.Query().Get("word") | ||||||
| 	ctx := r.Context() | 	ctx := r.Context() | ||||||
|  | 	tmpl, err := template.ParseGlob("components/*.html") | ||||||
|  | 	if err != nil { | ||||||
|  | 		abortWithError(w, err.Error()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
| 	session, ok := ctx.Value(models.CtxSessionKey).(*models.Session) | 	session, ok := ctx.Value(models.CtxSessionKey).(*models.Session) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		// trying to get color without a session -> error | 		// trying to get color without a session -> error | ||||||
| 		abortWithError(w, "session not found") | 		http.Redirect(w, r, "/", 302) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	// // get room by room-id |  | ||||||
| 	// room, err := getRoomByID(session.CurrentRoom) |  | ||||||
| 	// if err != nil { |  | ||||||
| 	// 	log.Error("failed to get room", "error", err, "session", session) |  | ||||||
| 	// 	abortWithError(w, "failed to get room") |  | ||||||
| 	// 	return |  | ||||||
| 	// } |  | ||||||
| 	// log.Debug("got room", "room", room) |  | ||||||
| 	state, err := loadState(session.Username) | 	state, err := loadState(session.Username) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| @@ -56,11 +53,6 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, "word is not found") | 		abortWithError(w, "word is not found") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	tmpl, err := template.ParseGlob("components/*.html") |  | ||||||
| 	if err != nil { |  | ||||||
| 		abortWithError(w, err.Error()) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	cardword := models.WordCard{ | 	cardword := models.WordCard{ | ||||||
| 		Word:     word, | 		Word:     word, | ||||||
| 		Color:    models.StrToWordColor(color), | 		Color:    models.StrToWordColor(color), | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ package handlers | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"golias/config" | 	"golias/config" | ||||||
|  | 	"golias/models" | ||||||
| 	"golias/pkg/cache" | 	"golias/pkg/cache" | ||||||
| 	"html/template" | 	"html/template" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
| @@ -35,6 +36,21 @@ var roundWords = map[string]string{ | |||||||
| 	"rat":        "white", | 	"rat":        "white", | ||||||
| 	"perplexity": "blue", | 	"perplexity": "blue", | ||||||
| 	"notion":     "red", | 	"notion":     "red", | ||||||
|  | 	"guitar":     "blue", | ||||||
|  | 	"ocean":      "blue", | ||||||
|  | 	"moon":       "blue", | ||||||
|  | 	"coffee":     "blue", | ||||||
|  | 	"mountain":   "blue", | ||||||
|  | 	"book":       "blue", | ||||||
|  | 	"camera":     "blue", | ||||||
|  | 	"apple":      "red", | ||||||
|  | 	"fire":       "red", | ||||||
|  | 	"rose":       "red", | ||||||
|  | 	"sun":        "red", | ||||||
|  | 	"cherry":     "red", | ||||||
|  | 	"heart":      "red", | ||||||
|  | 	"tomato":     "red", | ||||||
|  | 	"cloud":      "white", | ||||||
| } | } | ||||||
|  |  | ||||||
| func HandlePing(w http.ResponseWriter, r *http.Request) { | func HandlePing(w http.ResponseWriter, r *http.Request) { | ||||||
| @@ -47,16 +63,13 @@ func HandleHome(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	// check if user in a room | 	userState, _ := getStateByCtx(r.Context()) | ||||||
| 	// roomID := getRoomIDFromCtx(r.Context()) | 	if userState == nil { | ||||||
| 	// roomID = "test-id" | 		userState = &models.UserState{} | ||||||
| 	// if roomID != "" { | 	} | ||||||
| 	// 	// get room data | 	// if err != nil { | ||||||
| 	// 	userState := models.MakeTestState() | 	// abortWithError(w, err.Error()) | ||||||
| 	// 	tmpl.ExecuteTemplate(w, "room", userState) |  | ||||||
| 	// return | 	// return | ||||||
| 	// } | 	// } | ||||||
| 	// userState := models.MakeTestState() |  | ||||||
| 	userState, _ := getStateByCtx(r.Context()) |  | ||||||
| 	tmpl.ExecuteTemplate(w, "base", userState) | 	tmpl.ExecuteTemplate(w, "base", userState) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -94,7 +94,7 @@ func GetSession(next http.Handler) http.Handler { | |||||||
| 		} | 		} | ||||||
| 		ctx := context.WithValue(r.Context(), | 		ctx := context.WithValue(r.Context(), | ||||||
| 			models.CtxUsernameKey, userSession.Username) | 			models.CtxUsernameKey, userSession.Username) | ||||||
| 		ctx = context.WithValue(r.Context(), | 		ctx = context.WithValue(ctx, | ||||||
| 			models.CtxSessionKey, userSession) | 			models.CtxSessionKey, userSession) | ||||||
| 		if err := cacheSetSession(sessionToken, | 		if err := cacheSetSession(sessionToken, | ||||||
| 			userSession); err != nil { | 			userSession); err != nil { | ||||||
|   | |||||||
| @@ -59,6 +59,21 @@ func MakeTestState() *UserState { | |||||||
| 		{Word: "rat", Color: "white"}, | 		{Word: "rat", Color: "white"}, | ||||||
| 		{Word: "perplexity", Color: "blue"}, | 		{Word: "perplexity", Color: "blue"}, | ||||||
| 		{Word: "notion", Color: "red"}, | 		{Word: "notion", Color: "red"}, | ||||||
|  | 		{Word: "guitar", Color: "blue"}, | ||||||
|  | 		{Word: "ocean", Color: "blue"}, | ||||||
|  | 		{Word: "moon", Color: "blue"}, | ||||||
|  | 		{Word: "coffee", Color: "blue"}, | ||||||
|  | 		{Word: "mountain", Color: "blue"}, | ||||||
|  | 		{Word: "book", Color: "blue"}, | ||||||
|  | 		{Word: "camera", Color: "blue"}, | ||||||
|  | 		{Word: "apple", Color: "red"}, | ||||||
|  | 		{Word: "fire", Color: "red"}, | ||||||
|  | 		{Word: "rose", Color: "red"}, | ||||||
|  | 		{Word: "sun", Color: "red"}, | ||||||
|  | 		{Word: "cherry", Color: "red"}, | ||||||
|  | 		{Word: "heart", Color: "red"}, | ||||||
|  | 		{Word: "tomato", Color: "red"}, | ||||||
|  | 		{Word: "cloud", Color: "white"}, | ||||||
| 	} | 	} | ||||||
| 	room := Room{ | 	room := Room{ | ||||||
| 		ID:          "test-id", | 		ID:          "test-id", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder