Skip to content

Commit 0befa26

Browse files
committed
Code cleanup and refactoring
1 parent a830c4c commit 0befa26

30 files changed

+572
-493
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,33 @@ go get -u github.com/thinkoner/thinkgo
5252
package main
5353

5454
import (
55-
"github.com/thinkoner/thinkgo"
56-
"fmt"
57-
"github.com/thinkoner/thinkgo/router"
58-
"github.com/thinkoner/thinkgo/context"
55+
"fmt"
56+
57+
"github.com/thinkoner/thinkgo"
58+
"github.com/thinkoner/thinkgo/think"
5959
)
6060

6161
func main() {
62-
think := thinkgo.BootStrap()
63-
think.RegisterRoute(func(route *router.Route) {
62+
th := thinkgo.New()
63+
th.RegisterRoute(func(route *think.Route) {
6464

65-
route.Get("/", func(req *context.Request) *context.Response {
65+
route.Get("/", func(req *think.Req) *think.Res {
6666
return thinkgo.Text("Hello ThinkGo !")
6767
})
6868

69-
route.Get("/ping", func(req *context.Request) *context.Response {
69+
route.Get("/ping", func(req *think.Req) *think.Res {
7070
return thinkgo.Json(map[string]string{
7171
"message": "pong",
7272
})
7373
})
7474

7575
// Dependency injection
76-
route.Get("/user/{name}", func(req *context.Request, name string) *context.Response {
76+
route.Get("/user/{name}", func(req *think.Req, name string) *think.Res {
7777
return thinkgo.Text(fmt.Sprintf("Hello %s !", name))
7878
})
7979
})
8080
// listen and serve on 0.0.0.0:9011
81-
think.Run()
81+
th.Run()
8282
}
8383
```
8484

context/request.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package context
22

33
import (
4+
"bytes"
45
"errors"
6+
"io/ioutil"
57
"net/http"
68
"net/url"
79
"strings"
@@ -232,6 +234,21 @@ func (r *Request) Method() string {
232234
return r.method
233235
}
234236

237+
// GetContent Returns the request body content.
238+
func (r *Request) GetContent() ([]byte, error) {
239+
var body []byte
240+
241+
if body == nil {
242+
body, err := ioutil.ReadAll(r.Request.Body)
243+
if err != nil {
244+
return nil, err
245+
}
246+
r.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
247+
}
248+
249+
return body, nil
250+
}
251+
235252
// Session get the session associated with the request.
236253
func (r *Request) Session() Session {
237254
return r.session

context/response.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,26 @@ func NewResponse() *Response {
9494
r := &Response{
9595
Header: &http.Header{},
9696
}
97+
r.SetCode(http.StatusOK)
9798
r.SetContentType("text/html")
9899
r.SetCharset("utf-8")
99-
r.SetCode(http.StatusOK)
100100
r.CookieHandler = ParseCookieHandler()
101101
return r
102102
}
103103

104104
// NotFoundResponse Create a new HTTP NotFoundResponse
105105
func NotFoundResponse() *Response {
106-
r := NewResponse()
107-
r.SetContent("Not Found")
108-
r.SetCode(http.StatusNotFound)
109-
return r
106+
return NewResponse().SetCode(http.StatusNotFound).SetContent("Not Found")
110107
}
111108

112109
// NotFoundResponse Create a new HTTP Error Response
113110
func ErrorResponse() *Response {
114-
r := NewResponse()
115-
r.SetContent("Server Error")
116-
r.SetCode(http.StatusInternalServerError)
117-
return r
111+
return NewResponse().SetCode(http.StatusInternalServerError).SetContent("Server Error")
118112
}
119113

120114
// Redirect Create a new HTTP Redirect Response
121115
func Redirect(to string) *Response {
122-
r := NewResponse()
116+
r := NewResponse().SetCode(http.StatusMovedPermanently)
123117
r.Header.Set("Location", to)
124-
r.SetCode(http.StatusMovedPermanently)
125118
return r
126119
}

coverage.txt

-120
This file was deleted.

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module github.com/thinkoner/thinkgo
22

3+
go 1.11
4+
35
require (
46
github.com/gomodule/redigo v2.0.0+incompatible
57
github.com/stretchr/testify v1.3.0

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
77
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
98
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

pipeline.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package thinkgo
22

33
import (
44
"container/list"
5+
"html/template"
56
"net/http"
67

7-
"github.com/thinkoner/thinkgo/app"
88
"github.com/thinkoner/thinkgo/context"
99
"github.com/thinkoner/thinkgo/router"
10+
"github.com/thinkoner/thinkgo/think"
1011
)
1112

1213
type Pipeline struct {
13-
handlers []app.Handler
14+
handlers []think.Handler
1415
pipeline *list.List
1516
passable *context.Request
1617
}
@@ -24,13 +25,13 @@ func NewPipeline() *Pipeline {
2425
}
2526

2627
// Pipe Push a Middleware Handler to the pipeline
27-
func (p *Pipeline) Pipe(m app.Handler) *Pipeline {
28+
func (p *Pipeline) Pipe(m think.Handler) *Pipeline {
2829
p.pipeline.PushBack(m)
2930
return p
3031
}
3132

3233
// Pipe Batch push Middleware Handlers to the pipeline
33-
func (p *Pipeline) Through(hls []app.Handler) *Pipeline {
34+
func (p *Pipeline) Through(hls []think.Handler) *Pipeline {
3435
for _, hl := range hls {
3536
p.Pipe(hl)
3637
}
@@ -64,23 +65,29 @@ func (p *Pipeline) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6465
switch result.(type) {
6566
case router.Response:
6667
result.(router.Response).Send(w)
67-
return
68+
break
69+
case template.HTML:
70+
think.Html(string(result.(template.HTML))).Send(w)
71+
break
6872
case http.Handler:
6973
result.(http.Handler).ServeHTTP(w, r)
70-
return
74+
break
75+
default:
76+
think.Response(result).Send(w)
77+
break
7178
}
7279
}
7380

7481
func (p *Pipeline) handler(passable *context.Request, e *list.Element) interface{} {
7582
if e == nil {
7683
return nil
7784
}
78-
hl := e.Value.(app.Handler)
85+
hl := e.Value.(think.Handler)
7986
result := hl.Process(passable, p.closure(e))
8087
return result
8188
}
8289

83-
func (p *Pipeline) closure(e *list.Element) app.Closure {
90+
func (p *Pipeline) closure(e *list.Element) think.Closure {
8491
return func(req *context.Request) interface{} {
8592
e = e.Next()
8693
return p.handler(req, e)

response.go

-37
This file was deleted.

router/parameter.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package router
2+
3+
type parameter struct {
4+
name string
5+
value string
6+
}

0 commit comments

Comments
 (0)