-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
178 lines (144 loc) · 4.53 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
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
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
)
var (
db DB
port int
)
func init() {
flag.IntVar(&port, "port", 8080, "The port to bind to on localhost")
db.Insert(Article{User: "jane", Body: "Hello, world!"})
db.Insert(Article{User: "john", Body: "Lorem ipsum dolor sit amet."})
}
func main() {
flag.Parse()
router := httprouter.New()
router.GET("/", index)
router.GET("/articles", list)
router.POST("/articles", create)
router.OPTIONS("/articles", options)
router.GET("/articles/:id", fetch)
router.PUT("/articles/:id", update)
router.DELETE("/articles/:id", del)
router.OPTIONS("/articles/:id", options)
router.ServeFiles("/site/*filepath", http.Dir(""))
addr := fmt.Sprintf("localhost:%d", port)
fmt.Printf("\nBlog articles test API!\n\n")
fmt.Printf("This server is now listening on %s\n", addr)
fmt.Printf("If you ran this command from inside your site's folder you can view your site at http://%s/site/\n", addr)
fmt.Println("You can make the following API requests")
fmt.Printf("GET http://%s/articles -- Get all articles\n", addr)
fmt.Printf("POST http://%s/articles -- Make an article. Send a body like {\"user\": \"alice\", \"body\": \"foo\"}\n", addr)
fmt.Printf("GET http://%s/articles/:id -- Get a particular article\n", addr)
fmt.Printf("PUT http://%s/articles/:id -- Update an article. Send a body like {\"user\": \"anna\", \"body\": \"bar\"}\n", addr)
fmt.Printf("DELETE http://%s/articles/:id -- Delete an article\n", addr)
fmt.Printf("\nPress Ctrl-c at any time to kill this server\n\n")
var h http.Handler = router
h = mwCORSHeaders(h)
h = mwLogger(h)
log.Fatal(http.ListenAndServe(addr, h))
}
func mwLogger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Serving %-6s %s\n", r.Method, r.URL.Path)
h.ServeHTTP(w, r)
})
}
func mwCORSHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
h.ServeHTTP(w, r)
})
}
func serveJSON(w http.ResponseWriter, data interface{}, status int) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(&data); err != nil {
s := http.StatusInternalServerError
http.Error(w, http.StatusText(s), s)
return
}
}
func idFromParams(ps httprouter.Params) (int, error) {
idStr := ps.ByName("id")
id, err := strconv.Atoi(idStr)
if err != nil {
return 0, err
}
return id, nil
}
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintln(w, "Welcome to the Articles test API!")
}
func options(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Access-Control-Allow-Methods", "PUT, POST, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.WriteHeader(http.StatusOK)
}
func list(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveJSON(w, db.FindAll(), http.StatusOK)
}
func create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
article := Article{}
if err := json.NewDecoder(r.Body).Decode(&article); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
article = db.Insert(article)
serveJSON(w, article, http.StatusCreated)
}
func fetch(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var (
id int
err error
)
if id, err = idFromParams(ps); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if article, err := db.FindOne(id); err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
serveJSON(w, article, http.StatusOK)
}
}
func update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var (
id int
err error
)
if id, err = idFromParams(ps); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
article, err := db.FindOne(id)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if err := json.NewDecoder(r.Body).Decode(&article); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
db.Update(article)
serveJSON(w, article, http.StatusOK)
}
func del(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var (
id int
err error
)
if id, err = idFromParams(ps); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
db.Delete(id)
w.WriteHeader(http.StatusNoContent)
}