-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorillamux1.go
100 lines (100 loc) · 3 KB
/
gorillamux1.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type User struct {
ID string `json:"id"`
Lastname string `json:"lastname"`
Firstname string `json:"firstname"`
Age int `json:"age"`
Email string `json:"email"`
}
var users = []User{}
func main() {
r := mux.NewRouter()
usersR := r.PathPrefix("/users").Subrouter()
usersR.Path("").Methods(http.MethodGet).HandlerFunc(getAllUsers)
usersR.Path("").Methods(http.MethodPost).HandlerFunc(createUser)
usersR.Path("/{id}").Methods(http.MethodGet).HandlerFunc(getUserByID)
usersR.Path("/{id}").Methods(http.MethodPut).HandlerFunc(uptdateUser)
usersR.Path("/{id}").Methods(http.MethodDelete).HandlerFunc(deleteUser)
fmt.Println("Start listening")
fmt.Println(http.ListenAndServe(":8080", r))
}
func getAllUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(users); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func getUserByID(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
index := indexByID(users, id)
if index < 0 {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(users[index]); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func uptdateUser(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
index := indexByID(users, id)
if index < 0 {
w.WriteHeader(http.StatusNotFound)
return
}
u := User{}
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
users[index] = u
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(&u); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func deleteUser(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
index := indexByID(users, id)
if index < 0 {
w.WriteHeader(http.StatusNotFound)
return
}
users = append(users[:index], users[index+1:]...)
w.WriteHeader(http.StatusOK)
}
func createUser(w http.ResponseWriter, r *http.Request) {
u := User{}
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
users = append(users, u)
response, err := json.Marshal(&u)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(response)
}
func indexByID(users []User, id string) int {
for i := 0; i < len(users); i++ {
if users[i].ID == id {
return i
}
}
return -1
}