-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (89 loc) · 2.64 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
// Building a REST ful API with Go
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
)
// Client is model for client data
type Client struct {
ID string `json:"id"`
Name string `json:"name"`
JobTitle string `json:"job_title"`
BusinessUnit string `json:"business_unit"`
Gender string `json:"gender"`
Salary string `json:"salary"`
City string `json:"city"`
Country string `json:"country"`
}
// Init clients var as a slice to store the database
var clients []Client
func main() {
//generate mock data
clients = append(clients,
Client{ID: "2002",
Name: "Kai Le",
JobTitle: "Controls Engineer",
BusinessUnit: "Manufacturing",
Gender: "Male",
Salary: "$92,368",
City: "Columbus",
Country: "United States"},
Client{ID: "2003",
Name: "Robert Patel",
JobTitle: "Analyst",
BusinessUnit: "Corporate",
Gender: "Male",
Salary: "$45,703",
City: "Chicago",
Country: "United States"},
Client{ID: "2004",
Name: "Harper Castillo",
JobTitle: "Network Administrator",
BusinessUnit: "Research & Development",
Gender: "Male",
Salary: "$83,576",
City: "Shanghai",
Country: "China"},
Client{ID: "2005",
Name: "Jade Hu",
JobTitle: "Sr. Analyst",
BusinessUnit: "Speciality Products",
Gender: "Female",
Salary: "$89,744",
City: "Chongqing",
Country: "China"},
Client{ID: "2006",
Name: "Jose Wong",
JobTitle: "Director",
BusinessUnit: "Manufacturing",
Gender: "Male",
Salary: "$150,558",
City: "Phoenix",
Country: "United States"},
Client{ID: "2007",
Name: "Sophia Gutierrez`",
JobTitle: "Manager",
BusinessUnit: "Special Products",
Gender: "Female",
Salary: "$102,649",
City: "Austin",
Country: "United States"},
Client{ID: "2008",
Name: "Lillian Lewis",
JobTitle: "Technical Architect",
BusinessUnit: "Research & Development",
Gender: "Female",
Salary: "$83,323",
City: "Seattle",
Country: "United States"})
//initialize router
router := mux.NewRouter()
//endpoints
router.HandleFunc("/client", getClients).Methods("GET")
router.HandleFunc("/client/{id}", getClient).Methods("GET")
router.HandleFunc("/client", createClient).Methods("POST")
router.HandleFunc("/client/{id}", updateClient).Methods("POST")
router.HandleFunc("/client/{id}", deleteClient).Methods("DELETE")
log.Fatal(http.ListenAndServe(":5000", router))
}