tiny-url-ex/controller.plugin/ROOT/main.go

180 lines
3.0 KiB
Go
Raw Normal View History

package main
import (
"git.xdrm.io/example/aicra/db"
"git.xdrm.io/go/aicra/api"
2018-10-01 13:15:02 +00:00
"git.xdrm.io/go/aicra/driver"
2018-07-05 17:19:39 +00:00
e "git.xdrm.io/go/aicra/err"
)
2018-10-01 13:15:02 +00:00
func main() {}
type RootController int
func Export() driver.Controller { return new(RootController) }
2018-06-16 16:04:28 +00:00
// Redirects to an url from a key
func (rctl RootController) Get(d api.Arguments) api.Response {
r := api.NewResponse()
2018-06-16 16:04:28 +00:00
/* (1) Init redis connection */
cli := db.Connect()
if cli == nil {
2018-06-16 16:04:28 +00:00
r.Err = e.Failure
return *r
}
/* (2) Extract api input */
key, err := d.GetString("url")
2018-06-16 16:04:28 +00:00
if err != nil {
2018-06-16 16:04:28 +00:00
r.Err = e.InvalidParam
2018-10-01 19:21:33 +00:00
r.Err.Put("url")
r.Err.Put(err.Error())
2018-06-16 16:04:28 +00:00
return *r
}
/* (3) Check if match for this key */
val := cli.Get(db.DATA, key)
if val == nil {
2018-06-16 16:04:28 +00:00
r.Err = e.NoMatchFound
return *r
}
/* (4) Redirect to value */
r.Set("_REDIRECT_", string(val))
2018-06-16 16:04:28 +00:00
r.Err = e.Success
return *r
}
// Stores a new tinyurl/fullurl combination
func (rctl RootController) Post(d api.Arguments) api.Response {
r := api.NewResponse()
2018-06-16 16:04:28 +00:00
/* (1) Init redis connection */
cli := db.Connect()
if cli == nil {
2018-06-16 16:04:28 +00:00
r.Err = e.Failure
return *r
}
/* (2) Extract api input */
target, err := d.GetString("target")
if err != nil {
r.Err = e.InvalidParam
r.Err.Put("target")
r.Err.Put(err.Error())
return *r
}
2018-06-16 16:04:28 +00:00
url, err := d.GetString("url")
if err != nil {
2018-06-16 16:04:28 +00:00
r.Err = e.InvalidParam
r.Err.Put("url")
r.Err.Put(err.Error())
2018-06-16 16:04:28 +00:00
return *r
}
/* (3) Check if key already used */
if cli.Get(db.DATA, url) != nil {
2018-06-16 16:04:28 +00:00
r.Err = e.AlreadyExists
r.Err.Put("url")
2018-06-16 16:04:28 +00:00
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 api.Arguments) api.Response {
r := api.NewResponse()
/* (1) Init redis connection */
cli := db.Connect()
if cli == nil {
r.Err = e.Failure
return *r
}
/* (2) Extract api input */
target, err := d.GetString("target")
if err != nil {
r.Err = e.InvalidParam
r.Err.Put("target")
r.Err.Put(err.Error())
return *r
}
url, err := d.GetString("url")
if err != nil {
r.Err = e.InvalidParam
r.Err.Put("url")
r.Err.Put(err.Error())
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 api.Arguments) api.Response {
r := api.NewResponse()
/* (1) Init redis connection */
cli := db.Connect()
if cli == nil {
r.Err = e.Failure
return *r
}
/* (2) Extract api input */
url, err := d.GetString("url")
if err != nil {
r.Err = e.InvalidParam
r.Err.Put("url")
r.Err.Put(err.Error())
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) {
2018-06-16 16:04:28 +00:00
r.Err = e.Failure
return *r
}
r.Err = e.Success
return *r
}