示例#1
0
文件: basic.go 项目: jango2015/wk
// url: /basic/set/32?str=s&uint=64&float=3.14&byte=8
func (c *BasicController) Set(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	var id int

	if i, ok := ctx.RouteData.Int("id"); ok {
		id = i
	} else {
		return wk.Data(0), nil
	}

	var count int = 0
	for i := 0; i < len(c.data); i++ {
		d := c.data[i]

		if d.Int != id {
			continue
		}
		if x := ctx.FormValue("str"); x != "" {
			d.Str = x
		}
		if x, ok := ctx.FormInt("uint"); ok {
			d.Uint = uint64(x)
		}
		if x, ok := ctx.FormFloat("float"); ok {
			d.Float = float32(x)
		}
		if x, ok := ctx.FormInt("byte"); ok {
			d.Byte = byte(x)
		}
		count++
	}

	return wk.Data(count), nil
}
示例#2
0
文件: config.go 项目: jango2015/wk
// url: /config/query
func (c *ConfigController) Query(ctx *wk.HttpContext) (wk.HttpResult, error) {
	// maybe support "a b[@field=xxx] c d " later
	n, ok := c.node().Query("key_config Db_Log Host")
	if ok {
		return wk.Data(n.Literal), nil
	}
	return wk.Data(ok), nil
}
示例#3
0
文件: basic.go 项目: jango2015/wk
// url: /basic/delete/32
func (c *BasicController) Delete(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	l := len(c.data)
	if i, ok := ctx.RouteData.Int("id"); ok {
		c.deleteByInt(i)
	}
	return wk.Data(l - len(c.data)), nil
}
示例#4
0
文件: config.go 项目: jango2015/wk
// url: /config/map
func (c *ConfigController) Map(ctx *wk.HttpContext) (wk.HttpResult, error) {
	data, err := c.node().MustChild("key_map").Map()
	if err != nil {
		return nil, err
	}
	return wk.Data(data), nil
}
示例#5
0
文件: user.go 项目: jango2015/wk
// get: /user/exists/sheldon
func (uc *UserController) Exists(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	valid := true
	if name := ctx.FV("name"); name != "" {
		valid = !userRepo.Exists(name)
	}
	return wk.Data(valid), nil
}
示例#6
0
文件: config.go 项目: jango2015/wk
// url: /config/composite
func (c *ConfigController) Composite(ctx *wk.HttpContext) (wk.HttpResult, error) {
	conf := &Config{}
	err := c.node().MustChild("key_config").Value(conf)
	if err != nil {
		return nil, err
	}
	return wk.Data(conf), nil
}
示例#7
0
文件: config.go 项目: jango2015/wk
// url: /config/bool
func (c *ConfigController) Bool(ctx *wk.HttpContext) (wk.HttpResult, error) {
	//Int, Uint, Float, Bool, String
	b, err := c.node().MustChild("key_bool").Bool()
	if err != nil {
		return nil, err
	}
	return wk.Data(b), nil
}
示例#8
0
文件: basic.go 项目: jango2015/wk
// url: /basic/add/?int=32&str=string&uint=1024&float=1.1&byte=64
func (c *BasicController) Add(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	data := Data{
		Int:   ctx.FormIntOr("int", 0),
		Uint:  uint64(ctx.FormIntOr("uint", 0)),
		Str:   ctx.FormValue("str"),
		Float: float32(ctx.FormFloatOr("float", 0.0)),
		Byte:  byte(ctx.FormIntOr("byte", 0)),
	}
	c.data = append(c.data, data)
	return wk.Data(data.String()), nil
}
示例#9
0
文件: basic.go 项目: jango2015/wk
// url: /basic/rangecount/?start=1&end=99
func (c *BasicController) RangeCount(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	start := ctx.FormIntOr("start", 0)
	end := ctx.FormIntOr("end", 0)
	var count int = 0

	for _, d := range c.data {
		if d.Int >= start && d.Int <= end {
			count++
		}
	}
	return wk.Data(count), nil
}
示例#10
0
文件: basic.go 项目: jango2015/wk
// url: post /basic/post/
func (c *BasicController) Post(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	var body []byte
	if body, err = ctx.ReadBody(); err != nil {
		return nil, err
	}

	data := &Data{}
	err = json.Unmarshal(body, data)
	if err != nil {
		return nil, err
	}
	c.data = append(c.data, *data)
	return wk.Data(true), nil
}
示例#11
0
文件: config.go 项目: jango2015/wk
// url: /config/value
func (c *ConfigController) Value(ctx *wk.HttpContext) (wk.HttpResult, error) {
	v := Driver{
		Driver:   "driver",
		Host:     "host",
		User:     "******",
		Password: "******",
		A:        "aaa",
		B:        "bbb",
	}

	err := c.node().MustChild("key_struct").Value(&v)
	if err != nil {
		return nil, err
	}
	return wk.Data(v), nil
}
示例#12
0
文件: session.go 项目: jango2015/wk
// url: /session/keys
func (c *Session) Keys(ctx *wk.HttpContext) (wk.HttpResult, error) {
	keys, err := ctx.Session.Keys()
	return wk.Data(fmt.Sprintln(keys)), err
}
示例#13
0
文件: session.go 项目: jango2015/wk
// url: /session/abandon
func (c *Session) Abandon(ctx *wk.HttpContext) (wk.HttpResult, error) {
	err := ctx.Session.Abandon()
	return wk.Data(true), err
}
示例#14
0
文件: session.go 项目: jango2015/wk
// url: /session/remove?k=test
func (c *Session) Remove(ctx *wk.HttpContext) (wk.HttpResult, error) {
	err := ctx.Session.Remove(ctx.FV("k"))
	return wk.Data(err == nil), err
}
示例#15
0
文件: session.go 项目: jango2015/wk
// url: /session/get?k=test
func (c *Session) Get(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	v, _, err := ctx.Session.Get(ctx.FV("k"))
	return wk.Data(v), err
}
示例#16
0
文件: session.go 项目: jango2015/wk
// url: /session/add?k=test&v=101
func (c *Session) Add(ctx *wk.HttpContext) (wk.HttpResult, error) {
	ok, err := ctx.Session.Add(ctx.FV("k"), ctx.FV("v"))
	return wk.Data(ok), err
}
示例#17
0
文件: session.go 项目: jango2015/wk
// url: /session/id
func (c *Session) Id(ctx *wk.HttpContext) (wk.HttpResult, error) {
	id := ctx.SessionId()
	return wk.Data(id), nil
}
示例#18
0
文件: config.go 项目: jango2015/wk
// url: /config/refresh
func (c *ConfigController) Refresh(ctx *wk.HttpContext) (wk.HttpResult, error) {
	s := c.node().ChildString("key_string")
	return wk.Data(s), nil
}
示例#19
0
文件: config.go 项目: jango2015/wk
// url: /config/dump
func (c *ConfigController) Dump(ctx *wk.HttpContext) (wk.HttpResult, error) {
	return wk.Data(c.node().MustChild("key_config").Dump()), nil
}
示例#20
0
文件: basic.go 项目: jango2015/wk
// url: /basic/clear/
func (c *BasicController) Clear(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	c.data = make([]Data, 0)
	return wk.Data(len(c.data)), nil
}
示例#21
0
文件: config.go 项目: jango2015/wk
// url: /config/childint
func (c *ConfigController) ChildInt(ctx *wk.HttpContext) (wk.HttpResult, error) {
	//ChildInt, ChildUint, ChildFloat, ChildBool, ChildString
	i := c.node().ChildInt("key_int")
	return wk.Data(i), nil
}
示例#22
0
文件: basic.go 项目: jango2015/wk
// url: /basic/int/32
func (c *BasicController) Int(ctx *wk.HttpContext) (result wk.HttpResult, err error) {
	if id, ok := ctx.RouteData.Int("id"); ok {
		return wk.Json(c.getByInt(id)), nil
	}
	return wk.Data(""), nil
}
示例#23
0
文件: config.go 项目: jango2015/wk
// url: /config/childstringordefault
func (c *ConfigController) ChildStringOrDefault(ctx *wk.HttpContext) (wk.HttpResult, error) {
	//ChildIntOrDefault, ChildUintOrDefault, ChildFloatOrDefault, ChildBoolOrDefault, ChildStringOrDefault
	s := c.node().ChildStringOrDefault("key_string_not", "default value")
	return wk.Data(s), nil
}
示例#24
0
文件: config.go 项目: jango2015/wk
// url: /config/child
func (c *ConfigController) Child(ctx *wk.HttpContext) (wk.HttpResult, error) {
	_, ok := c.node().Child("key_string")
	return wk.Data(ok), nil
}