forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (85 loc) · 2.65 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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how to create a basic CRUD based web api
// for customers.
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/ardanlabs/gotraining/topics/web/customer"
"github.com/gorilla/pat"
)
// App loads the entire API set together for use.
func App() http.Handler {
// Create a version of the pat router.
r := pat.New()
// Define the routes and order matters.
r.Get("/customers/{id}", showHandler)
r.Get("/customers", indexHandler)
r.Post("/customers", createHandler)
// Redirect requests from `/`` to `/customers`.
r.Handle("/", http.RedirectHandler("/customers", http.StatusMovedPermanently))
return r
}
// indexHandler returns the entire list of customers in the DB.
func indexHandler(res http.ResponseWriter, req *http.Request) {
// Retrieve the list of customers, encode to JSON
// and send the response.
if err := json.NewEncoder(res).Encode(customer.All()); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
}
// showHandler returns a single specified customer.
func showHandler(res http.ResponseWriter, req *http.Request) {
// Retrieve the customer id from the request.
idStr := req.URL.Query().Get(":id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
// Retrieve that customer from the DB.
c, err := customer.Find(id)
if err != nil {
http.Error(res, err.Error(), http.StatusNotFound)
return
}
// Encode the customer to JSON and send the response.
if err := json.NewEncoder(res).Encode(c); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
}
// createHandler adds new customers to the DB.
func createHandler(res http.ResponseWriter, req *http.Request) {
// Create a customer value.
var c customer.Customer
// Encode the customer document received into the customer value.
err := json.NewDecoder(req.Body).Decode(&c)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
// Save the customer in the DB.
c.ID, err = customer.Save(c)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
// Encode the customer to JSON and send the response.
b, err := json.Marshal(&c)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusCreated)
res.Write(b)
}
func main() {
// Start the http server to handle the request for
// both versions of the API.
log.Fatal(http.ListenAndServe(":3000", App()))
}