/
service.go
executable file
·335 lines (291 loc) · 8.64 KB
/
service.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package controllers
import (
"context"
"database/sql"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/bayugyug/rest-api-booking/config"
"github.com/bayugyug/rest-api-booking/driver"
"github.com/bayugyug/rest-api-booking/utils"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
)
const (
svcOptionWithHandler = "svc-opts-handler"
svcOptionWithAddress = "svc-opts-address"
svcOptionWithDbConfig = "svc-opts-db-config"
)
type Service struct {
Router *chi.Mux
Address string
Api *ApiHandler
Config driver.DbConnectorConfig
DB *sql.DB
Context context.Context
}
//api global handler
var ApiService *Service
//WithSvcOptHandler opts for handler
func WithSvcOptHandler(r *ApiHandler) *config.Option {
return config.NewOption(svcOptionWithHandler, r)
}
//WithSvcOptAddress opts for port#
func WithSvcOptAddress(r string) *config.Option {
return config.NewOption(svcOptionWithAddress, r)
}
//WithSvcOptDbConf opts for db connector
func WithSvcOptDbConf(r driver.DbConnectorConfig) *config.Option {
return config.NewOption(svcOptionWithDbConfig, r)
}
//NewService service new instance
func NewService(opts ...*config.Option) (*Service, error) {
//default
svc := &Service{
Address: ":8989",
Api: &ApiHandler{},
Context: context.Background(),
}
//add options if any
for _, o := range opts {
switch o.Name() {
case svcOptionWithHandler:
if s := o.Value().(*ApiHandler); s != nil {
svc.Api = s
}
case svcOptionWithAddress:
if s := o.Value().(string); s != "" {
svc.Address = s
}
case svcOptionWithDbConfig:
s := o.Value().(driver.DbConnectorConfig)
svc.Config = s
}
}
//set the actual router
svc.Router = svc.MapRoute()
//get db
dbh, err := driver.NewDbConnector(svc.Config)
if err != nil {
return svc, err
}
//save
svc.DB = dbh
return svc, nil
}
//Run run the http server based on settings
func (svc *Service) Run() {
//gracious timing
srv := &http.Server{
Addr: svc.Address,
Handler: svc.Router,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 60 * time.Second,
}
//async run
go func() {
log.Println("Listening on port ", svc.Address)
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
os.Exit(0)
}
}()
//watcher
stopChan := make(chan os.Signal)
signal.Notify(stopChan, os.Interrupt)
<-stopChan
log.Println("Shutting down service...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
srv.Shutdown(ctx)
defer cancel()
log.Println("Server gracefully stopped!")
}
//MapRoute route map all endpoints
func (svc *Service) MapRoute() *chi.Mux {
// Multiplexer
router := chi.NewRouter()
// Basic settings
router.Use(
render.SetContentType(render.ContentTypeJSON),
middleware.Logger,
middleware.DefaultCompress,
middleware.StripSlashes,
middleware.Recoverer,
middleware.RequestID,
middleware.RealIP,
)
// Basic gracious timing
router.Use(middleware.Timeout(60 * time.Second))
// Basic CORS
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
})
router.Use(cors.Handler)
router.Get("/", svc.Api.IndexPage)
/*
@Driver
POST /v1/api/driver
PUT /v1/api/driver
GET /v1/api/driver/{mobile}
DELETE /v1/api/driver
GET /v1/api/drivers
@Customer
POST /v1/api/customer
PUT /v1/api/customer
GET /v1/api/customer/{mobile}
DELETE /v1/api/customer
@Location
PUT /v1/api/location
GET /v1/api/location/{who}/{id}
@Booking
POST /v1/api/booking/
PUT /v1/api/booking/
GET /v1/api/booking/{booking_id}
PUT /v1/api/status/{who}
PUT /v1/api/password/{who}
PUT /v1/api/otp
*/
// Protected routes
router.Route("/v1", func(r chi.Router) {
r.Use(svc.SetContextKeyVal("api.version", "v1"))
r.Mount("/api/driver",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/", api.UpdateDriver)
sr.Get("/{id}", api.GetDriver)
sr.Delete("/{id}", api.DeleteDriver)
return sr
}(svc.Api))
r.Mount("/api/drivers",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Get("/{lat}/{lon}", api.GetDriversList)
return sr
}(svc.Api))
r.Mount("/api/vehiclestatus",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/", api.UpdateVehicleStatus)
return sr
}(svc.Api))
r.Mount("/api/customer",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/", api.UpdateCustomer)
sr.Get("/{id}", api.GetCustomer)
sr.Delete("/{id}", api.DeleteCustomer)
return sr
}(svc.Api))
r.Mount("/api/location",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/", api.UpdateLocation)
sr.Get("/{who}/{mobile}", api.GetLocation)
return sr
}(svc.Api))
r.Mount("/api/booking",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/", api.UpdateBooking)
sr.Post("/", api.CreateBooking)
sr.Get("/{id}", api.GetBooking)
sr.Put("/pickup-time/{id}", api.UpdateBookingPickupTime)
sr.Put("/dropoff-time/{id}", api.UpdateBookingDropTime)
sr.Put("/status/customer/{id}", api.UpdateBookingCustomerStatus)
sr.Put("/status/driver/{id}/{status}", api.UpdateBookingDriverStatus)
return sr
}(svc.Api))
r.Mount("/api/address",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Post("/", api.GetAddress)
return sr
}(svc.Api))
r.Mount("/api/password",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/customer", api.UpdateCustomerPassword)
sr.Put("/driver", api.UpdateDriverPassword)
return sr
}(svc.Api))
r.Mount("/api/status",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Put("/customer/{id}/{status}", api.UpdateCustomerStatus)
sr.Put("/driver/{id}/{status}", api.UpdateDriverStatus)
return sr
}(svc.Api))
//not-yet implemented ;-)
r.Mount("/api/logout",
func(api *ApiHandler) *chi.Mux {
sr := chi.NewRouter()
sr.Use(jwtauth.Verifier(utils.AppJwtToken.TokenAuth), svc.BearerChecker)
sr.Post("/", api.Logout)
return sr
}(svc.Api))
})
//public-routes
router.Group(func(r chi.Router) {
r.Post("/v1/api/login", svc.Api.Login)
r.Post("/v1/api/customer", svc.Api.CreateCustomer)
r.Post("/v1/api/driver", svc.Api.CreateDriver)
r.Put("/v1/api/otp", svc.Api.UpdateOtp)
})
return router
}
//SetContextKeyVal version context
func (svc *Service) SetContextKeyVal(k, v string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), k, v))
next.ServeHTTP(w, r)
})
}
}
//BearerChecker check token
func (svc *Service) BearerChecker(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token, _, err := jwtauth.FromContext(r.Context())
if err != nil {
switch err {
default:
log.Println("ERROR:", err)
svc.Api.ReplyErrContent(w, r, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
return
case jwtauth.ErrExpired:
log.Println("ERROR: Expired")
http.Error(w, "Expired", http.StatusUnauthorized)
svc.Api.ReplyErrContent(w, r, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
return
case jwtauth.ErrUnauthorized:
log.Println("ERROR: ErrUnauthorized")
svc.Api.ReplyErrContent(w, r, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
return
}
}
if token == nil || !token.Valid {
svc.Api.ReplyErrContent(w, r, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
return
}
// Token is authenticated, pass it through
next.ServeHTTP(w, r)
})
}