66package rest
77
88import (
9- "./utils"
109 "errors"
1110 "fmt"
11+ "github.com/go-rs/rest-api-framework/utils"
1212 "net/http"
1313 "regexp"
1414)
1515
16+ type Handler func (ctx * Context )
17+
1618/**
1719 * API - Application
1820 */
1921type API struct {
22+ prefix string
2023 routes []route
2124 interceptors []interceptor
2225 exceptions []exception
23- unhandled func ( ctx * Context )
26+ unhandled Handler
2427}
2528
2629/**
@@ -31,28 +34,28 @@ type route struct {
3134 pattern string
3235 regex * regexp.Regexp
3336 params []string
34- handle func ( ctx * Context )
37+ handle Handler
3538}
3639
3740/**
3841 * Request interceptor
3942 */
4043type interceptor struct {
41- handle func ( ctx * Context )
44+ handle Handler
4245}
4346
4447/**
4548 * Exception Route
4649 */
4750type exception struct {
4851 message string
49- handle func ( ctx * Context )
52+ handle Handler
5053}
5154
5255/**
5356 * Common Route
5457 */
55- func (api * API ) Route (method string , pattern string , handle func ( ctx * Context ) ) {
58+ func (api * API ) Route (method string , pattern string , handle Handler ) {
5659 regex , params , err := utils .Compile (pattern )
5760 if err != nil {
5861 fmt .Println ("Error in pattern" , err )
@@ -98,8 +101,8 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
98101 break
99102 }
100103
101- if route .method == req .Method && route .regex .Match (urlPath ) {
102- ctx .found = true
104+ if ( route .method == "" || route . method == req .Method ) && route .regex .Match (urlPath ) {
105+ ctx .found = route . method != "" //?
103106 ctx .Params = utils .Exec (route .regex , route .params , urlPath )
104107 route .handle (& ctx )
105108 }
@@ -133,49 +136,53 @@ func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
133136 }
134137}
135138
136- func (api * API ) Use (handle func ( ctx * Context ) ) {
139+ func (api * API ) Use (handle Handler ) {
137140 task := interceptor {
138141 handle : handle ,
139142 }
140143 api .interceptors = append (api .interceptors , task )
141144}
142145
143- func (api * API ) GET (pattern string , handle func (ctx * Context )) {
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 ) {
144151 api .Route ("GET" , pattern , handle )
145152}
146153
147- func (api * API ) POST (pattern string , handle func ( ctx * Context ) ) {
154+ func (api * API ) Post (pattern string , handle Handler ) {
148155 api .Route ("POST" , pattern , handle )
149156}
150157
151- func (api * API ) PUT (pattern string , handle func ( ctx * Context ) ) {
158+ func (api * API ) Put (pattern string , handle Handler ) {
152159 api .Route ("PUT" , pattern , handle )
153160}
154161
155- func (api * API ) DELETE (pattern string , handle func ( ctx * Context ) ) {
162+ func (api * API ) Delete (pattern string , handle Handler ) {
156163 api .Route ("DELETE" , pattern , handle )
157164}
158165
159- func (api * API ) OPTIONS (pattern string , handle func ( ctx * Context ) ) {
166+ func (api * API ) Options (pattern string , handle Handler ) {
160167 api .Route ("OPTIONS" , pattern , handle )
161168}
162169
163- func (api * API ) HEAD (pattern string , handle func ( ctx * Context ) ) {
170+ func (api * API ) Head (pattern string , handle Handler ) {
164171 api .Route ("HEAD" , pattern , handle )
165172}
166173
167- func (api * API ) PATCH (pattern string , handle func ( ctx * Context ) ) {
174+ func (api * API ) Patch (pattern string , handle Handler ) {
168175 api .Route ("PATCH" , pattern , handle )
169176}
170177
171- func (api * API ) Exception (err string , handle func ( ctx * Context ) ) {
178+ func (api * API ) Exception (err string , handle Handler ) {
172179 exp := exception {
173180 message : err ,
174181 handle : handle ,
175182 }
176183 api .exceptions = append (api .exceptions , exp )
177184}
178185
179- func (api * API ) UnhandledException (handle func ( ctx * Context ) ) {
186+ func (api * API ) UnhandledException (handle Handler ) {
180187 api .unhandled = handle
181188}
0 commit comments