diff --git a/manifest.json b/manifest.json index 0e629ce..af492f4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,4 @@ { - "GET": { "info": "redirects to given tiny url", "scope": [[]], @@ -36,6 +35,22 @@ "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" } }, "out": {} + }, + + "/": { + "auth": { + "POST": { + "info": "returns a 1-minute access token", + "scope": [[]], + "in": { + "username": { "info": "user name", "type": "varchar(3,20)" }, + "password": { "info": "password", "type": "varchar(5,150)" } + }, + "out": { + "token": { "info": "access token", "type": "varchar(256,256)" } + } + } + } } } \ No newline at end of file diff --git a/root/authi.go b/root/authi.go new file mode 100644 index 0000000..8fd855e --- /dev/null +++ b/root/authi.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + e "git.xdrm.io/go/xb-api/err" + i "git.xdrm.io/go/xb-api/implement" +) + +// Builds an access token from credentials +func Post(d i.Arguments, r *i.Response) i.Response { + + if d.Has("_AUTHORIZATION_") { + fmt.Printf("authorization: '%s'\n", d["_AUTHORIZATION_"].(string)) + } + + r.Err = e.Success + return *r +} diff --git a/root/db/db.go b/root/db/db.go new file mode 100644 index 0000000..d8fa576 --- /dev/null +++ b/root/db/db.go @@ -0,0 +1,75 @@ +package db + +import ( + "fmt" + "github.com/go-redis/redis" +) + +const NONCE = "go-tiny-url" + +var domain = "data" + +type db redis.Client + +// Returns a connected client to dataset +func Connect() *db { + cli := redis.NewClient(&redis.Options{ + Addr: "127.0.0.1:6379", + Password: "", + DB: 0, + }) + + if _, err := cli.Ping().Result(); err != nil { + return nil + } + + return (*db)(cli) +} + +// returns value from key (nil if nothing) +func (c *db) Get(key string) []byte { + + // 1. Try to get + if val, err := (*redis.Client)(c).Get(fmt.Sprintf("%s:%s:%s", NONCE, domain, key)).Result(); err != nil { + // 2. nil if nothing found + return nil + } else { + // 3. return if found + return []byte(val) + } + +} + +// stores a value for a key (success state in return) +func (c *db) Set(key string, value string) bool { + + // 1. Try to set + if (*redis.Client)(c).Set(fmt.Sprintf("%s:%s:%s", NONCE, domain, key), value, 0).Err() != nil { + // 2. failure + return false + } + + // 3. success + return true + +} + +// deletes the value for a key (success state in return) +func (c *db) Del(key string) bool { + + // 1. Try to set + if (*redis.Client)(c).Del(fmt.Sprintf("%s:%s:%s", NONCE, domain, key)).Err() != nil { + // 2. failure + return false + } + + // 3. success + return true + +} + +// Switch following operations to DATA dataset +func (c *db) SwitchData() { domain = "data" } + +// Switch following operations to AUTH dataset +func (c *db) SwitchAuth() { domain = "auth" } diff --git a/root/i.go b/root/i.go index 90d8a73..1eb7130 100644 --- a/root/i.go +++ b/root/i.go @@ -1,78 +1,16 @@ package main import ( - "fmt" + db "./db" e "git.xdrm.io/go/xb-api/err" i "git.xdrm.io/go/xb-api/implement" - "github.com/go-redis/redis" ) -const NONCE = "go-tiny-url" - -type db redis.Client - -// Returns a connected client to dataset -func connect() *db { - cli := redis.NewClient(&redis.Options{ - Addr: "127.0.0.1:6379", - Password: "", - DB: 0, - }) - - if _, err := cli.Ping().Result(); err != nil { - return nil - } - - return (*db)(cli) -} - -// returns value from key (nil if nothing) -func (c *db) get(key string) []byte { - - // 1. Try to get - if val, err := c.Get(fmt.Sprintf("%s:%s", NONCE, key)).Result(); err != nil { - // 2. nil if nothing found - return nil - } else { - // 3. return if found - return []byte(val) - } - -} - -// stores a value for a key (success state in return) -func (c *db) set(key string, value string) bool { - - // 1. Try to set - if c.Set(fmt.Sprintf("%s:%s", NONCE, key), value, 0).Err() != nil { - // 2. failure - return false - } - - // 3. success - return true - -} - -// deletes the value for a key (success state in return) -func (c *db) del(key string) bool { - - // 1. Try to set - if c.Del(fmt.Sprintf("%s:%s", NONCE, key)).Err() != nil { - // 2. failure - return false - } - - // 3. success - return true - -} - // Redirects to an url from a key func Get(d i.Arguments, r *i.Response) i.Response { /* (1) Init redis connection */ - cli := connect() + cli := db.Connect() if cli == nil { r.Err = e.Failure return *r @@ -88,7 +26,7 @@ func Get(d i.Arguments, r *i.Response) i.Response { } /* (3) Check if match for this key */ - val := cli.get(key) + val := cli.Get(key) if val == nil { r.Err = e.NoMatchFound return *r @@ -103,7 +41,7 @@ func Get(d i.Arguments, r *i.Response) i.Response { // Stores a new tinyurl/fullurl combination func Post(d i.Arguments, r *i.Response) i.Response { /* (1) Init redis connection */ - cli := connect() + cli := db.Connect() if cli == nil { r.Err = e.Failure return *r @@ -119,13 +57,13 @@ func Post(d i.Arguments, r *i.Response) i.Response { } /* (3) Check if key already used */ - if cli.get(url) != nil { + if cli.Get(url) != nil { r.Err = e.AlreadyExists return *r } /* (4) Store */ - if !cli.set(url, target) { + if !cli.Set(url, target) { r.Err = e.Failure return *r } @@ -138,7 +76,7 @@ func Post(d i.Arguments, r *i.Response) i.Response { func Put(d i.Arguments, r *i.Response) i.Response { /* (1) Init redis connection */ - cli := connect() + cli := db.Connect() if cli == nil { r.Err = e.Failure return *r @@ -154,13 +92,13 @@ func Put(d i.Arguments, r *i.Response) i.Response { } /* (3) Check if key already used */ - if cli.get(url) == nil { + if cli.Get(url) == nil { r.Err = e.NoMatchFound return *r } /* (4) Update */ - if !cli.set(url, target) { + if !cli.Set(url, target) { r.Err = e.Failure return *r } @@ -173,7 +111,7 @@ func Put(d i.Arguments, r *i.Response) i.Response { func Delete(d i.Arguments, r *i.Response) i.Response { /* (1) Init redis connection */ - cli := connect() + cli := db.Connect() if cli == nil { r.Err = e.Failure return *r @@ -188,13 +126,13 @@ func Delete(d i.Arguments, r *i.Response) i.Response { } /* (3) Check if key already used */ - if cli.get(url) == nil { + if cli.Get(url) == nil { r.Err = e.NoMatchFound return *r } /* (4) Delete */ - if !cli.del(url) { + if !cli.Del(url) { r.Err = e.Failure return *r }