2
2
package main
3
3
4
4
import (
5
- _ "embed"
5
+ _ "embed"
6
6
"encoding/json"
7
7
"flag"
8
8
"fmt"
@@ -13,7 +13,6 @@ import (
13
13
"time"
14
14
)
15
15
16
-
17
16
//go:embed ui.html
18
17
var uiContent []byte
19
18
@@ -51,8 +50,24 @@ type ErrorResponse struct {
51
50
Error string `json:"error"`
52
51
}
53
52
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
+
54
60
// handleQuery handles the /query endpoint
55
61
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
+
56
71
// Only allow POST
57
72
if r .Method != http .MethodPost {
58
73
http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
@@ -140,20 +155,22 @@ func sendErrorResponse(w http.ResponseWriter, message string, statusCode int) {
140
155
141
156
// Health check endpoint
142
157
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
+
143
167
w .Header ().Set ("Content-Type" , "application/json" )
144
168
json .NewEncoder (w ).Encode (map [string ]interface {}{
145
169
"status" : "ok" ,
146
170
"timestamp" : time .Now ().Format (time .RFC3339 ),
147
171
})
148
172
}
149
173
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
-
157
174
// handleUI serves the main UI page
158
175
func (s * Server ) handleUI (w http.ResponseWriter , r * http.Request ) {
159
176
// Add CORS headers
@@ -229,12 +246,15 @@ func main() {
229
246
}
230
247
defer server .Close ()
231
248
249
+ // Create a new mux for routing
250
+ mux := http .NewServeMux ()
251
+
232
252
// 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 )
236
256
237
257
// Start server
238
258
log .Printf ("GigAPI server running at http://localhost:%s" , port )
239
- log .Fatal (http .ListenAndServe (":" + port , nil ))
259
+ log .Fatal (http .ListenAndServe (":" + port , mux ))
240
260
}
0 commit comments