From 15eb1d5e81acc81d80f1db8b27cbc3a4e6e87bbb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 16 Jun 2018 18:04:28 +0200 Subject: [PATCH 1/8] tiny url working example --- manifest.json | 121 ++++++--------------------------------------- root/i.go | 81 ++++++++++++++++++++++++++++-- root/user/posti.go | 32 ------------ root/useri.go | 36 -------------- 4 files changed, 91 insertions(+), 179 deletions(-) delete mode 100644 root/user/posti.go delete mode 100644 root/useri.go 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 -} From 3dfdece7e5523f4bf875a5060dcdbfd0387557ca Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 16 Jun 2018 22:08:14 +0200 Subject: [PATCH 2/8] full CRUD resource (put for overriding, delete for ...) --- manifest.json | 23 +++++++- root/i.go | 150 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 154 insertions(+), 19 deletions(-) diff --git a/manifest.json b/manifest.json index 7e1f65e..0e629ce 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "info": "redirects to given tiny url", "scope": [[]], "in": { - "URL#0": { "info": "tiny url to redirect to", "name": "url", "type": "varchar(3,30)" } + "URL#0": { "info": "tiny url to redirect to", "name": "url", "type": "varchar(1,30)" } }, "out": {} }, @@ -13,10 +13,29 @@ "info": "creates a new tiny url", "scope": [[]], "in": { - "url": { "info": "preferred tiny url", "type": "varchar(3,30)" }, + "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" }, "target": { "info": "url to shorten", "type": "varchar(5,300)" } }, "out": {} + }, + + "PUT": { + "info": "overrides an existing tiny url", + "scope": [[]], + "in": { + "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" }, + "target": { "info": "url to shorten", "type": "varchar(5,300)" } + }, + "out": {} + }, + + "DELETE": { + "info": "removes an existing tiny url", + "scope": [[]], + "in": { + "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" } + }, + "out": {} } } \ No newline at end of file diff --git a/root/i.go b/root/i.go index af4f1a4..90d8a73 100644 --- a/root/i.go +++ b/root/i.go @@ -1,30 +1,79 @@ 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" + "github.com/go-redis/redis" ) -// Initiates connection to redis dataset -func redisConnect() (*r.Client, error) { - cli := r.NewClient(&r.Options{ +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, }) - _, err := cli.Ping().Result() + 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 - return cli, err } // Redirects to an url from a key func Get(d i.Arguments, r *i.Response) i.Response { /* (1) Init redis connection */ - cli, err := redisConnect() - if err != nil { + cli := connect() + if cli == nil { r.Err = e.Failure return *r } @@ -39,14 +88,14 @@ func Get(d i.Arguments, r *i.Response) i.Response { } /* (3) Check if match for this key */ - val, err := cli.Get(key).Result() - if err != nil { + val := cli.get(key) + if val == nil { r.Err = e.NoMatchFound return *r } /* (4) Redirect to value */ - r.Set("_REDIRECT_", val) + r.Set("_REDIRECT_", string(val)) r.Err = e.Success return *r } @@ -54,8 +103,8 @@ 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, err := redisConnect() - if err != nil { + cli := connect() + if cli == nil { r.Err = e.Failure return *r } @@ -70,15 +119,82 @@ func Post(d i.Arguments, r *i.Response) i.Response { } /* (3) Check if key already used */ - _, err = cli.Get(url).Result() - if err == nil { + if cli.get(url) != nil { r.Err = e.AlreadyExists return *r } /* (4) Store */ - err = cli.Set(url, target, 0).Err() - if err != nil { + if !cli.set(url, target) { + r.Err = e.Failure + return *r + } + + r.Err = e.Success + return *r +} + +// Overrides a existing tinyurl with new target +func Put(d i.Arguments, r *i.Response) i.Response { + + /* (1) Init redis connection */ + cli := 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(url) == nil { + r.Err = e.NoMatchFound + return *r + } + + /* (4) Update */ + if !cli.set(url, target) { + r.Err = e.Failure + return *r + } + + r.Err = e.Success + return *r +} + +// Deletes an existing tinyurl +func Delete(d i.Arguments, r *i.Response) i.Response { + + /* (1) Init redis connection */ + cli := 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(url) == nil { + r.Err = e.NoMatchFound + return *r + } + + /* (4) Delete */ + if !cli.del(url) { r.Err = e.Failure return *r } From 3fcb75e44d8891fdbd9f9d07ef47f8761bcd65a4 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 17 Jun 2018 21:24:47 +0200 Subject: [PATCH 3/8] set 'redis' management in local package + add 'auth' controller (in manifest only) TODO --- manifest.json | 17 +++++++++- root/authi.go | 18 +++++++++++ root/db/db.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ root/i.go | 86 +++++++-------------------------------------------- 4 files changed, 121 insertions(+), 75 deletions(-) create mode 100644 root/authi.go create mode 100644 root/db/db.go 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 } From 4180b8dcb0e9506b0a1600caac79e6a786bbad71 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 17 Jun 2018 21:25:06 +0200 Subject: [PATCH 4/8] update port to 4242 --- cmd/test/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index c2cc9d5..edcb8f4 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -13,7 +13,7 @@ func main() { } log.Printf("Server up and running\n") - err = srv.Launch(4444) + err = srv.Launch(4242) if err != nil { log.Fatalf("*** server failed (%s)\n", err) } From 3e3e4f796a494d4b79cd996339dbd8130f63fccd Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 5 Jul 2018 19:18:25 +0200 Subject: [PATCH 5/8] rename repo --- README.md | 6 +++--- cmd/test/main.go | 2 +- root/authi.go | 4 ++-- root/i.go | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2e4a3e1..a79f1b9 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ All commands above must be launched from the root of this repository. In order for next steps to work properly whatever your configuration, you might execute the following command: ```bash -chmod +x $GOPATH/src/git.xdrm.io/go/xb-api/*.sh +chmod +x $GOPATH/src/git.xdrm.io/go/aicra/*.sh ``` ### 2. Build type checkers @@ -14,7 +14,7 @@ chmod +x $GOPATH/src/git.xdrm.io/go/xb-api/*.sh Default types have to be built before using them. ```bash -$GOPATH/src/git.xdrm.io/go/xb-api/build-types.sh +$GOPATH/src/git.xdrm.io/go/aicra/build-types.sh ``` If the build process succeeds, the `./types` folder must contain *.so* files. @@ -24,7 +24,7 @@ If the build process succeeds, the `./types` folder must contain *.so* files. Controllers are located under the `./root` file structure. ```bash -$GOPATH/src/git.xdrm.io/go/xb-api/build-controllers.sh ./root +$GOPATH/src/git.xdrm.io/go/aicra/build-controllers.sh ./root ``` If the build process succeeds, the `./controllers` folder must contain *.so* files. diff --git a/cmd/test/main.go b/cmd/test/main.go index edcb8f4..f61b0ba 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -1,7 +1,7 @@ package main import ( - "git.xdrm.io/go/xb-api" + "git.xdrm.io/go/aicra" "log" ) diff --git a/root/authi.go b/root/authi.go index 8fd855e..a91037b 100644 --- a/root/authi.go +++ b/root/authi.go @@ -2,8 +2,8 @@ package main import ( "fmt" - e "git.xdrm.io/go/xb-api/err" - i "git.xdrm.io/go/xb-api/implement" + e "git.xdrm.io/go/aicra/err" + i "git.xdrm.io/go/aicra/implement" ) // Builds an access token from credentials diff --git a/root/i.go b/root/i.go index 1eb7130..000d83b 100644 --- a/root/i.go +++ b/root/i.go @@ -2,8 +2,8 @@ package main import ( db "./db" - e "git.xdrm.io/go/xb-api/err" - i "git.xdrm.io/go/xb-api/implement" + e "git.xdrm.io/go/aicra/err" + i "git.xdrm.io/go/aicra/implement" ) // Redirects to an url from a key From b488f8969ec7ed205d40088abb7471ac2807aa3f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 7 Jul 2018 18:13:08 +0200 Subject: [PATCH 6/8] main update --- cmd/test/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index f61b0ba..88c8b50 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -7,12 +7,13 @@ import ( func main() { - srv, err := gfw.Init("manifest.json", nil) + srv, err := aicra.Init("manifest.json") if err != nil { - log.Fatal(err) + log.Fatal("cannot load config", err) } - log.Printf("Server up and running\n") + log.Printf("[Server up] 0.0.0.0:4242\n") + err = srv.Launch(4242) if err != nil { log.Fatalf("*** server failed (%s)\n", err) From ee82f48adac5c0bf8597a02cd247a77fb00c7d0c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 7 Jul 2018 23:09:19 +0200 Subject: [PATCH 7/8] update --- .gitignore | 3 ++- cmd/test/main.go | 4 ++-- middleware/1-auth/main.go | 11 +++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 middleware/1-auth/main.go diff --git a/.gitignore b/.gitignore index f1fe8d1..d7a1847 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.so \ No newline at end of file +*.so +.build \ No newline at end of file diff --git a/cmd/test/main.go b/cmd/test/main.go index 88c8b50..7677cb2 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -7,14 +7,14 @@ import ( func main() { - srv, err := aicra.Init("manifest.json") + server, err := aicra.New("manifest.json") if err != nil { log.Fatal("cannot load config", err) } log.Printf("[Server up] 0.0.0.0:4242\n") - err = srv.Launch(4242) + err = server.Listen(4242) if err != nil { log.Fatalf("*** server failed (%s)\n", err) } diff --git a/middleware/1-auth/main.go b/middleware/1-auth/main.go new file mode 100644 index 0000000..a177db9 --- /dev/null +++ b/middleware/1-auth/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "git.xdrm.io/go/aicra/middleware" + "net/http" +) + +// Authentication middleware +func Inspect(req http.Request, scope middleware.Scope){ + scope = append(scope, "user"); +} From d5b9567663b6f94191853bcbed70dff97ed8ffdb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 7 Jul 2018 23:35:29 +0200 Subject: [PATCH 8/8] fix middleware + add example for manifest scope --- manifest.json | 6 +++--- middleware/1-auth/main.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/manifest.json b/manifest.json index af492f4..16a033a 100644 --- a/manifest.json +++ b/manifest.json @@ -10,7 +10,7 @@ "POST": { "info": "creates a new tiny url", - "scope": [[]], + "scope": [["admin"]], "in": { "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" }, "target": { "info": "url to shorten", "type": "varchar(5,300)" } @@ -20,7 +20,7 @@ "PUT": { "info": "overrides an existing tiny url", - "scope": [[]], + "scope": [["admin"]], "in": { "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" }, "target": { "info": "url to shorten", "type": "varchar(5,300)" } @@ -30,7 +30,7 @@ "DELETE": { "info": "removes an existing tiny url", - "scope": [[]], + "scope": [["admin"]], "in": { "URL#0": { "info": "preferred tiny url", "type": "varchar(1,30)", "name": "url" } }, diff --git a/middleware/1-auth/main.go b/middleware/1-auth/main.go index a177db9..6bfcc9d 100644 --- a/middleware/1-auth/main.go +++ b/middleware/1-auth/main.go @@ -6,6 +6,6 @@ import ( ) // Authentication middleware -func Inspect(req http.Request, scope middleware.Scope){ - scope = append(scope, "user"); +func Inspect(req http.Request, scope *middleware.Scope) { + *scope = append(*scope, "admin") }