-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
159 lines (141 loc) · 4.01 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/nebhale/client-go/bindings"
"github.com/urfave/negroni"
//"time"
"embed"
"io/fs"
"io/ioutil"
)
var db *sql.DB
//go:embed web/dist/shopbasket
var webStaticContent embed.FS
func HandleGetInventory(w http.ResponseWriter, r *http.Request) {
datastore := Datastore{db}
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
inventory, err := datastore.GetInventory(id)
fmt.Println(inventory)
response, err := json.Marshal(inventory)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
}
func HandleCreateInventory(w http.ResponseWriter, r *http.Request) {
datastore := Datastore{db}
var req Inventory
json.NewDecoder(r.Body).Decode(&req)
inventory, err := datastore.CreateInventory(req)
response, err := json.Marshal(inventory)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write([]byte(response))
}
func HandleListInventory(w http.ResponseWriter, r *http.Request) {
datastore := Datastore{db}
inventory, err := datastore.ListInventory()
fmt.Println(inventory)
response, err := json.Marshal(inventory)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
}
func HandleDeleteInventory(w http.ResponseWriter, r *http.Request) {
datastore := Datastore{db}
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
err := datastore.DeleteInventory(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}
func IOReadDir(root string) ([]string, error) {
var files []string
fileInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}
for _, file := range fileInfo {
files = append(files, file.Name())
}
return files, nil
}
func StatusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"build": "2}`))
}
func main() {
var err error
db, err = InitializeDB()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
}
if db != nil {
defer db.Close()
var greeting string
err = db.QueryRow("select 1").Scan(&greeting)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
fmt.Println(greeting)
}
n, router := Route()
n.UseHandler(router)
n.Run(":8080")
}
func InitializeDB() (*sql.DB, error) {
var err error
b := bindings.FromServiceBindingRoot()
b = bindings.Filter(b, "postgresql")
if len(b) != 1 {
_, _ = fmt.Fprintf(os.Stderr, "Incorrect number of PostgreSQL drivers: %d\n", len(b))
os.Exit(1)
}
connectionString, ok := bindings.Get(b[0], "pgbouncer-uri")
if !ok {
_, _ = fmt.Fprintln(os.Stderr, "No pgbouncer-uri in binding")
//os.Exit(1)
}
db, err = sql.Open("pgx", connectionString)
return db, err
}
func Route() (n *negroni.Negroni, rt *mux.Router) {
router := mux.NewRouter()
router.HandleFunc("/status", StatusHandler).Methods("GET")
router.HandleFunc("/api/inventory/{id}", HandleGetInventory).Methods("GET")
router.HandleFunc("/api/inventory", HandleCreateInventory).Methods("POST")
router.HandleFunc("/api/inventory", HandleListInventory).Methods("GET")
router.HandleFunc("/api/inventory/{id}", HandleDeleteInventory).Methods("DELETE")
webStaticContentRoot, _ := fs.Sub(webStaticContent, "web/dist/shopbasket")
n = negroni.New(negroni.NewRecovery(), negroni.NewLogger(), negroni.NewStatic(http.FS(webStaticContentRoot)))
return n, router
}