Skip to content

Commit ce29e16

Browse files
authored
Merge pull request #230 from devfeel/develop
add Tools include some useful functions
2 parents 78e780e + 210c964 commit ce29e16

6 files changed

Lines changed: 51 additions & 2 deletions

File tree

consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotweb
33
// Global define
44
const (
55
// Version current version
6-
Version = "1.7.7"
6+
Version = "1.7.11"
77
)
88

99
// Log define

context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type (
4646
RouterNode() RouterNode
4747
RouterParams() Params
4848
Handler() HttpHandle
49+
Tools() *Tools
4950
AppItems() core.ConcurrenceMap
5051
Cache() cache.Cache
5152
Items() core.ConcurrenceMap
@@ -116,6 +117,7 @@ type (
116117
items core.ConcurrenceMap
117118
viewData core.ConcurrenceMap
118119
handler HttpHandle
120+
tools *Tools
119121
}
120122

121123
WebSocket struct {
@@ -269,6 +271,15 @@ func (ctx *HttpContext) Items() core.ConcurrenceMap {
269271
return ctx.items
270272
}
271273

274+
// Tools get tools
275+
// lazy init when first use
276+
func (ctx *HttpContext) Tools() *Tools {
277+
if ctx.tools == nil {
278+
ctx.tools = new(Tools)
279+
}
280+
return ctx.tools
281+
}
282+
272283
// AppSetConfig get appset from config file
273284
// update for issue #16 Config file
274285
func (ctx *HttpContext) ConfigSet() core.ReadonlyMap {

example/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func main() {
9797

9898
func Index(ctx dotweb.Context) error {
9999
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
100-
ctx.WriteString(ctx.Request().RemoteIP())
100+
ctx.Write(200, []byte(ctx.Request().RemoteIP()))
101101
//_, err := ctx.WriteStringC(201, "index => ", ctx.RemoteIP(), "我是首页")
102102
return nil
103103
}
@@ -122,6 +122,18 @@ func IndexReg(ctx dotweb.Context) error {
122122
return ctx.WriteString("welcome to dotweb")
123123
}
124124

125+
func IndexPretty(ctx dotweb.Context) error {
126+
type Result struct {
127+
Code int
128+
Message string
129+
}
130+
result := Result{
131+
Code: 200,
132+
Message: "Test",
133+
}
134+
return ctx.WriteString(ctx.Tools().PrettyJson(result))
135+
}
136+
125137
func ReadPost(ctx dotweb.Context) error {
126138
return ctx.WriteString(ctx.Request().PostBody())
127139
}
@@ -172,5 +184,6 @@ func InitRoute(server *dotweb.HttpServer) {
172184
server.GET("/returnerr", ReturnError)
173185
server.GET("/redirect", Redirect)
174186
server.POST("/readpost", ReadPost)
187+
server.GET("/pretty", IndexPretty)
175188
//server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
176189
}

tools.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dotweb
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type Tools struct {
9+
}
10+
11+
func (t *Tools) PrettyJson(data interface{}) string {
12+
by, err := json.MarshalIndent(data, "", "\t")
13+
if err != nil {
14+
return fmt.Sprint(data)
15+
}
16+
return string(by)
17+
}

tools_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package dotweb

version.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## dotweb版本记录:
22

3+
4+
#### Version 1.7.11
5+
* Feature: add Tools include some useful functions
6+
* Feature: add Tools.PrettyJson used to pretty json format view in text
7+
* Detail: use ctx.Tools() to use Tools
8+
* 2020-05-10 15:00 at ShangHai
9+
310
#### Version 1.7.10
411
* Feature: add Request.ExistsQueryKey used to check is exists from query params with the given key.
512
* Opt: optimize file layout, remove module.go

0 commit comments

Comments
 (0)