-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
171 lines (153 loc) · 3.93 KB
/
handlers.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// homepage for the api
func welcome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome")
}
func getTodos(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: getTodos")
enableCors(&w)
w.Header().Set("Content-Type", "application/json")
responseJson, err := json.Marshal(todos)
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusOK)
w.Write(responseJson)
}
type UpdateTodoRequest struct {
ID int `json:"id"`
Title string `json:"title"`
}
func updateTodo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: updateTodo")
setupResponse(&w, r)
switch r.Method {
case "OPTIONS":
return
case "POST":
var todoToUpdate UpdateTodoRequest
decoder := json.NewDecoder(r.Body)
decoder.Decode(&todoToUpdate)
databaseUpdateTodo(todoToUpdate.ID, todoToUpdate.Title)
fetchTodos()
responseJson, err := json.Marshal(todos)
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusCreated)
w.Write(responseJson)
default:
responseJson, err := json.Marshal(map[string]string{"message": "Please use POST method"})
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusBadRequest)
w.Write(responseJson)
}
}
type NewTodoRequest struct {
Title string `json:"title"`
}
func addTodo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: addTodo")
setupResponse(&w, r)
switch r.Method {
case "OPTIONS":
return
case "POST":
var newTodo NewTodoRequest
decoder := json.NewDecoder(r.Body)
decoder.Decode(&newTodo)
insertTodo(newTodo.Title, false)
fetchTodos()
responseJson, err := json.Marshal(todos)
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusCreated)
w.Write(responseJson)
default:
responseJson, err := json.Marshal(map[string]string{"message": "Please use POST method"})
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusBadRequest)
w.Write(responseJson)
}
}
type DeleteTodoRequest struct {
ID int `json:"id"`
}
func deleteTodo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: deleteTodo")
setupResponse(&w, r)
switch r.Method {
case "OPTIONS":
return
case "POST":
var todoToDelete DeleteTodoRequest
decoder := json.NewDecoder(r.Body)
decoder.Decode(&todoToDelete)
databaseDeleteTodo(todoToDelete.ID)
fetchTodos()
responseJson, err := json.Marshal(todos)
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusOK)
w.Write(responseJson)
default:
responseJson, err := json.Marshal(map[string]string{"message": "Please use POST method"})
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusBadRequest)
w.Write(responseJson)
}
}
func deleteTodos(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: deleteTodos")
setupResponse(&w, r)
switch r.Method {
case "OPTIONS":
return
case "DELETE":
emptyTodos()
responseJson, err := json.Marshal(todos)
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusCreated)
w.Write(responseJson)
default:
responseJson, err := json.Marshal(map[string]string{"message": "Please use DELETE method"})
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusBadRequest)
w.Write(responseJson)
}
}
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func handleRequests() {
http.HandleFunc("/", welcome)
http.HandleFunc("/todos", getTodos)
http.HandleFunc("/update", updateTodo)
http.HandleFunc("/new", addTodo)
http.HandleFunc("/delete", deleteTodo)
http.HandleFunc("/delete-all", deleteTodos)
log.Fatal(http.ListenAndServe(":8000", nil))
}