-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (83 loc) · 1.7 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
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"log"
"net/http"
"time"
)
type config struct {
port int
dsn string
}
type post struct {
ID int
Title string
Slug string
Summary string
Content string
PublishedAt time.Time
}
type apps struct {
DB *sql.DB
}
func JSON(w http.ResponseWriter, status int, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
js, err := json.Marshal(data)
if err != nil {
return err
}
w.Write(js)
return nil
}
func (a *apps) findAllPost(ctx context.Context) ([]*post, error) {
query := fmt.Sprintf(`SELECT * FROM post`)
rows, err := a.DB.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []*post
for rows.Next() {
var post post
err := rows.Scan(&post.ID, &post.Title, &post.Slug, &post.Summary, &post.Content, &post.PublishedAt)
if err != nil {
return nil, err
}
posts = append(posts, &post)
}
return posts, nil
}
func (a *apps) getPosts(w http.ResponseWriter, r *http.Request) {
posts, err := a.findAllPost(r.Context())
if err != nil {
JSON(w, http.StatusBadRequest, err)
}
JSON(w, http.StatusOK, posts)
}
func main() {
var cfg = config{
port: 8000,
dsn: "postgres://app:app@postgres-container/app?sslmode=disable",
}
db, err := open(cfg)
if err != nil {
log.Fatal(err)
return
}
defer db.Close()
app := apps{db}
http.HandleFunc("/posts", app.getPosts)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", cfg.port), nil))
}
func open(cfg config) (*sql.DB, error) {
db, err := sql.Open("postgres", cfg.dsn)
if err != nil {
return nil, err
}
return db, nil
}