diff --git a/manifest.json b/manifest.json index dc49e76..7e1f65e 100644 --- a/manifest.json +++ b/manifest.json @@ -1,115 +1,22 @@ { "GET": { - "info": "returns api information", + "info": "redirects to given tiny url", "scope": [[]], - "in": {}, - "out": { - "version": { "info": "current api version", "type": "varchar(1,8)" }, - "info": { "info": "api information details", "type": "" } - } + "in": { + "URL#0": { "info": "tiny url to redirect to", "name": "url", "type": "varchar(3,30)" } + }, + "out": {} }, - "/": { - - "user": { - - "GET": { - "info": "gets all | a specific user by id", - "scope": [[]], - "in": { - "URL#0": { "info": "optional user id | all users are returned if missing", "name": "uid", "type": "?int", "default": null } - }, - "out": { - "users": { "info": "matching user(s) data", "type": "" } - } - }, - - "POST": { - "info": "creates a new user", - "scope": [["admin"]], - "in": { - "firstname": { "info": "new first name", "type": "varchar(3,20)" }, - "lastname": { "info": "new last name", "type": "varchar(3,20)" }, - "mail": { "info": "new mail", "type": "string" } - }, - "out": { - "user": { "info": "user data to acknowledge creation", "type": "" } - } - }, - - "PUT": { - "info": "updates data of an existing user; only the data given will be updated, all fields are optional", - "scope": [["admin"], ["user"]], - "in": { - "URL#0": { "info": "id of the user to update", "type": "int", "name": "user_id" }, - "firstname": { "info": "new first name", "type": "?varchar(3,20)", "default": null }, - "lastname": { "info": "new last name", "type": "?varchar(3,20)", "default": null }, - "mail": { "info": "new mail", "type": "?string", "default": null } - }, - "out": { - "user": { "info": "new updated data to acknowledge updates", "type": "" } - } - }, - - "DELETE": { - "info": "deletes an existing user", - "scope": [["admin"], ["user"]], - "in": { - "URL#0": { "info": "id of the user to delete", "type": "int" } - }, - "out": {} - }, - - "/": { - "post": { - "GET": { - "info": "gets all | a specific post for an existing user", - "scope": [["user"], []], - "in": { - "URL#0": { "info": "optional post id", "type": "int", "name": "?post_id", "default": null }, - "URL#1": { "info": "user id (if missing, current connected user)", "type": "int", "name": "?user_id", "default": null } - }, - "out": { - "posts": { "info": "matching post(s) data", "type": "" } - } - }, - - "POST": { - "info": "creates a new post for the current connected user", - "scope": [["user"]], - "in": { - "title": { "info": "new post title", "type": "varchar(3,20)" }, - "content": { "info": "new post content", "type": "string" } - }, - "out": { - "post": { "info": "post data to acknowledge creation", "type": "" } - } - }, - - "PUT": { - "info": "updates data of an existing post of the current connected user; only the data given will be updated, all fields are optional", - "scope": [["user"]], - "in": { - "title": { "info": "new post title", "type": "?varchar(3,20)", "default": null }, - "content": { "info": "new post content", "type": "?string", "default": null } - }, - "out": { - "post": { "info": "new updated data to acknowledge updates", "type": "" } - } - }, - - "DELETE": { - "info": "deletes an existing post of the current connected user", - "scope": [["user"]], - "in": { - "URL#0": { "info": "id of the post to delete", "type": "int", "name": "post_id" } - }, - "out": {} - } - } - } - } - + "POST": { + "info": "creates a new tiny url", + "scope": [[]], + "in": { + "url": { "info": "preferred tiny url", "type": "varchar(3,30)" }, + "target": { "info": "url to shorten", "type": "varchar(5,300)" } + }, + "out": {} } + } \ No newline at end of file diff --git a/root/i.go b/root/i.go index 6f17c5d..af4f1a4 100644 --- a/root/i.go +++ b/root/i.go @@ -1,15 +1,88 @@ package main import ( - "fmt" e "git.xdrm.io/go/xb-api/err" i "git.xdrm.io/go/xb-api/implement" + r "github.com/go-redis/redis" ) +// Initiates connection to redis dataset +func redisConnect() (*r.Client, error) { + cli := r.NewClient(&r.Options{ + Addr: "127.0.0.1:6379", + Password: "", + DB: 0, + }) + + _, err := cli.Ping().Result() + + return cli, err +} + +// Redirects to an url from a key func Get(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("GET /\n") - r.Set("api_version", 1.2) - r.Set("received_data", d) + + /* (1) Init redis connection */ + cli, err := redisConnect() + if err != 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, err := cli.Get(key).Result() + if err != nil { + r.Err = e.NoMatchFound + return *r + } + + /* (4) Redirect to value */ + r.Set("_REDIRECT_", val) + r.Err = e.Success + return *r +} + +// Stores a new tinyurl/fullurl combination +func Post(d i.Arguments, r *i.Response) i.Response { + /* (1) Init redis connection */ + cli, err := redisConnect() + if err != 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 */ + _, err = cli.Get(url).Result() + if err == nil { + r.Err = e.AlreadyExists + return *r + } + + /* (4) Store */ + err = cli.Set(url, target, 0).Err() + if err != nil { + r.Err = e.Failure + return *r + } + r.Err = e.Success return *r } diff --git a/root/user/posti.go b/root/user/posti.go deleted file mode 100644 index be9de83..0000000 --- a/root/user/posti.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "fmt" - e "git.xdrm.io/go/xb-api/err" - i "git.xdrm.io/go/xb-api/implement" -) - -func Get(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("GET /user/post\n") - r.Set("method_test", "Get") - r.Err = e.Upload - return *r -} -func Post(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("POST /user/post\n") - r.Set("method_test", "Post") - r.Err = e.Download - return *r -} -func Put(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("PUT /user/post\n") - r.Set("method_test", "Put") - r.Err = e.MissingDownloadHeaders - return *r -} -func Delete(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("DELETE /user/post\n") - r.Set("method_test", "Delete") - r.Err = e.MissingDownloadBody - return *r -} diff --git a/root/useri.go b/root/useri.go deleted file mode 100644 index debb121..0000000 --- a/root/useri.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "fmt" - e "git.xdrm.io/go/xb-api/err" - i "git.xdrm.io/go/xb-api/implement" -) - -func Get(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("GET /user\n") - r.Set("method_test", "Get") - r.Set("received_data", d) - r.Err = e.Upload - return *r -} -func Post(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("POST /user\n") - r.Set("method_test", "Post") - r.Set("received_data", d) - r.Err = e.Download - return *r -} -func Put(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("PUT /user\n") - r.Set("method_test", "Put") - r.Set("received_data", d) - r.Err = e.MissingDownloadHeaders - return *r -} -func Delete(d i.Arguments, r *i.Response) i.Response { - fmt.Printf("DELETE /user\n") - r.Set("method_test", "Delete") - r.Set("received_data", d) - r.Err = e.MissingDownloadBody - return *r -}