-
Notifications
You must be signed in to change notification settings - Fork 21
/
json-server.go
34 lines (26 loc) · 793 Bytes
/
json-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
// "encoding/json"
"fmt"
"log"
"net/http"
)
const jsonServerPort = ":8080" // Port to serve JSON api on
func responder(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
enableCors(&w)
w.Write([]byte(jsonData))
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Not supported.")
}
}
func serveJSON() {
http.HandleFunc("/forza", responder)
log.Printf("JSON Telemetry Server started at http://localhost%s", jsonServerPort)
log.Fatal(http.ListenAndServe(jsonServerPort, nil))
}
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}