-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (138 loc) · 4 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"entgo.io/ent/dialect"
entsql "entgo.io/ent/dialect/sql"
"github.com/gorilla/mux"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/newtonmunene99/go-books/ent"
"github.com/newtonmunene99/go-books/ent/migrate"
"github.com/newtonmunene99/go-books/env"
)
var client *ent.Client
func Open(databaseUrl string) *ent.Client {
db, err := sql.Open("pgx", databaseUrl)
if err != nil {
log.Fatal(err)
}
// Create an ent.Driver from `db`.
drv := entsql.OpenDB(dialect.Postgres, db)
return ent.NewClient(ent.Driver(drv))
}
func main() {
client = Open(fmt.Sprintf("postgresql://%v:%v@127.0.0.1/go-books", env.DB_USERNAME, env.DB_PASSWORD))
ctx := context.Background()
err := client.Schema.Create(
ctx,
migrate.WithDropIndex(true),
migrate.WithDropColumn(true),
)
if err != nil {
log.Fatal(err)
}
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
r := mux.NewRouter()
r.Path("/").Methods("GET").HandlerFunc(ListBooksHandler)
r.Path("/").Methods("POST").HandlerFunc(CreateBookHandler)
r.Path("/categories").Methods("GET").HandlerFunc(ListCategoriesHandler)
r.Path("/categories").Methods("POST").HandlerFunc(CreateCategoryHandler)
srv := &http.Server{
Addr: "0.0.0.0:8080",
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r, // Pass our instance of gorilla/mux in.
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
srv.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}
func ListCategoriesHandler(w http.ResponseWriter, r *http.Request) {
categories, err := client.Category.Query().All(context.Background())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf("%v", categories)))
}
func CreateCategoryHandler(w http.ResponseWriter, r *http.Request) {
var body ent.Category
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
category, err := client.Category.
Create().
SetName(body.Name).
Save(context.Background())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf("%v", category)))
}
func ListBooksHandler(w http.ResponseWriter, r *http.Request) {
books, err := client.Book.Query().All(context.Background())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf("%v", books)))
}
func CreateBookHandler(w http.ResponseWriter, r *http.Request) {
var body ent.Book
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
category, err := client.Category.Get(context.Background(), body.CategoryID)
if err != nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(fmt.Sprintf("Category with Id %v Not Found: %v", body.CategoryID, err)))
}
book, err := client.Book.
Create().
SetTitle(body.Title).
SetYear(body.Year).
SetAuthor(body.Author).
SetCategoryID(category.ID).
AddCategory(category).
Save(context.Background())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf("%v", book)))
}