-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (70 loc) · 1.95 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
package main
import (
"restapi/platform/initialize"
//"database/sql"
"github.com/jmoiron/sqlx"
"fmt"
"log"
"net/http"
"restapi/platform/data"
//"io/ioutil"
"encoding/json"
"gopkg.in/validator.v2"
_ "github.com/lib/pq"
)
//Env Create a custom struct which holds a connection pool
type Env struct {
DB *sqlx.DB
}
func main() {
// Initialize db
initialize.SyncSchemas()
// Initialise the connection pool.
db := data.OpenData()
// Close db when program exits
defer db.Close()
// Create an instance of Env containing the connection pool.
env := &Env{DB: db}
// Use env.booksIndex as the handler function for the /books route and env.PostItems for /posts route.
http.HandleFunc("/data", env.booksIndex)
http.HandleFunc("/post", env.PostItems)
http.ListenAndServe(":3000", nil)
}
// PostItems handler
func (env *Env) PostItems(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var feed data.Item // Struct Item is defined in data package
err := json.NewDecoder(r.Body).Decode(&feed) // Unmarshall JSON request body into feed
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// JSON POST input validator, if not valid, return 500 error
if errs := validator.Validate(feed); errs != nil {
log.Println("Validate error")
http.Error(w, http.StatusText(500), 500)
} else {
err = data.PostAll(env.DB, &feed) // Pass db connection pool and Item struct to PostAll
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(500), 500)
return
}
}
// Return supplied data as a response to client
fmt.Fprintf(w, "Data: %+v", feed)
}
}
// Define booksIndex as a method on Env.
func (env *Env) booksIndex(w http.ResponseWriter, r *http.Request) {
// Pass db connection pool to AllItems
bks, err := data.AllItems(env.DB)
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(500), 500)
return
}
for _, bk := range bks {
fmt.Fprintf(w, "%s", bk.Post)
}
}