-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (101 loc) · 2.5 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
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"os"
_ "github.com/lib/pq"
)
func main() {
port := os.Getenv("PORT")
dsn := os.Getenv("DSN")
db, err := sql.Open("postgres", dsn)
if err != nil {
log.Fatal(err)
}
if err = db.Ping(); err != nil {
log.Fatal(err)
}
defer db.Close()
server := NewServer(db)
log.Fatal(http.ListenAndServe(":"+port, server))
}
type Contact struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
type Server struct {
mux *http.ServeMux
db *sql.DB
}
func NewServer(db *sql.DB) *Server {
mux := http.NewServeMux()
server := &Server{mux, db}
mux.HandleFunc("/msg", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "text/plain")
w.Write([]byte("hello!?"))
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
server.getContacts(w, r)
case http.MethodPost:
server.postContacts(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
return server
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func (s *Server) getContacts(w http.ResponseWriter, r *http.Request) {
rows, err := s.db.Query("select id, name, email from contacts limit 100")
if err != nil {
internalServerError(w, err)
return
}
contacts := make([]Contact, 0)
for rows.Next() {
var contact Contact
err := rows.Scan(&contact.ID, &contact.Name, &contact.Email)
if err != nil {
internalServerError(w, err)
return
}
contacts = append(contacts, contact)
}
if err = rows.Err(); err != nil {
internalServerError(w, err)
return
}
w.Header().Add("content-type", "application/json")
if err := json.NewEncoder(w).Encode(contacts); err != nil {
log.Println(err)
}
}
func (s *Server) postContacts(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var contact Contact
if err := json.NewDecoder(r.Body).Decode(&contact); err != nil {
// Bad request?
internalServerError(w, err)
return
}
row := s.db.QueryRow("insert into contacts (name, email) values ($1, $2) returning id", contact.Name, contact.Email)
if err := row.Scan(&contact.ID); err != nil {
internalServerError(w, err)
return
}
w.Header().Add("content-type", "application/json")
if err := json.NewEncoder(w).Encode(contact); err != nil {
log.Println(err)
}
}
func internalServerError(w http.ResponseWriter, err error) {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}