-
Notifications
You must be signed in to change notification settings - Fork 15
/
express.go
218 lines (195 loc) · 5.68 KB
/
express.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
// Package goexpress provides the actual hook that
// enables you to start building your application.
//
// The basic Express() functions returns an instance
// for the express which can be further be used as
// an express hook.
//
// app.Use(), app.Get(), app.Post(), app.Delete(), app.Push()
// app.Put() are the top level functions that can be used in
// the same fashion as the express-js ones are.
package goexpress
import (
"context"
"fmt"
"log"
http "net/http"
"os"
"os/signal"
"time"
)
type express struct {
router *router
server *http.Server
started bool
drainTimeout time.Duration
drainMethod func(ExpressInterface)
properties map[string]interface{}
}
// Express returns a new instance of express
func Express() ExpressInterface {
var exp = &express{}
exp.router = newRouter()
exp.properties = make(map[string]interface{})
return exp
}
// ServeHTTP is the default function to handle HTTP request
func (e *express) ServeHTTP(res http.ResponseWriter, req *http.Request) {
hijack, ok := res.(http.Hijacker)
if !ok {
http.Error(res, "Request Hijacking not supported for this request", http.StatusInternalServerError)
} else {
conn, bufrw, err := hijack.Hijack()
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
var response = newResponse(res, req, bufrw, conn, &e.properties)
var request = newRequest(req, &e.properties)
var index = 0
var executedRoutes = 0
var _next NextFunc
// doctor the request in case of any error
defer func() {
if err := recover(); err != nil {
if !response.HasEnded() {
response.Error(500, "Internal server error")
}
}
}()
_next = func(n NextFunc) {
if response.HasEnded() == true {
// we are done
return
}
var handler, i, isMiddleware = e.router.FindNext(index, request.method, request.url, request)
if i == -1 {
// done handling
if executedRoutes == 0 {
// 404
response.header.SetStatus(404)
response.Write("Not Found")
response.End()
return
}
// should close connection
if response.HasEnded() == false {
response.End()
return
}
} else {
if isMiddleware == false {
executedRoutes++
}
index = i + 1
handler(request, response)
if response.HasEnded() == false {
n(n)
}
}
}
_next(_next)
}
}
// Extension to provide Router.Get functionalities
func (e *express) Get(url string, middleware Middleware) ExpressInterface {
e.router.Get(url, middleware)
return e
}
// Extension to provide Router.Post functionality
func (e *express) Post(url string, middleware Middleware) ExpressInterface {
e.router.Post(url, middleware)
return e
}
// Extension to provide Router.Put functionality
func (e *express) Put(url string, middleware Middleware) ExpressInterface {
e.router.Put(url, middleware)
return e
}
// Extension to provide Router.Patch functionality
func (e *express) Patch(url string, middleware Middleware) ExpressInterface {
e.router.Patch(url, middleware)
return e
}
// Extension to provide Router.Delete functionality
func (e *express) Delete(url string, middleware Middleware) ExpressInterface {
e.router.Delete(url, middleware)
return e
}
// Extension to provide Router.Options functionality
func (e *express) Options(url string, middleware Middleware) ExpressInterface {
e.router.Options(url, middleware)
return e
}
// Extension to provide Router.Use functionality
func (e *express) Use(middleware interface{}) ExpressInterface {
e.router.Use(middleware)
return e
}
// NewRouter returns a new instance of express Router
func NewRouter() Router {
var route = &router{}
return route
}
// Sets global app properties that can be accessed under express struct
func (e *express) SetProp(key string, value interface{}) ExpressInterface {
e.properties[key] = value
return e
}
// Return the app property
func (e *express) GetProp(key string, value interface{}) interface{} {
return e.properties[key]
}
// Starts the App Server
func (e *express) Start(port string) ExpressInterface {
if e.started {
return e
}
server := &http.Server{Addr: "0.0.0.0:" + port}
server.Handler = e
log.Print("Listening at: ", port)
e.server = server
e.started = true
// run a kill trap thread
go e.captureInterrupt()
err := server.ListenAndServe()
if err != nil {
log.Fatal("Server Closed Down:", err)
}
return e
}
// Shutdown tries to stop the running server after a given timeout context
func (e *express) Shutdown(ctx context.Context) error {
log.Println("Stopping the server")
return e.server.Shutdown(ctx)
}
// ShutdownTimeout sets a timeout for draining the requests before shutting down
func (e *express) ShutdownTimeout(t time.Duration) ExpressInterface {
e.drainTimeout = t
return e
}
// BeforeShutdown sets a method as an exit hook
// todo: allow multiples of them
func (e *express) BeforeShutdown(handler func(ExpressInterface)) ExpressInterface {
e.drainMethod = handler
return e
}
func (e *express) captureInterrupt() {
killChannel := make(chan os.Signal, 1)
signal.Notify(killChannel, os.Interrupt)
<-killChannel
fmt.Println("Beginning to shutdown server")
if e.drainMethod != nil {
// call the drainer method
e.drainMethod(e)
}
drainTimeout := e.drainTimeout
// if nothing set, set it default
if drainTimeout == time.Duration(0) {
drainTimeout = 10 * time.Second
}
// stop the server with a default delay
ctx, cancel := context.WithTimeout(context.Background(), drainTimeout)
defer cancel()
e.Shutdown(ctx)
}