Skip to content

Commit 74cf23c

Browse files
authored
Merge pull request #2 from go-rs/develop
alpha.2
2 parents 6d50f40 + 5c6fa3f commit 74cf23c

File tree

7 files changed

+40
-25
lines changed

7 files changed

+40
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ REST API framework for go lang
66
Released alpha version
77
<br>
88
See examples
9-
- Request Interceptors/Middlewares
9+
- Request Interceptors/Middleware
1010
- Routes with URL pattern
1111
- Methods [GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH]
1212
- Extend routes with namespace

api.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package rest
77

88
import (
99
"errors"
10-
"fmt"
1110
"net/http"
1211
"regexp"
1312

@@ -59,8 +58,7 @@ type exception struct {
5958
func (api *API) Route(method string, pattern string, handle Handler) {
6059
regex, params, err := utils.Compile(pattern)
6160
if err != nil {
62-
fmt.Println("Error in pattern", err)
63-
panic(1)
61+
panic(err)
6462
}
6563
api.routes = append(api.routes, route{
6664
method: method,

context.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ func (ctx *Context) Set(key string, val interface{}) {
5959
/**
6060
* Get request data from context
6161
*/
62-
func (ctx *Context) Get(key string) interface{} {
63-
return ctx.data[key]
62+
func (ctx *Context) Get(key string) (val interface{}, exists bool) {
63+
val = ctx.data[key]
64+
exists = val != nil
65+
return
6466
}
6567

6668
/**
@@ -134,11 +136,19 @@ func (ctx *Context) Text(data string) {
134136
* Send data
135137
*/
136138
func (ctx *Context) send(data []byte, err error) {
137-
if ctx.end && err != nil {
139+
if ctx.end {
138140
return
139141
}
142+
143+
if err != nil {
144+
ctx.err = err
145+
return
146+
}
147+
140148
ctx.Response.WriteHeader(ctx.status)
141149
_, err = ctx.Response.Write(data)
150+
151+
//TODO: check - should not be recursive
142152
if err != nil {
143153
ctx.err = err
144154
return

render/json.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package render
77

88
import (
99
"encoding/json"
10+
"errors"
1011
"net/http"
1112
"reflect"
1213
)
@@ -20,21 +21,29 @@ var (
2021
jsonType = "application/json"
2122
)
2223

24+
var (
25+
invalidJson = errors.New("INVALID_JSON_RESPONSE")
26+
)
27+
2328
/**
2429
* JSON Write
2530
*/
26-
func (j JSON) Write(w http.ResponseWriter) ([]byte, error) {
27-
var data []byte
28-
var err error
31+
func (j JSON) Write(w http.ResponseWriter) (data []byte, err error) {
2932
if reflect.TypeOf(j.Body).String() == "string" {
30-
rawIn := json.RawMessage(j.Body.(string))
31-
data, err = rawIn.MarshalJSON()
33+
data, err = json.RawMessage(j.Body.(string)).MarshalJSON()
3234
} else {
3335
data, err = json.Marshal(j.Body)
3436
}
37+
3538
if err != nil {
36-
return nil, err
39+
return
3740
}
41+
42+
if !json.Valid(data) {
43+
err = invalidJson
44+
return
45+
}
46+
3847
w.Header().Set("Content-Type", jsonType)
39-
return data, nil
48+
return
4049
}

render/text.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020
/**
2121
* Text Write
2222
*/
23-
func (j Text) Write(w http.ResponseWriter) ([]byte, error) {
24-
data := []byte(j.Body)
23+
func (j Text) Write(w http.ResponseWriter) (data []byte, err error) {
24+
data = []byte(j.Body)
2525
w.Header().Set("Content-Type", plainType)
26-
return data, nil
26+
return
2727
}

utils/url.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import (
1212

1313
const sep = "/"
1414

15-
func Compile(str string) (*regexp.Regexp, []string, error) {
15+
func Compile(str string) (regex *regexp.Regexp, keys []string, err error) {
1616
pattern := ""
17-
keys := make([]string, 0)
18-
_str := strings.Split(str, "/")
17+
keys = make([]string, 0)
1918

20-
for _, val := range _str {
19+
for _, val := range strings.Split(str, "/") {
2120
if val != "" {
2221
switch val[0] {
2322
case 42:
@@ -45,9 +44,8 @@ func Compile(str string) (*regexp.Regexp, []string, error) {
4544
// pattern += "(?:/)?"
4645
// }
4746

48-
regex, err := regexp.Compile("^" + pattern + "/?$")
49-
50-
return regex, keys, err
47+
regex, err = regexp.Compile("^" + pattern + "/?$")
48+
return
5149
}
5250

5351
func Exec(regex *regexp.Regexp, keys []string, uri []byte) *map[string]string {

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1-alpha.1
1+
0.0.1-alpha.2

0 commit comments

Comments
 (0)