-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiserver.go
69 lines (60 loc) · 1.6 KB
/
apiserver.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
)
type User struct {
UserName string
FirstName string
LastName string
Country string
}
var userData = map[string]User{
"john": User{"jdoe", "John", "Doe", "France"},
}
var port = flag.Int("port", 12346, "port to listen on, default is 12346")
func corsWrapper(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
fmt.Printf("Request Origin header %s\n", origin)
if origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
}
fn(w, r)
}
}
func userHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(userData[r.URL.Path[len("/users/"):]])
io.WriteString(w, string(b))
}
func createUserHandler(w http.ResponseWriter, r *http.Request) {
//post only
if r.Method != "POST" {
return
}
var nUser User
var e error
if e = json.NewDecoder(r.Body).Decode(&nUser); e == nil {
fmt.Printf("User created %v\n", nUser)
//save user
userData[nUser.UserName] = nUser
//write response
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(userData[nUser.UserName])
io.WriteString(w, string(b))
return
}
w.WriteHeader(http.StatusBadRequest)
fmt.Printf("Error in creating user %v", e.Error())
}
func main() {
flag.Parse()
http.HandleFunc("/users/", corsWrapper(userHandler))
http.HandleFunc("/users", corsWrapper(createUserHandler))
log.Fatal(http.ListenAndServe(fmt.Sprintf("apiserver.cors.com:%d", *port), nil))
}