-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_server.go
More file actions
56 lines (47 loc) · 1.4 KB
/
Copy pathhttp_server.go
File metadata and controls
56 lines (47 loc) · 1.4 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
func StartHttp(port int) {
mux := http.NewServeMux()
mux.HandleFunc("/api/game_servers/available", func(w http.ResponseWriter, r *http.Request) {
servers := []map[string]interface{}{
{
"game_server": map[string]interface{}{
// You'll probably want to change the IP to localhost. In my case, I'm using a Windows VM to test
// this, so I need to bind the IP for the VMware network to use my host
"uri": "zp://127.0.0.1:3301/",
},
},
}
data, _ := json.Marshal(servers)
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(data); err != nil {
log.Fatal(err)
}
})
mux.HandleFunc("/clientInstrumentation/report", func(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "failed to read body", http.StatusBadRequest)
return
}
defer r.Body.Close()
body := string(bodyBytes)
log.Println(body)
// Write response
response := map[string]interface{}{"success": true}
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Println("failed to write response:", err)
}
})
fmt.Printf("✓ HTTP Server started on 127.0.0.1:%d\n", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), mux)
}