1- /*!
2- * rest-api-framework
3- * Copyright(c) 2019 Roshan Gade
4- * MIT Licensed
5- */
1+ // go-rs/rest-api-framework
2+ // Copyright(c) 2019 Roshan Gade. All rights reserved.
3+ // MIT Licensed
4+
65package rest
76
87import (
@@ -15,11 +14,16 @@ import (
1514 "github.com/go-rs/rest-api-framework/utils"
1615)
1716
17+ // Handler function is used to perform a specified task on request.
18+ // In handler function, you will get rest context object,
19+ // which carries request, response writer objects and other methods too.
1820type Handler func (ctx * Context )
1921
20- /**
21- * API - Application
22- */
22+ // API
23+ // It provides all methods, which are required to setup all kind of routes.
24+ // It manages request interceptor/middlewares. which are used to intercept requests calls before performing actual operation,
25+ // such as authentication, method override, request logger, etc.
26+ // Also, it handles errors, which are thrown by users
2327type API struct {
2428 prefix string
2529 routes []route
@@ -28,9 +32,7 @@ type API struct {
2832 unhandled Handler
2933}
3034
31- /**
32- * Route
33- */
35+ // routes, which help to find and execute/perform the exact or matched url path
3436type route struct {
3537 method string
3638 pattern string
@@ -39,97 +41,113 @@ type route struct {
3941 handle Handler
4042}
4143
42- /**
43- * Request interceptor
44- */
44+ // request interceptors, which help to intercept every request before executing the respective route
4545type interceptor struct {
4646 handle Handler
4747}
4848
49- /**
50- * Exception Route
51- */
49+ // user exceptions, which is a common way to handle an error thrown by user
5250type exception struct {
5351 message string
5452 handle Handler
5553}
5654
57- /**
58- * Common Route
59- */
55+ // Initialize an API with prefix value and return the API pointer
56+ func New (prefix string ) * API {
57+ return & API {
58+ prefix : prefix ,
59+ }
60+ }
61+
62+ // Route method is used to define specific routes with handler.
63+ // You can use http method, declare patten and finally you have to pass handler
6064func (api * API ) Route (method string , pattern string , handle Handler ) {
65+ pattern = api .prefix + pattern
6166 regex , params , err := utils .Compile (pattern )
6267 if err != nil {
6368 panic (err )
6469 }
6570 api .routes = append (api .routes , route {
66- method : method ,
71+ method : strings . ToUpper ( method ) ,
6772 pattern : pattern ,
6873 regex : regex ,
6974 params : params ,
7075 handle : handle ,
7176 })
7277}
7378
79+ // Use method is use to declare interceptors/middlewares
80+ // It executes on all type method request
7481func (api * API ) Use (handle Handler ) {
7582 task := interceptor {
7683 handle : handle ,
7784 }
7885 api .interceptors = append (api .interceptors , task )
7986}
8087
88+ // All method is slightly similar to Use method,
89+ // but in All method you can use pattern before intercepting any request
8190func (api * API ) All (pattern string , handle Handler ) {
8291 api .Route ("" , pattern , handle )
8392}
8493
94+ // Get method is used for GET http method with specific pattern
8595func (api * API ) Get (pattern string , handle Handler ) {
8696 api .Route (http .MethodGet , pattern , handle )
8797}
8898
99+ // Post method is used for POST http method with specific pattern
89100func (api * API ) Post (pattern string , handle Handler ) {
90101 api .Route (http .MethodPost , pattern , handle )
91102}
92103
104+ // Put method is used for PUT http method with specific pattern
93105func (api * API ) Put (pattern string , handle Handler ) {
94106 api .Route (http .MethodPut , pattern , handle )
95107}
96108
109+ // Delete method is used for DELETE http method with specific pattern
97110func (api * API ) Delete (pattern string , handle Handler ) {
98111 api .Route (http .MethodDelete , pattern , handle )
99112}
100113
114+ // Options method is used for OPTIONS http method with specific pattern
101115func (api * API ) Options (pattern string , handle Handler ) {
102116 api .Route (http .MethodOptions , pattern , handle )
103117}
104118
119+ // Head method is used for HEAD http method with specific pattern
105120func (api * API ) Head (pattern string , handle Handler ) {
106121 api .Route (http .MethodHead , pattern , handle )
107122}
108123
124+ // Patch method is used for PATCH http method with specific pattern
109125func (api * API ) Patch (pattern string , handle Handler ) {
110126 api .Route (http .MethodPatch , pattern , handle )
111127}
112128
113- func (api * API ) Exception (err string , handle Handler ) {
129+ // On method is used to handle a custom errors thrown by users
130+ func (api * API ) On (err string , handle Handler ) {
114131 exp := exception {
115132 message : err ,
116133 handle : handle ,
117134 }
118135 api .exceptions = append (api .exceptions , exp )
119136}
120137
138+ // UnhandledException method is used to handle all unhandled exceptions
121139func (api * API ) UnhandledException (handle Handler ) {
122140 api .unhandled = handle
123141}
124142
143+ // error variables to handle expected errors
125144var (
126- ErrNotFound = errors .New ("URL_NOT_FOUND" )
127- ErrUncaughtException = errors .New ("UNCAUGHT_EXCEPTION" )
145+ errNotFound = errors .New ("URL_NOT_FOUND" )
146+ errUncaughtException = errors .New ("UNCAUGHT_EXCEPTION" )
128147)
129148
130- /**
131- * Required handle for http module
132- */
149+ // It's required handle for http module.
150+ // Every request travels from this method.
133151func (api * API ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
134152
135153 // STEP 1: initialize context
@@ -145,12 +163,13 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
145163 defer func () {
146164 err := recover ()
147165 if err != nil {
148- log .Fatalln ("uncaught exception - " , err )
149- if ! ctx .end {
150- ctx .err = ErrUncaughtException
166+ //TODO: log only with debugger mode
167+ log .Println ("uncaught exception - " , err )
168+ if ctx .end == false {
169+ ctx .err = errUncaughtException
151170 ctx .unhandledException ()
152- return
153171 }
172+ return
154173 }
155174 }()
156175
@@ -191,7 +210,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
191210 // STEP 5: unhandled exceptions
192211 if ! ctx .end {
193212 if ctx .err == nil && ! ctx .found {
194- ctx .err = ErrNotFound
213+ ctx .err = errNotFound
195214 }
196215
197216 if api .unhandled != nil {
0 commit comments