-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
110 lines (102 loc) · 3.36 KB
/
main.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright 2023 Upbound Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"log"
"net/http"
"time"
)
// This is a very simple server that stores and retrieves robots to a map that
// will be lost when the server is restarted.
type Robot struct {
Name string `json:"name"`
Color string `json:"color"`
}
var robots = make(map[string]*Robot)
func main() { //nolint:gocyclo
http.HandleFunc("/robots", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
name := r.URL.Query().Get("name")
robot, ok := robots[name]
if !ok {
http.Error(w, "Robot not found", http.StatusNotFound)
return
}
if err := json.NewEncoder(w).Encode(robot); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case "POST":
robot := new(Robot)
if err := json.NewDecoder(r.Body).Decode(robot); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if _, ok := robots[robot.Name]; ok {
http.Error(w, "Robot already exists", http.StatusBadRequest)
return
}
robots[robot.Name] = robot
w.WriteHeader(http.StatusCreated)
case "PUT":
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
robot := new(Robot)
err := json.NewDecoder(r.Body).Decode(robot)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if _, ok := robots[name]; !ok {
http.Error(w, "Robot not found", http.StatusNotFound)
return
}
delete(robots, name)
robots[robot.Name] = robot
w.WriteHeader(http.StatusOK)
case "DELETE":
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
if _, ok := robots[name]; !ok {
http.Error(w, "Robot not found", http.StatusNotFound)
return
}
delete(robots, name)
w.WriteHeader(http.StatusOK)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
})
log.Println("Listening on :9090")
log.Printf("Example commands:\n")
log.Printf("Create:\n\tcurl -XPOST -H \"Content-type: application/json\" -d '{\"name\": \"myrobot\", \"color\": \"green\"}' 'http://127.0.0.1:9090/robots'\n")
log.Printf("Get:\n\tcurl 'http://127.0.0.1:9090/robots?&name=myrobot'\n")
log.Printf("Update:\n\tcurl -XPUT -H \"Content-type: application/json\" -d '{\"name\": \"myrobot\", \"color\": \"yellow\"}' 'http://127.0.0.1:9090/robots?&name=myrobot'\n")
log.Printf("Delete:\n\tcurl -XDELETE 'http://127.0.0.1:9090/robots?&name=myrobot'\n")
s := &http.Server{
Addr: ":9090",
Handler: http.TimeoutHandler(http.DefaultServeMux, 10*time.Second, "timeout"),
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Fatal(s.ListenAndServe())
}