Skip to content

Commit 3540ace

Browse files
committed
Added recovery() and gzip support
1 parent 203194b commit 3540ace

File tree

8 files changed

+192
-103
lines changed

8 files changed

+192
-103
lines changed

api.go

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package rest
77

88
import (
99
"errors"
10+
"log"
1011
"net/http"
1112
"regexp"
1213

@@ -69,23 +70,89 @@ func (api *API) Route(method string, pattern string, handle Handler) {
6970
})
7071
}
7172

73+
func (api *API) Use(handle Handler) {
74+
task := interceptor{
75+
handle: handle,
76+
}
77+
api.interceptors = append(api.interceptors, task)
78+
}
79+
80+
func (api *API) All(pattern string, handle Handler) {
81+
api.Route("", pattern, handle)
82+
}
83+
84+
func (api *API) Get(pattern string, handle Handler) {
85+
api.Route(http.MethodGet, pattern, handle)
86+
}
87+
88+
func (api *API) Post(pattern string, handle Handler) {
89+
api.Route(http.MethodPost, pattern, handle)
90+
}
91+
92+
func (api *API) Put(pattern string, handle Handler) {
93+
api.Route(http.MethodPut, pattern, handle)
94+
}
95+
96+
func (api *API) Delete(pattern string, handle Handler) {
97+
api.Route(http.MethodDelete, pattern, handle)
98+
}
99+
100+
func (api *API) Options(pattern string, handle Handler) {
101+
api.Route(http.MethodOptions, pattern, handle)
102+
}
103+
104+
func (api *API) Head(pattern string, handle Handler) {
105+
api.Route(http.MethodHead, pattern, handle)
106+
}
107+
108+
func (api *API) Patch(pattern string, handle Handler) {
109+
api.Route(http.MethodPatch, pattern, handle)
110+
}
111+
112+
func (api *API) Exception(err string, handle Handler) {
113+
exp := exception{
114+
message: err,
115+
handle: handle,
116+
}
117+
api.exceptions = append(api.exceptions, exp)
118+
}
119+
120+
func (api *API) UnhandledException(handle Handler) {
121+
api.unhandled = handle
122+
}
123+
124+
var (
125+
ErrNotFound = errors.New("URL_NOT_FOUND")
126+
ErrUncaughtException = errors.New("UNCAUGHT_EXCEPTION")
127+
)
128+
72129
/**
73130
* Required handle for http module
74131
*/
75132
func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
76133

77-
urlPath := []byte(req.URL.Path)
78-
134+
// STEP 1: initialize context
79135
ctx := Context{
80136
Request: req,
81137
Response: res,
82138
Query: req.URL.Query(),
83139
}
84140

85-
// STEP 1: initialize context
86141
ctx.init()
87142
defer ctx.destroy()
88143

144+
defer func() {
145+
err := recover()
146+
if err != nil {
147+
log.Fatalln("uncaught exception - ", err)
148+
if !ctx.end {
149+
ctx.err = ErrUncaughtException
150+
ctx.unhandledException()
151+
return
152+
}
153+
}
154+
}()
155+
89156
// STEP 2: execute all interceptors
90157
for _, task := range api.interceptors {
91158
if ctx.end || ctx.err != nil {
@@ -96,6 +163,7 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
96163
}
97164

98165
// STEP 3: check routes
166+
urlPath := []byte(req.URL.Path)
99167
for _, route := range api.routes {
100168
if ctx.end || ctx.err != nil {
101169
break
@@ -122,7 +190,7 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
122190
// STEP 5: unhandled exceptions
123191
if !ctx.end {
124192
if ctx.err == nil && !ctx.found {
125-
ctx.err = errors.New("URL_NOT_FOUND")
193+
ctx.err = ErrNotFound
126194
}
127195

128196
if api.unhandled != nil {
@@ -135,54 +203,3 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
135203
ctx.unhandledException()
136204
}
137205
}
138-
139-
func (api *API) Use(handle Handler) {
140-
task := interceptor{
141-
handle: handle,
142-
}
143-
api.interceptors = append(api.interceptors, task)
144-
}
145-
146-
func (api *API) All(pattern string, handle Handler) {
147-
api.Route("", pattern, handle)
148-
}
149-
150-
func (api *API) Get(pattern string, handle Handler) {
151-
api.Route("GET", pattern, handle)
152-
}
153-
154-
func (api *API) Post(pattern string, handle Handler) {
155-
api.Route("POST", pattern, handle)
156-
}
157-
158-
func (api *API) Put(pattern string, handle Handler) {
159-
api.Route("PUT", pattern, handle)
160-
}
161-
162-
func (api *API) Delete(pattern string, handle Handler) {
163-
api.Route("DELETE", pattern, handle)
164-
}
165-
166-
func (api *API) Options(pattern string, handle Handler) {
167-
api.Route("OPTIONS", pattern, handle)
168-
}
169-
170-
func (api *API) Head(pattern string, handle Handler) {
171-
api.Route("HEAD", pattern, handle)
172-
}
173-
174-
func (api *API) Patch(pattern string, handle Handler) {
175-
api.Route("PATCH", pattern, handle)
176-
}
177-
178-
func (api *API) Exception(err string, handle Handler) {
179-
exp := exception{
180-
message: err,
181-
handle: handle,
182-
}
183-
api.exceptions = append(api.exceptions, exp)
184-
}
185-
186-
func (api *API) UnhandledException(handle Handler) {
187-
api.unhandled = handle
188-
}

api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ func TestAPI_ServeHTTP(t *testing.T) {
118118
defer dummy.Close()
119119

120120
res, err := http.Get(dummy.URL)
121-
122121
if err != nil {
123122
t.Error("ServeHTTP error")
123+
return
124124
}
125125

126126
greeting, err := ioutil.ReadAll(res.Body)
127-
res.Body.Close()
127+
_ = res.Body.Close()
128128
if err != nil {
129129
t.Error("ServeHTTP error")
130130
}

0 commit comments

Comments
 (0)