-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestserver4g.go
More file actions
46 lines (41 loc) · 1.01 KB
/
testserver4g.go
File metadata and controls
46 lines (41 loc) · 1.01 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
package main
import (
"encoding/json"
"log"
"net/http"
)
func main() {
log.Print("Starting test server for G")
http.HandleFunc("/", index)
log.Fatal(http.ListenAndServe(":8080", nil))
}
type request struct {
Language string `json:"language"`
}
type response struct {
Ok bool `json:"ok"`
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoder := json.NewDecoder(r.Body)
var request request
if err := decoder.Decode(&request); err != nil {
http.Error(w, `{"error_message": "internal error"}`, http.StatusBadRequest)
return
}
log.Printf("host: %v, referer: %v", r.Host, r.Referer())
log.Printf("language: %v", request.Language)
log.Printf("headers:")
for i := range r.Header {
log.Printf("\t%v => %v", i, r.Header.Get(i))
}
payload, err := json.Marshal(response{
Ok: true,
})
if err != nil {
http.Error(w, `{"error_message": "internal error"}`, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(payload)
}