r-spin endpoint with array

This commit is contained in:
Shel Le 2024-08-29 19:46:38 -07:00
parent f30fe3ef37
commit 446071eee7

View File

@ -1,11 +1,13 @@
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"github.com/redis/go-redis/v9"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
@ -28,27 +30,24 @@ func initRedis() {
}
// Handler for getting all messages
func getMessages(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
func getSpin(w http.ResponseWriter, r *http.Request) {
w.Header().Set(
"Content-Type",
"application/json",
)
// Use a SCAN operation instead of KEYS to avoid performance issues with large datasets
var messages []Message
iter := rdb.Scan(ctx, 0, "*", 0).Iterator()
for iter.Next(ctx) {
id := iter.Val()
content, err := rdb.Get(ctx, id).Result()
if err != nil {
http.Error(w, "Failed to get message content", http.StatusInternalServerError)
return
}
messages = append(messages, Message{ID: id, Content: content})
}
if err := iter.Err(); err != nil {
http.Error(w, "Failed to scan keys", http.StatusInternalServerError)
return
fmt.Println("roulette number")
n := 38
array := make([]int, n)
for i := range array {
array[i] = i
}
json.NewEncoder(w).Encode(messages)
// array = append(array, "00")
json.NewEncoder(w).Encode(array)
}
// Handler for getting a single message by ID
@ -94,7 +93,7 @@ func main() {
// Initialize Redis connection
initRedis()
http.HandleFunc("/messages", getMessages)
http.HandleFunc("/r-spin", getSpin)
http.HandleFunc("/message", getMessage)
http.HandleFunc("/add-message", addMessage)