forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
81 lines (68 loc) · 1.52 KB
/
blog.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
package main
import (
"database/sql"
"encoding/json"
"net/http"
)
type api struct {
db *sql.DB
}
type post struct {
ID int
Title string
Body string
}
func (a *api) posts(w http.ResponseWriter, r *http.Request) {
rows, err := a.db.Query("SELECT id, title, body FROM posts")
if err != nil {
a.fail(w, "failed to fetch posts: "+err.Error(), 500)
return
}
defer rows.Close()
var posts []*post
for rows.Next() {
p := &post{}
if err := rows.Scan(&p.ID, &p.Title, &p.Body); err != nil {
a.fail(w, "failed to scan post: "+err.Error(), 500)
return
}
posts = append(posts, p)
}
if rows.Err() != nil {
a.fail(w, "failed to read all posts: "+rows.Err().Error(), 500)
return
}
data := struct {
Posts []*post
}{posts}
a.ok(w, data)
}
func main() {
// @NOTE: the real connection is not required for tests
db, err := sql.Open("mysql", "root@/blog")
if err != nil {
panic(err)
}
app := &api{db: db}
http.HandleFunc("/posts", app.posts)
http.ListenAndServe(":8080", nil)
}
func (a *api) fail(w http.ResponseWriter, msg string, status int) {
w.Header().Set("Content-Type", "application/json")
data := struct {
Error string
}{Error: msg}
resp, _ := json.Marshal(data)
w.WriteHeader(status)
w.Write(resp)
}
func (a *api) ok(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
resp, err := json.Marshal(data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
a.fail(w, "oops something evil has happened", 500)
return
}
w.Write(resp)
}