-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (113 loc) · 3.23 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"embedded-nextjs-poc/memstore"
"embedded-nextjs-poc/models"
//"embedded-nextjs-poc/models"
"encoding/json"
"log"
"net/http"
)
func main() {
// Create a new request multiplexer
// Take incoming requests and dispatch them to the matching handlers
mux := http.NewServeMux()
h := UserHandler{}
h.store = *memstore.NewUserMemStore()
h.store.Add(models.User{
Name: "clay",
Email: "clay@ih",
})
h.store.Add(models.User{
Name: "Sheila",
Email: "sheila@ih",
})
h.store.Add(models.User{
Name: "Marcelo",
Email: "marcelo@ih",
})
log.Println("Configuring...")
mux.HandleFunc("GET /", h.Home)
mux.HandleFunc("GET /api/user/{id}", h.GetUser)
mux.HandleFunc("GET /api/user", h.GetUsers)
mux.HandleFunc("POST /api/user", h.AddUser)
mux.HandleFunc("PUT /api/user/{id}", h.SaveUser)
mux.HandleFunc("DELETE /api/user/{id}", h.DeleteUser)
// Run the server
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
log.Println("Listening...")
server.ListenAndServe() // Run the http server
}
func (h *UserHandler) errorResponse(w http.ResponseWriter, statusCode int, errorString string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
encodingError := json.NewEncoder(w).Encode(UserError{
StatusCode: statusCode,
Error: errorString,
})
if encodingError != nil {
http.Error(w, encodingError.Error(), http.StatusInternalServerError)
}
}
type UserError struct {
StatusCode int `json:"status_code"`
Error string `json:"error"`
}
func (h *UserHandler) Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("This is my home page"))
}
func (h *UserHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
users, err := h.store.List()
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(users)
if err != nil {
h.errorResponse(w, http.StatusInternalServerError, err.Error())
return
}
}
func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
user, err := h.store.Get(id)
if err != nil {
h.errorResponse(w, http.StatusNotFound, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(user)
}
func (h *UserHandler) AddUser(w http.ResponseWriter, r *http.Request) {
var user models.User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
h.errorResponse(w, http.StatusBadRequest, err.Error())
}
newUser := h.store.Add(user)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(newUser)
}
func (h *UserHandler) SaveUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id := r.PathValue("id")
var user models.User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
h.errorResponse(w, http.StatusBadRequest, err.Error())
}
user, err = h.store.Update(id, user)
if err != nil {
h.errorResponse(w, http.StatusBadRequest, err.Error())
}
err = json.NewEncoder(w).Encode(user)
}
func (h *UserHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
err := h.store.Remove(id)
if err != nil {
h.errorResponse(w, http.StatusNotFound, err.Error())
}
}
type UserHandler struct {
store memstore.UserMemStore
}