diff --git a/context.go b/context.go index e3af99e..4aa5e43 100644 --- a/context.go +++ b/context.go @@ -15,6 +15,7 @@ import ( "os" "path/filepath" "reflect" + "strings" ) type Handler interface { @@ -57,6 +58,10 @@ func (ctx *Context) Req() *http.Request { return ctx.req } +func (ctx *Context) IsAjax() bool { + return ctx.Req().Header.Get("X-Requested-With") == "XMLHttpRequest" +} + func (ctx *Context) SecureCookies(secret string) Cookies { return &secureCookies{ (*cookies)(ctx), @@ -82,6 +87,23 @@ func (ctx *Context) Params() *Params { return &ctx.params } +func (ctx *Context) IP() string { + proxy := []string{} + if ips := ctx.Req().Header.Get("X-Forwarded-For"); ips != "" { + proxy = strings.Split(ips, ",") + } + if len(proxy) > 0 && proxy[0] != "" { + return proxy[0] + } + ip := strings.Split(ctx.Req().RemoteAddr, ":") + if len(ip) > 0 { + if ip[0] != "[" { + return ip[0] + } + } + return "127.0.0.1" +} + func (ctx *Context) Action() interface{} { ctx.newAction() return ctx.action diff --git a/cookie.go b/cookie.go index 3cead40..df8ea38 100644 --- a/cookie.go +++ b/cookie.go @@ -10,12 +10,12 @@ import ( "crypto/sha1" "encoding/base64" "fmt" + "html/template" "io/ioutil" "net/http" "strconv" "strings" "time" - "html/template" ) func isValidCookieValue(p []byte) bool { diff --git a/cookie_test.go b/cookie_test.go index 517d514..1e1c87d 100644 --- a/cookie_test.go +++ b/cookie_test.go @@ -1670,4 +1670,4 @@ func TestCookie60(t *testing.T) { expect(t, recorder.Code, http.StatusOK) refute(t, len(buff.String()), 0) expect(t, buff.String(), "1") -} \ No newline at end of file +} diff --git a/form.go b/form.go index ef41b17..bd41744 100644 --- a/form.go +++ b/form.go @@ -2,9 +2,9 @@ package tango import ( "errors" + "html/template" "net/http" "strconv" - "html/template" ) type Forms http.Request @@ -226,4 +226,4 @@ func (ctx *Context) FormFloat64(key string, defaults ...float64) float64 { func (ctx *Context) FormBool(key string, defaults ...bool) bool { return (*Forms)(ctx.req).MustBool(key, defaults...) -} \ No newline at end of file +} diff --git a/form_test.go b/form_test.go index 21e12fc..ac93377 100644 --- a/form_test.go +++ b/form_test.go @@ -846,4 +846,4 @@ func TestForm30(t *testing.T) { expect(t, recorder.Code, http.StatusOK) refute(t, len(buff.String()), 0) expect(t, buff.String(), "1") -} \ No newline at end of file +} diff --git a/group.go b/group.go index 5e8766d..93737c0 100644 --- a/group.go +++ b/group.go @@ -106,4 +106,4 @@ func (t *Tango) addGroup(p string, g *Group) { func (t *Tango) Group(p string, o interface{}) { t.addGroup(p, getGroup(o)) -} \ No newline at end of file +} diff --git a/param.go b/param.go index 7ded00e..9436240 100644 --- a/param.go +++ b/param.go @@ -6,8 +6,8 @@ package tango import ( "errors" - "strconv" "html/template" + "strconv" ) type ( @@ -25,7 +25,7 @@ func (p *Params) Get(key string) string { return "" } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } for _, v := range *p { @@ -41,7 +41,7 @@ func (p *Params) String(key string) (string, error) { return "", errors.New("not exist") } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } for _, v := range *p { @@ -57,7 +57,7 @@ func (p *Params) Strings(key string) ([]string, error) { return nil, errors.New("not exist") } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } var s = make([]string, 0) @@ -77,7 +77,7 @@ func (p *Params) Escape(key string) (string, error) { return "", errors.New("not exist") } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } for _, v := range *p { @@ -133,7 +133,7 @@ func (p *Params) MustString(key string, defaults ...string) string { return "" } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } for _, v := range *p { @@ -152,7 +152,7 @@ func (p *Params) MustStrings(key string, defaults ...[]string) []string { return []string{} } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } var s = make([]string, 0) @@ -175,7 +175,7 @@ func (p *Params) MustEscape(key string, defaults ...string) string { return "" } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } for _, v := range *p { @@ -316,7 +316,7 @@ func (p *Params) Set(key, value string) { return } if key[0] != ':' && key[0] != '*' { - key = ":"+ key + key = ":" + key } for i, v := range *p { diff --git a/param_test.go b/param_test.go index 9907bd6..67c0f78 100644 --- a/param_test.go +++ b/param_test.go @@ -1005,4 +1005,4 @@ func TestParams36(t *testing.T) { expect(t, recorder.Code, http.StatusOK) refute(t, len(buff.String()), 0) expect(t, buff.String(), "1") -} \ No newline at end of file +} diff --git a/pool.go b/pool.go index b9ca380..3d9beaf 100644 --- a/pool.go +++ b/pool.go @@ -39,4 +39,4 @@ func (p *pool) New() reflect.Value { p.cur++ p.lock.Unlock() return res -} \ No newline at end of file +} diff --git a/return.go b/return.go index 0f9f58d..782acde 100644 --- a/return.go +++ b/return.go @@ -12,7 +12,7 @@ import ( ) type StatusResult struct { - Code int + Code int Result interface{} } diff --git a/return_test.go b/return_test.go index ffd65e5..11a4d40 100644 --- a/return_test.go +++ b/return_test.go @@ -218,16 +218,16 @@ func TestReturnJson3(t *testing.T) { } type JsonReturn3 struct { - Json + Json } func (JsonReturn3) Get() (int, interface{}) { - if true { - return 201, map[string]string{ - "say": "Hello tango!", - } - } - return 500, errors.New("something error") + if true { + return 201, map[string]string{ + "say": "Hello tango!", + } + } + return 500, errors.New("something error") } func TestReturnJson4(t *testing.T) {