-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (40 loc) · 951 Bytes
/
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
package main
import (
"flag"
"io"
"net/http"
)
var (
port string
)
func main() {
flag.StringVar(&port, "port", "30000", "待ち受けポート番号")
flag.Parse()
http.HandleFunc("/", endpoint)
http.ListenAndServe(":"+port, nil)
}
func endpoint(w http.ResponseWriter, r *http.Request) {
key := r.URL.Query().Get("key")
switch r.Method {
case "GET":
val := get(key)
if val == "" {
http.Error(w, "Key Not Found : "+key, http.StatusNotFound)
}
io.WriteString(w, val)
case "POST", "PUT":
if err := r.ParseForm(); err != nil {
http.Error(w, "Sever Error : Request ParseError", http.StatusBadRequest)
}
key := r.FormValue("key")
value := r.FormValue("value")
if key == "" || value == "" {
http.Error(w, "Sever Error : key "+key+" value "+value, http.StatusBadRequest)
}
postOrPut(key, value)
case "DELETE":
del(key)
default:
http.Error(w, "Doesn't support protocol.", http.StatusNotAcceptable)
}
}