2018-06-10 20:49:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-08 13:29:25 +00:00
|
|
|
"git.xdrm.io/example/aicra/db"
|
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-09-28 14:06:20 +00:00
|
|
|
"git.xdrm.io/go/aicra/response"
|
2018-07-11 17:08:09 +00:00
|
|
|
i "git.xdrm.io/go/aicra/response"
|
2018-06-10 20:49:32 +00:00
|
|
|
)
|
|
|
|
|
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
|
2018-10-01 13:15:02 +00:00
|
|
|
func (rctl RootController) Get(d i.Arguments) i.Response {
|
2018-09-28 14:06:20 +00:00
|
|
|
|
|
|
|
r := response.New()
|
2018-06-16 16:04:28 +00:00
|
|
|
|
|
|
|
/* (1) Init redis connection */
|
2018-06-17 19:24:47 +00:00
|
|
|
cli := db.Connect()
|
2018-06-16 20:08:14 +00:00
|
|
|
if cli == nil {
|
2018-06-16 16:04:28 +00:00
|
|
|
r.Err = e.Failure
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Extract api input */
|
|
|
|
key, ok := d["url"].(string)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
r.Err = e.InvalidParam
|
2018-10-01 19:21:33 +00:00
|
|
|
r.Err.Put("url")
|
2018-06-16 16:04:28 +00:00
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Check if match for this key */
|
2018-07-08 13:29:25 +00:00
|
|
|
val := cli.Get(db.DATA, key)
|
2018-06-16 20:08:14 +00:00
|
|
|
if val == nil {
|
2018-06-16 16:04:28 +00:00
|
|
|
r.Err = e.NoMatchFound
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Redirect to value */
|
2018-06-16 20:08:14 +00:00
|
|
|
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
|
2018-10-01 13:15:02 +00:00
|
|
|
func (rctl RootController) Post(d i.Arguments) i.Response {
|
2018-09-28 14:06:20 +00:00
|
|
|
|
|
|
|
r := response.New()
|
2018-06-16 16:04:28 +00:00
|
|
|
/* (1) Init redis connection */
|
2018-06-17 19:24:47 +00:00
|
|
|
cli := db.Connect()
|
2018-06-16 20:08:14 +00:00
|
|
|
if cli == nil {
|
2018-06-16 16:04:28 +00:00
|
|
|
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 */
|
2018-07-08 13:29:25 +00:00
|
|
|
if cli.Get(db.DATA, url) != nil {
|
2018-06-16 16:04:28 +00:00
|
|
|
r.Err = e.AlreadyExists
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Store */
|
2018-07-08 13:29:25 +00:00
|
|
|
if !cli.Set(db.DATA, url, target) {
|
2018-06-16 20:08:14 +00:00
|
|
|
r.Err = e.Failure
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Err = e.Success
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides a existing tinyurl with new target
|
2018-10-01 13:15:02 +00:00
|
|
|
func (rctl RootController) Put(d i.Arguments) i.Response {
|
2018-09-28 14:06:20 +00:00
|
|
|
|
|
|
|
r := response.New()
|
2018-06-16 20:08:14 +00:00
|
|
|
|
|
|
|
/* (1) Init redis connection */
|
2018-06-17 19:24:47 +00:00
|
|
|
cli := db.Connect()
|
2018-06-16 20:08:14 +00:00
|
|
|
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 */
|
2018-07-08 13:29:25 +00:00
|
|
|
if cli.Get(db.DATA, url) == nil {
|
2018-06-16 20:08:14 +00:00
|
|
|
r.Err = e.NoMatchFound
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Update */
|
2018-07-08 13:29:25 +00:00
|
|
|
if !cli.Set(db.DATA, url, target) {
|
2018-06-16 20:08:14 +00:00
|
|
|
r.Err = e.Failure
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Err = e.Success
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes an existing tinyurl
|
2018-10-01 13:15:02 +00:00
|
|
|
func (rctl RootController) Delete(d i.Arguments) i.Response {
|
2018-09-28 14:06:20 +00:00
|
|
|
|
|
|
|
r := response.New()
|
2018-06-16 20:08:14 +00:00
|
|
|
|
|
|
|
/* (1) Init redis connection */
|
2018-06-17 19:24:47 +00:00
|
|
|
cli := db.Connect()
|
2018-06-16 20:08:14 +00:00
|
|
|
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 */
|
2018-07-08 13:29:25 +00:00
|
|
|
if cli.Get(db.DATA, url) == nil {
|
2018-06-16 20:08:14 +00:00
|
|
|
r.Err = e.NoMatchFound
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Delete */
|
2018-07-08 13:29:25 +00:00
|
|
|
if !cli.Del(db.DATA, url) {
|
2018-06-16 16:04:28 +00:00
|
|
|
r.Err = e.Failure
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
2018-06-10 20:49:32 +00:00
|
|
|
r.Err = e.Success
|
|
|
|
return *r
|
|
|
|
}
|