diff --git a/doc/README.md b/doc/README.md index ad42540..51646e3 100644 --- a/doc/README.md +++ b/doc/README.md @@ -99,144 +99,38 @@ The API uses the routes defined in the _api.json_ configuration to export your c * `PUT` to edit an existing article * `DELETE` to delete an exisint article -*Note that each method must be a valid HTTP METHOD*. +*Note that these are all the available methods handled by aicra* # **III.** Project Configuration - -The documentation consists of a _chain_ of urls each one can contain several HTTP method specifications. -Note that each url can be chained apart the method specifications. - - - +The documentation consists of directions for how to compile and run the project controllers, middle-wares and type checkers. ## **1** - configuration format -The configuration is a set of `uri` paths that can contain up to 4 method types: **POST**, **DELETE**, **PUT**, **GET** or `uri` subpaths. - -For instance the 4 methods directly inside `uri1` will be triggered when the calling URI is `/uri1`, the ones directly inside `uri2` will be triggered when calling `/uri1/uri2` and so on.. - -You can also set full paths if you don't need transitional methods, for instance the path `uri5/uri6/uri7` will be triggered by the url `/uri5/uri6/uri7`. - -**Example:** The example file loaded with the default configuration can be found [here](./../../src/config/api/3.0/modules.json). ```json { - "uri1" : { - "GET": method.definition, - "POST": method.definition, - "PUT": method.definition, - "DELETE": method.definition, - - "uri2": { - "GET": method.definition, - "POST": method.definition, - "PUT": method.definition, - "DELETE": method.definition, - - "uri3": {} - } - }, - - "uri5/uri6/uri7": { - "GET": method.definition, - "POST": method.definition, - "PUT": method.definition, - "DELETE": method.definition - } + "root": "", + "driver": "", + "types": { + "default": true, + "folder": "" + }, + "controllers": { + "folder": "api definition. + +### example + +For instance here, we implement a simple **user** controller. + +```go +package main + +import ( + "git.xdrm.io/aicra/driver" + "git.xdrm.io/aicra/response" + "git.xdrm.io/aicra/err" +) + +// for the API code to export our middle-ware +func Export() driver.Controller { return new(UserController) } + +// mockup type to implement the driver.Controller interface +type UserController interface{} + +func (ctl UserController) Get(args response.Arguments) response.Response { + res := response.New() + + // extract user ID argument + user_id, ok := args["user_id"].(float64) + if !ok { + res.Err = e.Failure + return *res + } + + // fetch user data + res.Set("user", getUserData(user_id)) + return res +} + +func (ctl UserController) Post(args response.Arguments) response.Response { + res := response.New() + + // extract user ID argument + username, ok := args["username"].(string) + password, ok1 := args["password"].(string) + + if !ok || !ok1 { + res.Err = e.Failure + return *res + } + + // store user data + res.Set("created", storeUser(username, password)) + return res +} +``` + +*Note*: Functions `getUserData(int) map[string]string` and `storeUser(string,string) bool` do not exist, it was in order for all to understand the example. + + + + # **V.** Type Checker -Each type checker checks the parameter values according to the type given in the api definition. +Each type checker checks http request extracted values according to the type given in the api definition. -The default types below are available in the default package. -To add a new type, just open the file `/build/api/Checker.php` and add an entry in the `switch` statement. +The default types below are available in the `$GOPATH/src/git.xdrm.io/go/aicra/internal/checker/default`, they are loaded by default in any project. You can choose not to load them by settings `types`.`default` to `false` in the project configuration. To add custom types you must implement the [driver.Checker](https://godoc.org/git.xdrm.io/go/aicra/driver#Checker) interface and export it in order for aicra to dynamically load it. ## **1** - Default types @@ -481,7 +413,6 @@ To add a new type, just open the file `/build/api/Checker.php` and add an entry |`float`|`-10.2`, `23.5`|Any float| |`string`|`"Hello!"`|String that can be of any length (even empty)| |`digest(L)`|`"4612473aa81f93a878..."`|String with a length of `L`, containing only hexadecimal lowercase characters.| -|`mail`|`"a.b@c.def"`|Valid email address| |`array`|`[]`, `[1, "a"]`|Any array| |`bool`|`true`, `false`|Boolean| |`varchar(a,b)`|`"Hello!"`|String with a length between `a` and `b` (included)|