// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// url: /session/abandon func (c *Session) Abandon(ctx *wk.HttpContext) (wk.HttpResult, error) { err := ctx.Session.Abandon() return wk.Data(true), err }
// 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 }
// 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 }
// 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 }
// url: /session/id func (c *Session) Id(ctx *wk.HttpContext) (wk.HttpResult, error) { id := ctx.SessionId() return wk.Data(id), nil }
// url: /config/refresh func (c *ConfigController) Refresh(ctx *wk.HttpContext) (wk.HttpResult, error) { s := c.node().ChildString("key_string") return wk.Data(s), nil }
// url: /config/dump func (c *ConfigController) Dump(ctx *wk.HttpContext) (wk.HttpResult, error) { return wk.Data(c.node().MustChild("key_config").Dump()), nil }
// 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 }
// 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 }
// 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 }
// 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 }
// url: /config/child func (c *ConfigController) Child(ctx *wk.HttpContext) (wk.HttpResult, error) { _, ok := c.node().Child("key_string") return wk.Data(ok), nil }