-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
99 lines (86 loc) · 2.83 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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
// Contact struct (Model)
type Contact struct {
Name string `json:"name"`
Phone string `json:"phone"`
Email string `json:"email"`
}
// Init contacts var as a slice Contact struct
var contacts []Contact
// Get all contacts
func getContacts(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(contacts)
}
// Get single contact
func getContact(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r) // Gets params
// Looping through contacts and find one with the id from the params
for _, item := range contacts {
if item.Name == params["name"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Contact{})
}
// Add new contact
func createContact(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var contact Contact
_ = json.NewDecoder(r.Body).Decode(&contact)
contacts = append(contacts, contact)
json.NewEncoder(w).Encode(contact)
}
// Update contact
func updateContact(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for idx, item := range contacts {
if item.Name == params["name"] {
contacts = append(contacts[:idx], contacts[idx+1:]...)
var contact Contact
_ = json.NewDecoder(r.Body).Decode(&contact)
contact.Name = params["name"]
contacts = append(contacts, contact)
json.NewEncoder(w).Encode(contact)
return
}
}
}
// Delete contact
func deleteContact(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for idx, item := range contacts {
if item.Name == params["name"] {
contacts = append(contacts[:idx], contacts[idx+1:]...)
break
}
}
json.NewEncoder(w).Encode(contacts)
}
// Main function
func main() {
// Init router
r := mux.NewRouter()
// Hardcoded data - @todo: add database
contacts = append(contacts, Contact{Name: "Friend_1", Phone: "98xxx-xxxxx", Email: "[email protected]"})
contacts = append(contacts, Contact{Name: "Friend_2", Phone: "96xxx-xxxxx", Email: "[email protected]"})
contacts = append(contacts, Contact{Name: "Friend_3", Phone: "97xxx-xxxxx", Email: "[email protected]"})
// Route handles & endpoints
r.HandleFunc("/contacts", getContacts).Methods("GET")
r.HandleFunc("/contacts/{name}", getContact).Methods("GET")
r.HandleFunc("/contacts", createContact).Methods("POST")
r.HandleFunc("/contacts/{name}", updateContact).Methods("PUT")
r.HandleFunc("/contacts/{name}", deleteContact).Methods("DELETE")
// Start server
log.Fatal(http.ListenAndServe(":3000", r))
}