Skip to content

Commit 08b0c4e

Browse files
lmanganiLorenzo Mangani
and
Lorenzo Mangani
authored
DEMO UI (#2)
* ui-test * ui-test * ui-test * ui-test * ui-test * ui-test * ui-test * ui-test * test * update ui * ui-test * Update server.go * Update queryClient.go * Update ui.html * self-provision url * Update Demo UI --------- Co-authored-by: Lorenzo Mangani <lmangani@Raspberry-M1.local> Co-authored-by: lmangani <>
1 parent e33737d commit 08b0c4e

File tree

2 files changed

+461
-13
lines changed

2 files changed

+461
-13
lines changed

server.go

+33-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package main
33

44
import (
5-
_ "embed"
5+
_ "embed"
66
"encoding/json"
77
"flag"
88
"fmt"
@@ -13,7 +13,6 @@ import (
1313
"time"
1414
)
1515

16-
1716
//go:embed ui.html
1817
var uiContent []byte
1918

@@ -51,8 +50,24 @@ type ErrorResponse struct {
5150
Error string `json:"error"`
5251
}
5352

53+
// addCORSHeaders adds CORS headers to the response
54+
func addCORSHeaders(w http.ResponseWriter) {
55+
w.Header().Set("Access-Control-Allow-Origin", "*")
56+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
57+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
58+
}
59+
5460
// handleQuery handles the /query endpoint
5561
func (s *Server) handleQuery(w http.ResponseWriter, r *http.Request) {
62+
// Add CORS headers
63+
addCORSHeaders(w)
64+
65+
// Handle preflight requests
66+
if r.Method == http.MethodOptions {
67+
w.WriteHeader(http.StatusOK)
68+
return
69+
}
70+
5671
// Only allow POST
5772
if r.Method != http.MethodPost {
5873
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
@@ -140,20 +155,22 @@ func sendErrorResponse(w http.ResponseWriter, message string, statusCode int) {
140155

141156
// Health check endpoint
142157
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
158+
// Add CORS headers
159+
addCORSHeaders(w)
160+
161+
// Handle preflight requests
162+
if r.Method == http.MethodOptions {
163+
w.WriteHeader(http.StatusOK)
164+
return
165+
}
166+
143167
w.Header().Set("Content-Type", "application/json")
144168
json.NewEncoder(w).Encode(map[string]interface{}{
145169
"status": "ok",
146170
"timestamp": time.Now().Format(time.RFC3339),
147171
})
148172
}
149173

150-
// addCORSHeaders adds CORS headers to the response
151-
func addCORSHeaders(w http.ResponseWriter) {
152-
w.Header().Set("Access-Control-Allow-Origin", "*")
153-
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
154-
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
155-
}
156-
157174
// handleUI serves the main UI page
158175
func (s *Server) handleUI(w http.ResponseWriter, r *http.Request) {
159176
// Add CORS headers
@@ -229,12 +246,15 @@ func main() {
229246
}
230247
defer server.Close()
231248

249+
// Create a new mux for routing
250+
mux := http.NewServeMux()
251+
232252
// Set up routes
233-
http.HandleFunc("/", server.handleUI)
234-
http.HandleFunc("/health", server.handleHealth)
235-
http.HandleFunc("/query", server.handleQuery)
253+
mux.HandleFunc("/", server.handleUI) // Serve UI at root path
254+
mux.HandleFunc("/health", server.handleHealth)
255+
mux.HandleFunc("/query", server.handleQuery)
236256

237257
// Start server
238258
log.Printf("GigAPI server running at http://localhost:%s", port)
239-
log.Fatal(http.ListenAndServe(":"+port, nil))
259+
log.Fatal(http.ListenAndServe(":"+port, mux))
240260
}

0 commit comments

Comments
 (0)