Disabling unused endpoints for new React frontend
All checks were successful
Build Pogdark API / Build Pogdark API (push) Successful in 34s
All checks were successful
Build Pogdark API / Build Pogdark API (push) Successful in 34s
This commit is contained in:
parent
268629a7ac
commit
807bc66acb
162
main.go
162
main.go
@ -149,7 +149,7 @@ func (s *Server) handleConnections(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to marshal JSON due to %s", err)
|
log.Fatalf("Unable to marshal JSON due to %s", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Broadcasting ws message to %d clients", s.clients)
|
fmt.Printf("Broadcasting ws message %s to %d clients\n", message, len(s.clients))
|
||||||
s.broadcast <- message
|
s.broadcast <- message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,7 +200,7 @@ func broadcastExpiredRecords(s *Server, removed string) {
|
|||||||
log.Println("Error marshalling json:", err)
|
log.Println("Error marshalling json:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("Broadcasting expiration: %s to %s clients\n", string(msgJSON), len(s.clients))
|
fmt.Printf("Broadcasting expiration: %s to %d clients\n", string(msgJSON), len(s.clients))
|
||||||
for client := range s.clients {
|
for client := range s.clients {
|
||||||
if err := client.conn.WriteMessage(websocket.TextMessage, msgJSON); err != nil {
|
if err := client.conn.WriteMessage(websocket.TextMessage, msgJSON); err != nil {
|
||||||
log.Println("Failed to broadcast update:", err)
|
log.Println("Failed to broadcast update:", err)
|
||||||
@ -267,59 +267,60 @@ func fetchAllRecords() (map[string]string, error) {
|
|||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getState(w http.ResponseWriter, r *http.Request) {
|
/*
|
||||||
if r.Method != http.MethodPost {
|
func getState(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
|
if r.Method != http.MethodPost {
|
||||||
return
|
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
return
|
||||||
fmt.Println("Checking Status")
|
}
|
||||||
var request struct {
|
fmt.Println("Checking Status")
|
||||||
Id string `json:"Id"`
|
var request struct {
|
||||||
}
|
Id string `json:"Id"`
|
||||||
err := json.NewDecoder(r.Body).Decode(&request)
|
}
|
||||||
if err != nil {
|
err := json.NewDecoder(r.Body).Decode(&request)
|
||||||
fmt.Println("Invalid JSON format")
|
if err != nil {
|
||||||
fmt.Println(r.Body)
|
fmt.Println("Invalid JSON format")
|
||||||
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
|
fmt.Println(r.Body)
|
||||||
return
|
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
return
|
|
||||||
} else if err != nil {
|
|
||||||
log.Printf("Redis error: %v", err)
|
|
||||||
http.Error(w, "Error connecting to Redis", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
if request.Id == "" {
|
||||||
err = json.Unmarshal([]byte(value), &message)
|
fmt.Println("Missing or empty Id field")
|
||||||
if err != nil {
|
http.Error(w, "Missing or empty Id field", http.StatusBadRequest)
|
||||||
log.Printf("Failed to decode Redis data for Id: %s - %v", request.Id, err)
|
return
|
||||||
http.Error(w, "Failed to parse data", http.StatusInternalServerError)
|
}
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
var message Message
|
||||||
err = json.NewEncoder(w).Encode(message)
|
err = json.Unmarshal([]byte(value), &message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
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)
|
||||||
@ -362,38 +363,39 @@ func setState(w http.ResponseWriter, r *http.Request, s *Server) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateState(w http.ResponseWriter, r *http.Request) {
|
/*
|
||||||
if r.Method != http.MethodGet {
|
func updateState(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "Only GET method is allowed", http.StatusMethodNotAllowed)
|
if r.Method != http.MethodGet {
|
||||||
return
|
http.Error(w, "Only GET method is allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
return
|
||||||
allRecords, err := fetchAllRecords()
|
}
|
||||||
if err != nil {
|
allRecords, err := fetchAllRecords()
|
||||||
log.Printf("Error fetching records: %v", err)
|
if err != nil {
|
||||||
http.Error(w, "Failed to fetch records", http.StatusInternalServerError)
|
log.Printf("Error fetching records: %v", err)
|
||||||
return
|
http.Error(w, "Failed to fetch records", http.StatusInternalServerError)
|
||||||
}
|
|
||||||
|
|
||||||
var messages []Message
|
|
||||||
for _, record := range allRecords {
|
|
||||||
var message Message
|
|
||||||
err := json.Unmarshal([]byte(record), &message)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error unmarshalling record: %v", err)
|
|
||||||
http.Error(w, "Failed to parse records", http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
messages = append(messages, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
var messages []Message
|
||||||
err = json.NewEncoder(w).Encode(messages)
|
for _, record := range allRecords {
|
||||||
if err != nil {
|
var message Message
|
||||||
log.Printf("Error encoding response: %v", err)
|
err := json.Unmarshal([]byte(record), &message)
|
||||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
if err != nil {
|
||||||
}
|
log.Printf("Error unmarshalling record: %v", err)
|
||||||
}
|
http.Error(w, "Failed to parse records", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
messages = append(messages, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
err = json.NewEncoder(w).Encode(messages)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error encoding response: %v", err)
|
||||||
|
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
func enableCORS(next http.Handler) http.Handler {
|
func enableCORS(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow all origins
|
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow all origins
|
||||||
@ -418,11 +420,11 @@ 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")
|
||||||
router.HandleFunc("/update", updateState).Methods("GET")
|
//router.HandleFunc("/update", updateState).Methods("GET")
|
||||||
corsRouter := enableCORS(router)
|
corsRouter := enableCORS(router)
|
||||||
|
|
||||||
// Start server and other necessary goroutines
|
// Start server and other necessary goroutines
|
||||||
|
Loading…
Reference in New Issue
Block a user