@@ -7,8 +7,10 @@ package rest
77
88import (
99 "errors"
10+ "log"
1011 "net/http"
1112 "regexp"
13+ "strings"
1214
1315 "github.com/go-rs/rest-api-framework/utils"
1416)
@@ -69,23 +71,89 @@ func (api *API) Route(method string, pattern string, handle Handler) {
6971 })
7072}
7173
74+ func (api * API ) Use (handle Handler ) {
75+ task := interceptor {
76+ handle : handle ,
77+ }
78+ api .interceptors = append (api .interceptors , task )
79+ }
80+
81+ func (api * API ) All (pattern string , handle Handler ) {
82+ api .Route ("" , pattern , handle )
83+ }
84+
85+ func (api * API ) Get (pattern string , handle Handler ) {
86+ api .Route (http .MethodGet , pattern , handle )
87+ }
88+
89+ func (api * API ) Post (pattern string , handle Handler ) {
90+ api .Route (http .MethodPost , pattern , handle )
91+ }
92+
93+ func (api * API ) Put (pattern string , handle Handler ) {
94+ api .Route (http .MethodPut , pattern , handle )
95+ }
96+
97+ func (api * API ) Delete (pattern string , handle Handler ) {
98+ api .Route (http .MethodDelete , pattern , handle )
99+ }
100+
101+ func (api * API ) Options (pattern string , handle Handler ) {
102+ api .Route (http .MethodOptions , pattern , handle )
103+ }
104+
105+ func (api * API ) Head (pattern string , handle Handler ) {
106+ api .Route (http .MethodHead , pattern , handle )
107+ }
108+
109+ func (api * API ) Patch (pattern string , handle Handler ) {
110+ api .Route (http .MethodPatch , pattern , handle )
111+ }
112+
113+ func (api * API ) Exception (err string , handle Handler ) {
114+ exp := exception {
115+ message : err ,
116+ handle : handle ,
117+ }
118+ api .exceptions = append (api .exceptions , exp )
119+ }
120+
121+ func (api * API ) UnhandledException (handle Handler ) {
122+ api .unhandled = handle
123+ }
124+
125+ var (
126+ ErrNotFound = errors .New ("URL_NOT_FOUND" )
127+ ErrUncaughtException = errors .New ("UNCAUGHT_EXCEPTION" )
128+ )
129+
72130/**
73131 * Required handle for http module
74132 */
75133func (api API ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
76134
77- urlPath := []byte (req .URL .Path )
78-
135+ // STEP 1: initialize context
79136 ctx := Context {
80137 Request : req ,
81138 Response : res ,
82139 Query : req .URL .Query (),
83140 }
84141
85- // STEP 1: initialize context
86142 ctx .init ()
87143 defer ctx .destroy ()
88144
145+ defer func () {
146+ err := recover ()
147+ if err != nil {
148+ log .Fatalln ("uncaught exception - " , err )
149+ if ! ctx .end {
150+ ctx .err = ErrUncaughtException
151+ ctx .unhandledException ()
152+ return
153+ }
154+ }
155+ }()
156+
89157 // STEP 2: execute all interceptors
90158 for _ , task := range api .interceptors {
91159 if ctx .end || ctx .err != nil {
@@ -96,12 +164,13 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
96164 }
97165
98166 // STEP 3: check routes
167+ urlPath := []byte (req .URL .Path )
99168 for _ , route := range api .routes {
100169 if ctx .end || ctx .err != nil {
101170 break
102171 }
103172
104- if (route .method == "" || route .method == req .Method ) && route .regex .Match (urlPath ) {
173+ if (route .method == "" || strings . EqualFold ( route .method , req .Method ) ) && route .regex .Match (urlPath ) {
105174 ctx .found = route .method != "" //?
106175 ctx .Params = utils .Exec (route .regex , route .params , urlPath )
107176 route .handle (& ctx )
@@ -122,7 +191,7 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
122191 // STEP 5: unhandled exceptions
123192 if ! ctx .end {
124193 if ctx .err == nil && ! ctx .found {
125- ctx .err = errors . New ( "URL_NOT_FOUND" )
194+ ctx .err = ErrNotFound
126195 }
127196
128197 if api .unhandled != nil {
@@ -135,54 +204,3 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
135204 ctx .unhandledException ()
136205 }
137206}
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- }
0 commit comments