Enable the getState function
All checks were successful
Build Pogdark API / Build Pogdark API (pull_request) Successful in 16s

This commit is contained in:
whysman 2025-02-24 19:21:49 -05:00
parent f7ddf11f94
commit 0eee8e006d

107
main.go
View File

@ -234,60 +234,59 @@ func fetchAllRecords() (map[string]string, error) {
return records, nil return records, nil
} }
/* func getState(w http.ResponseWriter, r *http.Request) {
func getState(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost {
if r.Method != http.MethodPost { http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed) return
return
}
fmt.Println("Checking Status")
var request struct {
Id string `json:"Id"`
}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
fmt.Println("Invalid JSON format")
fmt.Println(r.Body)
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
return
}
if request.Id == "" {
fmt.Println("Missing or empty Id field")
http.Error(w, "Missing or empty Id field", http.StatusBadRequest)
return
}
fmt.Println(request.Id)
value, err := redisClient.Get(ctx, request.Id).Result()
if errors.Is(err, redis.Nil) {
message := Message{Id: request.Id, Status: "none", Timestamp: time.Now().Format(time.RFC3339)}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(message)
if err != nil {
return
}
return
} else if err != nil {
log.Printf("Redis error: %v", err)
http.Error(w, "Error connecting to Redis", http.StatusInternalServerError)
return
}
var message Message
err = json.Unmarshal([]byte(value), &message)
if err != nil {
log.Printf("Failed to decode Redis data for Id: %s - %v", request.Id, err)
http.Error(w, "Failed to parse data", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(message)
if err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
} }
*/ fmt.Println("Checking Status")
var request struct {
Id string `json:"Id"`
}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
fmt.Println("Invalid JSON format")
fmt.Println(r.Body)
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
return
}
fmt.Println(request)
if request.Id == "" {
fmt.Println("Missing or empty Id field")
http.Error(w, "Missing or empty Id field", http.StatusBadRequest)
return
}
fmt.Println(request.Id)
value, err := redisClient.Get(ctx, request.Id).Result()
if errors.Is(err, redis.Nil) {
message := Message{Id: request.Id, Status: "none", Timestamp: time.Now().Format(time.RFC3339)}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(message)
if err != nil {
return
}
return
} else if err != nil {
log.Printf("Redis error: %v", err)
http.Error(w, "Error connecting to Redis", http.StatusInternalServerError)
return
}
var message Message
err = json.Unmarshal([]byte(value), &message)
if err != nil {
log.Printf("Failed to decode Redis data for Id: %s - %v", request.Id, err)
http.Error(w, "Failed to parse data", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(message)
if err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
}
func setState(w http.ResponseWriter, r *http.Request, s *Server) { func setState(w http.ResponseWriter, r *http.Request, s *Server) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed) http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
@ -385,7 +384,7 @@ func main() {
// Register routes on the mux router // Register routes on the mux router
router.HandleFunc("/ws", server.handleConnections).Methods("GET") router.HandleFunc("/ws", server.handleConnections).Methods("GET")
//router.HandleFunc("/get", getState).Methods("POST") router.HandleFunc("/get", getState).Methods("POST")
router.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { router.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
setState(w, r, server) setState(w, r, server)
}).Methods("POST") }).Methods("POST")