added IsAjax and RemoteAddr for context, gofmt #15
22
context.go
22
context.go
|
@ -15,6 +15,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
|
@ -57,6 +58,10 @@ func (ctx *Context) Req() *http.Request {
|
||||||
return ctx.req
|
return ctx.req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *Context) IsAjax() bool {
|
||||||
|
return ctx.Req().Header.Get("X-Requested-With") == "XMLHttpRequest"
|
||||||
|
}
|
||||||
|
|
||||||
func (ctx *Context) SecureCookies(secret string) Cookies {
|
func (ctx *Context) SecureCookies(secret string) Cookies {
|
||||||
return &secureCookies{
|
return &secureCookies{
|
||||||
(*cookies)(ctx),
|
(*cookies)(ctx),
|
||||||
|
@ -82,6 +87,23 @@ func (ctx *Context) Params() *Params {
|
||||||
return &ctx.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{} {
|
func (ctx *Context) Action() interface{} {
|
||||||
ctx.newAction()
|
ctx.newAction()
|
||||||
return ctx.action
|
return ctx.action
|
||||||
|
|
|
@ -10,12 +10,12 @@ import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"html/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isValidCookieValue(p []byte) bool {
|
func isValidCookieValue(p []byte) bool {
|
||||||
|
|
|
@ -1670,4 +1670,4 @@ func TestCookie60(t *testing.T) {
|
||||||
expect(t, recorder.Code, http.StatusOK)
|
expect(t, recorder.Code, http.StatusOK)
|
||||||
refute(t, len(buff.String()), 0)
|
refute(t, len(buff.String()), 0)
|
||||||
expect(t, buff.String(), "1")
|
expect(t, buff.String(), "1")
|
||||||
}
|
}
|
||||||
|
|
4
form.go
4
form.go
|
@ -2,9 +2,9 @@ package tango
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"html/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Forms http.Request
|
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 {
|
func (ctx *Context) FormBool(key string, defaults ...bool) bool {
|
||||||
return (*Forms)(ctx.req).MustBool(key, defaults...)
|
return (*Forms)(ctx.req).MustBool(key, defaults...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -846,4 +846,4 @@ func TestForm30(t *testing.T) {
|
||||||
expect(t, recorder.Code, http.StatusOK)
|
expect(t, recorder.Code, http.StatusOK)
|
||||||
refute(t, len(buff.String()), 0)
|
refute(t, len(buff.String()), 0)
|
||||||
expect(t, buff.String(), "1")
|
expect(t, buff.String(), "1")
|
||||||
}
|
}
|
||||||
|
|
2
group.go
2
group.go
|
@ -106,4 +106,4 @@ func (t *Tango) addGroup(p string, g *Group) {
|
||||||
|
|
||||||
func (t *Tango) Group(p string, o interface{}) {
|
func (t *Tango) Group(p string, o interface{}) {
|
||||||
t.addGroup(p, getGroup(o))
|
t.addGroup(p, getGroup(o))
|
||||||
}
|
}
|
||||||
|
|
18
param.go
18
param.go
|
@ -6,8 +6,8 @@ package tango
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -25,7 +25,7 @@ func (p *Params) Get(key string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range *p {
|
for _, v := range *p {
|
||||||
|
@ -41,7 +41,7 @@ func (p *Params) String(key string) (string, error) {
|
||||||
return "", errors.New("not exist")
|
return "", errors.New("not exist")
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range *p {
|
for _, v := range *p {
|
||||||
|
@ -57,7 +57,7 @@ func (p *Params) Strings(key string) ([]string, error) {
|
||||||
return nil, errors.New("not exist")
|
return nil, errors.New("not exist")
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
var s = make([]string, 0)
|
var s = make([]string, 0)
|
||||||
|
@ -77,7 +77,7 @@ func (p *Params) Escape(key string) (string, error) {
|
||||||
return "", errors.New("not exist")
|
return "", errors.New("not exist")
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range *p {
|
for _, v := range *p {
|
||||||
|
@ -133,7 +133,7 @@ func (p *Params) MustString(key string, defaults ...string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range *p {
|
for _, v := range *p {
|
||||||
|
@ -152,7 +152,7 @@ func (p *Params) MustStrings(key string, defaults ...[]string) []string {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
var s = make([]string, 0)
|
var s = make([]string, 0)
|
||||||
|
@ -175,7 +175,7 @@ func (p *Params) MustEscape(key string, defaults ...string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range *p {
|
for _, v := range *p {
|
||||||
|
@ -316,7 +316,7 @@ func (p *Params) Set(key, value string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if key[0] != ':' && key[0] != '*' {
|
if key[0] != ':' && key[0] != '*' {
|
||||||
key = ":"+ key
|
key = ":" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, v := range *p {
|
for i, v := range *p {
|
||||||
|
|
|
@ -1005,4 +1005,4 @@ func TestParams36(t *testing.T) {
|
||||||
expect(t, recorder.Code, http.StatusOK)
|
expect(t, recorder.Code, http.StatusOK)
|
||||||
refute(t, len(buff.String()), 0)
|
refute(t, len(buff.String()), 0)
|
||||||
expect(t, buff.String(), "1")
|
expect(t, buff.String(), "1")
|
||||||
}
|
}
|
||||||
|
|
2
pool.go
2
pool.go
|
@ -39,4 +39,4 @@ func (p *pool) New() reflect.Value {
|
||||||
p.cur++
|
p.cur++
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type StatusResult struct {
|
type StatusResult struct {
|
||||||
Code int
|
Code int
|
||||||
Result interface{}
|
Result interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -218,16 +218,16 @@ func TestReturnJson3(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonReturn3 struct {
|
type JsonReturn3 struct {
|
||||||
Json
|
Json
|
||||||
}
|
}
|
||||||
|
|
||||||
func (JsonReturn3) Get() (int, interface{}) {
|
func (JsonReturn3) Get() (int, interface{}) {
|
||||||
if true {
|
if true {
|
||||||
return 201, map[string]string{
|
return 201, map[string]string{
|
||||||
"say": "Hello tango!",
|
"say": "Hello tango!",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 500, errors.New("something error")
|
return 500, errors.New("something error")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReturnJson4(t *testing.T) {
|
func TestReturnJson4(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user