Skip to content

Commit 1ed5e5b

Browse files
authored
Merge pull request #9 from go-rs/develop
Documentation as per https://godoc.org/
2 parents 98c1935 + 64eabea commit 1ed5e5b

13 files changed

+95
-86
lines changed

api.go

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package rest
76

87
import (
@@ -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.
1820
type 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
2327
type 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
3436
type 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
4545
type 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
5250
type 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
6064
func (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
7481
func (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
8190
func (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
8595
func (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
89100
func (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
93105
func (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
97110
func (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
101115
func (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
105120
func (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
109125
func (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
121139
func (api *API) UnhandledException(handle Handler) {
122140
api.unhandled = handle
123141
}
124142

143+
// error variables to handle expected errors
125144
var (
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.
133151
func (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 {

context.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package rest
76

87
import (

context_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package rest
76

87
import (

examples/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010
)
1111

1212
func main() {
13-
var api rest.API
1413

15-
user.APIs(&api)
14+
var api = rest.New("")
15+
16+
user.Load(api)
1617

1718
// request interceptor / middleware
1819
// body-parser : json, raw, form-data, etc
@@ -37,5 +38,5 @@ func main() {
3738

3839
fmt.Println("Starting server.")
3940

40-
http.ListenAndServe(":8080", &api)
41+
http.ListenAndServe(":8080", api)
4142
}

examples/user/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"github.com/go-rs/rest-api-framework"
55
)
66

7-
func APIs(api *rest.API) {
7+
func Load(api *rest.API) {
88

99
var user rest.Namespace
1010

namespace.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/*!
2-
* rest-api-framework
3-
* Copyright(c) 2019 Roshan Gade
4-
* MIT Licensed
5-
*/
1+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
2+
// MIT Licensed
3+
64
package rest
75

86
import "net/http"

namespace_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package rest
76

87
import (

render/json.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package render
76

87
import (

render/json_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package render
76

87
import (

render/text.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package render
76

87
import (

render/text_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package render
76

87
import (

utils/url.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package utils
76

87
import (

utils/url_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
65
package utils
76

87
import (

0 commit comments

Comments
 (0)