package main import ( "git.xdrm.io/example/aicra/db" "git.xdrm.io/go/aicra/driver" e "git.xdrm.io/go/aicra/err" "git.xdrm.io/go/aicra/response" i "git.xdrm.io/go/aicra/response" ) func main() {} type RootController int func Export() driver.Controller { return new(RootController) } // Redirects to an url from a key func (rctl RootController) Get(d i.Arguments) i.Response { r := response.New() /* (1) Init redis connection */ cli := db.Connect() if cli == nil { r.Err = e.Failure return *r } /* (2) Extract api input */ key, ok := d["url"].(string) if !ok { r.Err = e.InvalidParam r.Err.BindArgument("url") return *r } /* (3) Check if match for this key */ val := cli.Get(db.DATA, key) if val == nil { r.Err = e.NoMatchFound return *r } /* (4) Redirect to value */ r.Set("_REDIRECT_", string(val)) r.Err = e.Success return *r } // Stores a new tinyurl/fullurl combination func (rctl RootController) Post(d i.Arguments) i.Response { r := response.New() /* (1) Init redis connection */ cli := db.Connect() if cli == nil { r.Err = e.Failure return *r } /* (2) Extract api input */ target, ok1 := d["target"].(string) url, ok2 := d["url"].(string) if !ok1 || !ok2 { r.Err = e.InvalidParam return *r } /* (3) Check if key already used */ if cli.Get(db.DATA, url) != nil { r.Err = e.AlreadyExists return *r } /* (4) Store */ if !cli.Set(db.DATA, url, target) { r.Err = e.Failure return *r } r.Err = e.Success return *r } // Overrides a existing tinyurl with new target func (rctl RootController) Put(d i.Arguments) i.Response { r := response.New() /* (1) Init redis connection */ cli := db.Connect() if cli == nil { r.Err = e.Failure return *r } /* (2) Extract api input */ target, ok1 := d["target"].(string) url, ok2 := d["url"].(string) if !ok1 || !ok2 { r.Err = e.InvalidParam return *r } /* (3) Check if key already used */ if cli.Get(db.DATA, url) == nil { r.Err = e.NoMatchFound return *r } /* (4) Update */ if !cli.Set(db.DATA, url, target) { r.Err = e.Failure return *r } r.Err = e.Success return *r } // Deletes an existing tinyurl func (rctl RootController) Delete(d i.Arguments) i.Response { r := response.New() /* (1) Init redis connection */ cli := db.Connect() if cli == nil { r.Err = e.Failure return *r } /* (2) Extract api input */ url, ok := d["url"].(string) if !ok { r.Err = e.InvalidParam return *r } /* (3) Check if key already used */ if cli.Get(db.DATA, url) == nil { r.Err = e.NoMatchFound return *r } /* (4) Delete */ if !cli.Del(db.DATA, url) { r.Err = e.Failure return *r } r.Err = e.Success return *r }