aicra/README.md

197 lines
7.4 KiB
Markdown
Raw Permalink Normal View History

2018-07-08 17:12:47 +00:00
# | aicra |
2018-05-30 15:01:16 +00:00
2018-07-08 23:13:06 +00:00
[![Go version](https://img.shields.io/badge/go_version-1.10.3-blue.svg)](https://golang.org/doc/go1.10)
2018-07-11 17:06:11 +00:00
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
2018-07-08 17:12:47 +00:00
[![Go Report Card](https://goreportcard.com/badge/git.xdrm.io/go/aicra)](https://goreportcard.com/report/git.xdrm.io/go/aicra)
2018-07-08 23:13:06 +00:00
[![Go doc](https://godoc.org/git.xdrm.io/go/aicra?status.svg)](https://godoc.org/git.xdrm.io/go/aicra)
2019-11-19 10:45:29 +00:00
[![Build Status](https://drone.xdrm.io/api/badges/go/aicra/status.svg)](https://drone.xdrm.io/go/aicra)
2018-05-30 15:01:16 +00:00
2018-07-11 17:06:11 +00:00
2020-04-04 15:33:08 +00:00
Aicra is a *configuration-driven* REST API engine written in Go.
2020-04-04 15:33:08 +00:00
Most of the management is done for you using a configuration file describing your API. you're left with implementing :
2020-03-22 14:03:35 +00:00
- handlers
2019-09-26 12:28:11 +00:00
- optionnally middle-wares (_e.g. authentication, csrf_)
2020-03-22 14:03:35 +00:00
- and optionnally your custom type checkers to check input parameters
2018-10-01 19:11:11 +00:00
2020-04-04 15:33:08 +00:00
> A example project is available [here](https://git.xdrm.io/go/articles-api)
2018-10-01 19:11:11 +00:00
2020-04-04 15:33:08 +00:00
## Table of contents
2018-07-11 17:13:15 +00:00
<!-- toc -->
2019-09-26 12:28:11 +00:00
- [I/ Installation](#i-installation)
2020-04-04 15:33:08 +00:00
- [II/ Usage](#ii-usage)
* [1) Build a server](#1-build-a-server)
* [2) API Configuration](#2-api-configuration)
- [Definition](#definition)
+ [Input Arguments](#input-arguments)
- [1. Input types](#1-input-types)
- [2. Global Format](#2-global-format)
2019-09-26 12:28:11 +00:00
- [III/ Change Log](#iii-change-log)
2018-07-11 17:13:15 +00:00
<!-- tocstop -->
2018-07-08 17:12:47 +00:00
2020-04-04 15:33:08 +00:00
## I/ Installation
2020-04-04 15:33:08 +00:00
You need a recent machine with `go` [installed](https://golang.org/doc/install). This package has not been tested under the version **1.14**.
```bash
2018-10-02 07:58:51 +00:00
go get -u git.xdrm.io/go/aicra/cmd/aicra
```
2019-09-26 12:28:11 +00:00
The library should now be available as `git.xdrm.io/go/aicra` in your imports.
2018-07-08 08:44:50 +00:00
2020-04-04 15:33:08 +00:00
## II/ Usage
2020-04-04 15:33:08 +00:00
### 1) Build a server
2018-07-08 17:12:47 +00:00
2020-04-04 15:33:08 +00:00
Here is some sample code that builds and sets up an aicra server using your api configuration file.
2018-07-08 17:12:47 +00:00
2019-09-26 12:28:11 +00:00
```go
package main
2020-03-22 14:03:35 +00:00
2019-09-26 12:28:11 +00:00
import (
2020-04-04 15:33:08 +00:00
"log"
"net/http"
"os"
2018-07-08 08:44:50 +00:00
2020-04-04 15:33:08 +00:00
"git.xdrm.io/go/aicra"
"git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/datatype/builtin"
2019-09-26 12:28:11 +00:00
)
2018-07-08 08:44:50 +00:00
2019-09-26 12:28:11 +00:00
func main() {
2018-07-08 08:44:50 +00:00
2020-04-04 15:33:08 +00:00
builder := &aicra.Builder{}
// add datatypes your api uses
builder.AddType(builtin.BoolDataType{})
builder.AddType(builtin.UintDataType{})
builder.AddType(builtin.StringDataType{})
2020-03-22 14:03:35 +00:00
2020-04-04 15:33:08 +00:00
config, err := os.Open("./api.json")
2020-03-22 14:03:35 +00:00
if err != nil {
2020-04-04 15:33:08 +00:00
log.Fatalf("cannot open config: %s", err)
2020-03-22 14:03:35 +00:00
}
2020-04-04 15:33:08 +00:00
// pass your configuration
err = builder.Setup(config)
config.Close()
2020-03-22 14:03:35 +00:00
if err != nil {
2020-04-04 15:33:08 +00:00
log.Fatalf("invalid config: %s", err)
2020-03-22 14:03:35 +00:00
}
2020-04-04 15:33:08 +00:00
// bind your handlers
builder.Bind(http.MethodGet, "/user/{id}", getUserById)
builder.Bind(http.MethodGet, "/user/{id}/username", getUsernameByID)
// build the server and start listening
server, err := builder.Build()
if err != nil {
log.Fatalf("cannot build server: %s", err)
}
http.ListenAndServe("localhost:8080", server)
2018-10-03 16:37:37 +00:00
}
```
2020-04-04 15:33:08 +00:00
Here is an example handler
```go
type req struct{
Param1 int
Param3 *string // optional are pointers
}
type res struct{
Output1 string
Output2 bool
}
func myHandler(r req) (*res, api.Error) {
err := doSomething()
if err != nil {
return nil, api.ErrorFailure
}
return &res{}, api.ErrorSuccess
}
```
2018-10-03 16:37:37 +00:00
2020-04-04 15:33:08 +00:00
### 2) API Configuration
2018-07-08 08:44:50 +00:00
2020-04-04 15:33:08 +00:00
The whole api behavior is described inside a json file (_e.g. usually api.json_). For a better understanding of the format, take a look at this working [template](https://git.xdrm.io/go/articles-api/src/master/api.json). This file defines :
2020-03-22 14:03:35 +00:00
- routes and their methods
2018-07-08 08:44:50 +00:00
- every input for each method (called *argument*)
- every output for each method
2020-03-22 14:03:35 +00:00
- scope permissions (list of permissions needed by clients)
2018-10-01 19:11:11 +00:00
- input policy :
2020-04-04 15:33:08 +00:00
- type of argument (_c.f. data types_)
2020-03-22 14:03:35 +00:00
- required/optional
- variable renaming
2018-10-01 19:11:11 +00:00
2020-04-04 15:33:08 +00:00
#### Format
2020-04-04 15:33:08 +00:00
The root of the json file must be an array containing your requests definitions. For each, you will have to create fields described in the table above.
2018-10-03 16:37:37 +00:00
| field path | description | example |
| ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| `info` | A short human-readable description of what the method does | `create a new user` |
| `scope` | A 2-dimensional array of permissions. The first dimension can be translated to a **or** operator, the second dimension as a **and**. It allows you to combine permissions in complex ways. | `[["A", "B"], ["C", "D"]]` can be translated to : this method needs users to have permissions (A **and** B) **or** (C **and** D) |
2020-04-04 15:33:08 +00:00
| `in` | The list of arguments that the clients will have to provide. [Read more](#input-arguments). | |
| `out` | The list of output data that will be returned by your controllers. It has the same syntax as the `in` field but optional parameters are not allowed |
2018-10-03 16:37:37 +00:00
2020-04-04 15:33:08 +00:00
### Input Arguments
2018-10-03 16:37:37 +00:00
Input arguments defines what data from the HTTP request the method needs. Aicra is able to extract 3 types of data :
2020-04-04 15:33:08 +00:00
- **URI** - data from inside the request path. For instance, if your controller is bound to the `/user/{id}` URI, you can set the input argument `{id}` matching this uri part.
2018-10-03 16:37:37 +00:00
- **Query** - data formatted at the end of the URL following the standard [HTTP Query](https://tools.ietf.org/html/rfc3986#section-3.4) syntax.
- **URL encoded** - data send inside the body of the request but following the [HTTP Query](https://tools.ietf.org/html/rfc3986#section-3.4) syntax.
- **Multipart** - data send inside the body of the request with a dedicated [format](https://tools.ietf.org/html/rfc2388#section-3). This format is not very lightweight but allows you to receive data as well as files.
- **JSON** - data send inside the body as a json object ; each key being a variable name, each value its content. Note that the HTTP header '**Content-Type**' must be set to `application/json` for the API to use it.
2020-04-04 15:33:08 +00:00
#### Format
2018-10-03 16:37:37 +00:00
The `in` field in each method contains as list of arguments where the key is the argument name, and the value defines how to manage the variable.
2020-03-22 14:03:35 +00:00
> Variable names from **URI** or **Query** must be named accordingly :
2018-10-03 16:37:37 +00:00
>
2020-03-22 14:03:35 +00:00
> - the **URI** variable `{id}` from your request route must be named `{id}`.
> - the variable `somevar` in the **Query** has to be names `GET@somevar`.
2018-10-03 16:37:37 +00:00
**Example**
In this example we want 3 arguments :
```json
2020-03-22 14:03:35 +00:00
[
{
"method": "PUT",
"path": "/article/{id}",
"scope": [["author"]],
"info": "updates an article",
"in": {
"{id}": { "info": "article id", "type": "int", "name": "article_id" },
"GET@title": { "info": "new article title", "type": "?string", "name": "title" },
"content": { "info": "new article content", "type": "string" }
},
"out": {
"id": { "info": "updated article id", "type": "uint" },
"title": { "info": "updated article title", "type": "string" },
"content": { "info": "updated article content", "type": "string" }
}
}
]
2018-10-03 16:37:37 +00:00
```
2020-04-04 15:33:08 +00:00
- the 1^st^ one is send at the end of the URI and is a number compliant with the `int` type checker. It is renamed `article_id`, this new name will be sent to the handler.
- the 2^nd^ one is send in the query (_e.g. [http://host/uri?get-var=value](http://host/uri?get-var=value)_). It must be a valid `string` or not given at all (the `?` at the beginning of the type tells that the argument is **optional**) ; it will be named `title`.
- the 3^rd^ can be send with a **JSON** body, in **multipart** or **URL encoded** it makes no difference and only give clients a choice over the technology to use. If not renamed, the variable will be given to the handler with the name `content`.