Compare commits

..

9 Commits

Author SHA1 Message Date
Adrien Marquès 1f9c3c02c1 Merge branch '0.2.0' of go/aicra into master
continuous-integration/drone/push Build is passing Details
2019-11-19 10:49:50 +00:00
Adrien Marquès 42b09c5c38 fix badge
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2019-11-19 11:45:29 +01:00
Adrien Marquès 0348d33439 remove jenkins support
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2019-11-18 16:18:42 +01:00
Adrien Marquès 82188560c6 add builtin.bool test + make other types that allow 'string' allow '[]byte' as well
continuous-integration/drone/push Build is passing Details
2019-11-18 16:17:02 +01:00
Adrien Marquès 1953abf722 add ci support
continuous-integration/drone/push Build is passing Details
2019-11-18 16:03:18 +01:00
Adrien Marquès acfbee3894 add startup logs
Log aicra configuration 'METHOD /PATH' when creating the instance
Log mapped handlers before starting listening

To know when we start listening (was transparently handled by http.ListenAndServer) ; added a wrapper around 'Server' (i.e. httpServer) that fulfills the http.Server interface
2019-09-26 19:03:37 +02:00
Adrien Marquès cabbc53a86 Merge branch '0.2.0' of gogs:go/aicra into 0.2.0 2019-09-26 18:09:52 +02:00
Adrien Marquès 4eb9f58074 Update readme 2019-09-26 14:28:11 +02:00
Adrien Marquès 54f2496943 feat: typechecks: tests for 'float64' 2019-05-06 19:26:36 +02:00
14 changed files with 515 additions and 489 deletions

11
.drone.yml Normal file
View File

@ -0,0 +1,11 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: test
image: golang:1.13
commands:
- go get ./...
- go test -v -race -cover -coverprofile ./coverage.out ./...

424
README.md
View File

@ -4,149 +4,99 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Go Report Card](https://goreportcard.com/badge/git.xdrm.io/go/aicra)](https://goreportcard.com/report/git.xdrm.io/go/aicra) [![Go Report Card](https://goreportcard.com/badge/git.xdrm.io/go/aicra)](https://goreportcard.com/report/git.xdrm.io/go/aicra)
[![Go doc](https://godoc.org/git.xdrm.io/go/aicra?status.svg)](https://godoc.org/git.xdrm.io/go/aicra) [![Go doc](https://godoc.org/git.xdrm.io/go/aicra?status.svg)](https://godoc.org/git.xdrm.io/go/aicra)
[![Build Status](https://ci.migration.xdrm.io/buildStatus/icon?job=aicra%2F0.2.0)](.) [![Build Status](https://drone.xdrm.io/api/badges/go/aicra/status.svg)](https://drone.xdrm.io/go/aicra)
**Aicra** is a *configuration-driven* REST **API engine** in *Go* that allows you to create a fully featured API. **Aicra** is a *configuration-driven* **web framework** written in Go that allows you to create a fully featured REST API.
The whole API management is done for you from a configuration file describing your API, you just need to implement : The whole management is done for you from a configuration file describing your API, you're left with implementing :
- controllers
- optionnally middle-wares (_e.g. authentication, csrf_)
- and optionnally type checkers to check input parameters
- the <u>controllers</u>
- the <u>middle-wares</u> (_e.g. authentication, csrf_)
- and optionnally the <u>type checkers</u> to check if input values follows some rules
> There is 2 available drivers that will load your implementations. The `plugin` driver is for Go programmers, the `generic` one is for any language (it uses standard input and output). The aicra server fulfills the `net/http` [Server interface](https://golang.org/pkg/net/http/#Server).
The engine has been designed with the following concepts in mind. > A example project is available [here](https://git.xdrm.io/go/tiny-url-ex)
| concept | explanation |
|---|---|
| meaningful defaults | Defaults and default values work without further understanding |
| configuration driven | Avoid information duplication. Automate anything that can be automated without losing control. Have *one* configuration that summarizes the whole API, its behavior and its automation flow. |
> A example project is available [here](https://git.xdrm.io/example/aicra)
### Table of contents ### Table of contents
<!-- toc --> <!-- toc -->
* [I. Installation](#i-installation) - [I/ Installation](#i-installation)
* [1. Download and install the package](#1-download-and-install-the-package) - [II/ Development](#ii-development)
* [II. Setup a project](#ii-setup-a-project) * [1) Main executable](#1-main-executable)
* [1. Compilation configuration](#1-compilation-configuration) * [2) API Configuration](#2-api-configuration)
* [Example](#example) - [Definition](#definition)
* [2. API Configuration](#2-api-configuration) + [Input Arguments](#input-arguments)
* [Definition](#definition) - [1. Input types](#1-input-types)
* [Input Arguments](#input-arguments) - [2. Global Format](#2-global-format)
* [1. Input types](#1-input-types) - [III/ Change Log](#iii-change-log)
* [2. Global Format](#2-global-format)
* [3. Example](#3-example)
* [3. Controllers](#3-controllers)
* [1. Plugin driver](#1-plugin-driver)
* [2. Generic driver](#2-generic-driver)
* [4. Middle-wares](#4-middle-wares)
* [1. Plugin driver](#1-plugin-driver-1)
* [2. Generic driver](#2-generic-driver-1)
* [5. Type checkers](#5-type-checkers)
* [1. Plugin driver](#1-plugin-driver-2)
* [2. Generic driver](#2-generic-driver-2)
* [III. Build your project](#iii-build-your-project)
* [IV. Main](#iv-main)
* [V. Change Log](#v-change-log)
<!-- tocstop --> <!-- tocstop -->
### I. Installation ### I/ Installation
You need a recent machine with `go` [installed](https://golang.org/doc/install). You need a recent machine with `go` [installed](https://golang.org/doc/install). This package has not been tested under the version **1.10**.
> This package has not been tested under the version **1.10**.
#### 1. Download and install the package
```bash ```bash
go get -u git.xdrm.io/go/aicra/cmd/aicra go get -u git.xdrm.io/go/aicra/cmd/aicra
``` ```
The library should now be available locally as `git.xdrm.io/go/aicra` your imports. Moreover, the **project compiler** have been installed as the `aicra` command. The library should now be available as `git.xdrm.io/go/aicra` in your imports.
> The executable `aicra` will be placed into your `$GOPATH/bin` folder, if added to your environment PATH it should be available as a standalone command in your terminal. If not, you can simply run `$GOPATH/bin/aicra` to use the command or create a symbolic link into `/usr/local/bin` for instance.
### II/ Development
### II. Setup a project
The default project structure is :
```bash
├── main.go # entry point
|
├── aicra.json # server configuration file
├── api.json # API configuration file
|
├── middleware # middleware implementations
├── controller # controller implementations
└── type # custom type checkers
```
#### 1) Main executable
#### 1. Compilation configuration The main executable will declare and run the aicra server, it might look quite like the code below.
The `aicra.json` configuration file defines where are located your controllers, type checkers, and middle-wares ; also it contains what driver you want to use, you have 2 choices : ```go
package main
import (
"log"
"net/http"
1. **plugin** - for Go implementations (_c.f. [go plugin system](https://golang.org/pkg/plugin/)_) "git.xdrm.io/go/aicra"
2. **generic** - for any language implementation (uses standard input and output) "git.xdrm.io/go/aicra/typecheck/builtin"
"git.xdrm.io/go/aicra/api"
)
func main() {
// 1. build server
server, err := aicra.New("path/to/your/api/definition.json");
if err != nil {
log.Fatalf("Cannot build the aicra server: %v\n", err)
}
The file uses the [json](https://json.org/) format, each field is described in the table above. // 2. add type checkers
server.Checkers.Add( builtin.NewAny() );
server.Checkers.Add( builtin.NewString() );
server.Checkers.Add( builtin.NewFloat64() );
| field | description | example value | // 3. bind your implementations
| ---------------------- | ------------------------------------------------------------ | --------------------------------- | server.HandleFunc(http.MethodGet, func(req api.Request, res *api.Response){
| `root` | The project folder path | `./some-path` or `/some/path` | // ... process stuff ...
| `driver` | The driver to use for loading controllers, middlewares and type checkers | `plugin` or `generic` | res.SetError(api.ErrorSuccess());
| `types`.`default` | Whether to load default types into the project | `true` or `false` | })
| `types`.`folder` | The folder (relative to the project root) where type checkers' implementations are located | `./type` or `type` |
| `controllers`.`folder` | The folder (relative to the project root) where controllers' implementations are located | `./controller` or `controller` |
| `middlewares`.`folder` | The folder (relative to the project root) where middlewares' implementations are located | `./middleware` or `middleware` |
A sample file can be found [here](https://git.xdrm.io/example/aicra/src/master/aicra.json). // 4. launch server
log.Fatal( http.ListenAndServer("localhost:8181", server) )
###### Example
In this example we have the controllers inside the `controller` folder, the middle-wares in the `middleware` folder and custom type checkers inside the `checker` folder, we want to load the built-in type checkers and are using the `plugin` driver. Also our project root is the relative current path `.` ; note that it is better using an absolute path as your project root.
```json
{
"root": ".",
"driver": "plugin",
"types": {
"default": true,
"folder": "checker"
},
"controllers": {
"folder": "controller.plugin"
},
"middlewares": {
"folder": "middleware.plugin"
}
} }
``` ```
#### 2. API Configuration #### 2) API Configuration
The whole project behavior is described inside the `api.json` file. For a better understanding of the format, take a look at this working [template](https://git.xdrm.io/example/aicra/src/master/api.json). This file defines : The whole project behavior is described inside a json file (_e.g. usually api.json_) file. For a better understanding of the format, take a look at this working [template](https://git.xdrm.io/go/tiny-url-ex/src/master/api.json). This file defines :
- resource routes and their methods - resource routes and their methods
- every input for each method (called *argument*) - every input for each method (called *argument*)
@ -182,11 +132,8 @@ For each method you will have to create fields described in the table above.
| `out` | The list of output data that will be returned by your controllers. It has the same syntax as the `in` field but is only use for readability purpose and documentation. | | | `out` | The list of output data that will be returned by your controllers. It has the same syntax as the `in` field but is only use for readability purpose and documentation. | |
##### Input Arguments ##### Input Arguments
###### 1. Input types ###### 1. Input types
Input arguments defines what data from the HTTP request the method needs. Aicra is able to extract 3 types of data : Input arguments defines what data from the HTTP request the method needs. Aicra is able to extract 3 types of data :
@ -239,248 +186,7 @@ In this example we want 3 arguments :
###### 3. Example ### III/ Change Log
In this example you can see a pretty basic user/article REST API definition. The API let's you fetch, create, edit, and delete users and do the same for their articles. Users actions will be available at the uri `/user`, and `/article` for articles.
#### 3. Controllers
Controllers implement `Get`, `Post`, `Put`, and `Delete` methods, and have access to special variables injected in the argument list :
- `_HTTP_METHOD_` the request's HTTP method in uppercase
- `_SCOPE_` the scope filled by middle-wares
- `_AUTHORIZATION_` the request's **Authorization** header
Also special variables found in the return data are processed with special actions :
- `_REDIRECT_` will redirect to the URL contained in the variable
##### 1. Plugin driver
For each route, you'll have to place your implementation into the `controller` folder (according to the *aicra.json* configuration) following the naming convention : add `/main.go` at the end of the route.
> <u>Example</u> - the URI `/path/to/some/uri` is handled by the file `controller/path/to/some/uri/main.go`
>
> <u>Exception</u> - the URI `/` is handled by the file `controller/ROOT/main.go`
A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/controller.plugin).
Each controller must implement the [driver.Controller](https://godoc.org/git.xdrm.io/go/aicra/driver#Controller) interface. In addition you must declare the function `func Export() Controller` to allow dynamic loading of your controller.
**Example**
Here is a base code for any controllers
```go
package main
import (
"git.xdrm.io/go/aicra/driver"
"git.xdrm.io/go/aicra/api"
e "git.xdrm.io/go/aicra/err"
)
// Mockup controller implementation
type MyController interface{}
func Export() driver.Controller { return new(MyController) }
// GET method management
func (c MyController) Get(args api.Arguments) api.Response {
res := api.NewResponse()
res.Err = e.Success
return *res
}
// POST method management
func (c MyController) Post(args api.Arguments) api.Response { /*...*/ }
// PUT method management
func (c MyController) Put(args api.Arguments) api.Response { /*...*/ }
// DELETE method management
func (c MyController) Delete(args api.Arguments) api.Response { /*...*/ }
```
##### 2. Generic driver
This is the same as with the plugin driver but without `/main.go` at the end.
> <u>Example</u> - The URI `/path/to/some/uri` will be handled by the executable `controller/path/to/some/uri`.
> <u>Exception</u> - The URI `/` will be handled by the executable `controller/ROOT`.
A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/controller.generic).
The programs will be given useful data (*i.e. method and arguments*) through its input arguments :
| Argument index | Description | Examble value |
| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| 1 | Uppercase HTTP method.<br>(_e.g. **$1** in bash, **argv[1]** in php_) | `GET`, `POST`, ... |
| 2 | JSON representation of the input arguments.<br>(_e.g. **$2** in bash, **argv[2]** in php_) | `{`<br> `"_SCOPE_": ["admin", "token"],`<br> `"somstring": "string",`<br> `"someint": 12`<br>`}` |
The standard output you will give back must be a key-value JSON representation of all the output variables.
#### 4. Middle-wares
In order for your project to manage authentication, the best solution is to use middle-wares, there are programs that updates a *Scope* (*i.e. a list of strings*) according to internal or persistent (*i.e.* database) information and the actual http request. They are all run before each request is forwarded to your controller. The Scopes are used to match the `scope` field in the configuration file and automatically block access to non-authenticated method calls. Scopes can also be used for implementation-specific behavior such as _CSRF_ management. Controllers have access to the scope through the variable `_SCOPE_`.
##### 1. Plugin driver
Each middleware must be **directly** inside the `middleware` folder (according to the _aicra.json_ configuration).
> Example - the `1-authentication` middleware will be inside `middleware/1-authentication/main.go`.
**Note** - middle-ware execution will be ordered by name. Prefixing your middle-wares with their order is a good practice.
A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/middleware.plugin).
Each middle-ware must implement the [driver.Middleware](https://godoc.org/git.xdrm.io/go/aicra/driver#Middleware) interface. In addition you must declare the function `func Export() Middleware` to allow dynamic loading of your middle-ware.
**Example**
Here is a base code for any middle-ware
```go
package main
import (
"git.xdrm.io/go/aicra/driver"
"net/http"
)
// Mockup middle-ware implementation
type MyMiddleware interface{}
func Export() driver.Middleware { return new(MyMiddleware) }
func (c MyMiddleware) Inspect(req http.Request, scope *[]string) {
// add scope according to request
if req.Header.Get("SomeHeader") {
*scope = append(*scope, "some-scope")
}
}
```
##### 2. Generic driver
This is the same as with the plugin driver but instead of without `/main.go` at the end.
> Example - the `1-authentication` middle-ware will be inside `middleware/1-authentication` where **1-authentication** is an executable
A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/middleware.generic).
The programs will be given useful data (*i.e. method and arguments*) through its input arguments :
| Argument index | Description | Examble value |
| -------------- | ------------------------------------------------------------ | ------------- |
| 1 | JSON representation of the input arguments.<br>(_e.g. **$1** in bash, **argv[1]** in php_) | ??? |
The standard output you will give back must be a JSON array containing the scope you want to add.
#### 5. Type checkers
In your configuration you can use built-in types (*e.g.* int, any, varchar, token, float, ...), but if you want project-specific ones, you can add your own types inside the `type` folder. You can check what structure to follow by looking at the [built-in types](https://git.xdrm.io/go/aicra/src/master/internal/checker/default). Also it is not required that you use built-in types, you can ignore them by setting `types.default = false` in the _aicra.json_ configuration.
Each type must be **directly** inside the `type` folder. The package name is arbitrary and does not have to match the name (but it is better if it is explicit), because the `Match()` method already does that.
##### 1. Plugin driver
Each type checker must be **directly** inside the `type` folder (according to the _aicra.json_ configuration).
> Example - the `number` type checker will be inside `type/number/main.go`.
A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/type.plugin).
##### 2. Generic driver
This is the same as with the plugin driver but instead of without `/main.go` at the end.
> Example - the `number` type checker will be inside `type/number` where **number** is an executable
A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/type.generic).
The programs will be given useful data (*i.e. method and arguments*) through its input arguments :
| Argument index | Description | Examble value |
| -------------- | ------------------------------------------------------------ | ------------------ |
| 1 | Uppercase method.<br>(_e.g. **$1** in bash, **argv[1]** in php_) | `MATCH` or `CHECK` |
| 2 | JSON representation of the input arguments.<br>(_e.g. **$2** in bash, **argv[2]** in php_) | ??? |
The standard output you will give back must be a `1` or `0` representing `true` and `false`.
+ When calling the `MATCH` method, the input argument consists of a string being the type checker name, you must return `1` this name is handled by the current type checker.
+ When calling the `CHECK` method, the input argument consists of a JSON representation wrapped inside the key `value`. For instance it could be `{"value": [1,2,3]}` if the input value is an array containing 1, 2, and 3.
### III. Build your project
After each controller, middle-ware or type checker implementation, you'll have to compile the project. This can be achieved through the command-line builder.
Usage is `aicra /path/to/your/project`.
Usually you just have to run the following command inside your project directory :
```bash
aicra .
```
The output should look like
![that](./README.assets/1531039386654.png).
#### IV. Main
The main default program is pretty small as shown below :
```go
package main
import (
"git.xdrm.io/go/aicra"
"net/http"
)
func main() {
// build from config
server, err := aicra.New("api.json")
if err != nil { panic(err) }
// launch server
err = http.ListenAndServe("127.0.0.1:4242", server)
if err != nil { panic(err) }
}
```
#### V. Change Log
- [x] human-readable json configuration - [x] human-readable json configuration
- [x] nested routes (*i.e. `/user/:id:` and `/user/post/:id:`*) - [x] nested routes (*i.e. `/user/:id:` and `/user/post/:id:`*)
@ -492,26 +198,18 @@ func main() {
- [x] application/json - [x] application/json
- [x] required vs. optional parameters with a default value - [x] required vs. optional parameters with a default value
- [x] parameter renaming - [x] parameter renaming
- [ ] ~~generic authentication system (*i.e. you can override the built-in one*)~~ Replaced by the middle-ware system
- [x] generic type check (*i.e. implement custom types alongside built-in ones*) - [x] generic type check (*i.e. implement custom types alongside built-in ones*)
- [ ] built-in types - [ ] built-in types
- [x] `any` - wildcard matching all values - [x] `any` - wildcard matching all values
- [x] `int` - any number (*e.g. float, int, uint*) - [x] `int` - see go types
- [x] `uint` - see go types
- [x] `float` - see go types
- [x] `string` - any text - [x] `string` - any text
- [x] `varchar(min, max)` - any string with a length between `min` and `max` - [x] `string(min, max)` - any string with a length between `min` and `max`
- [ ] `<a>` - array containing **only** elements matching `a` type - [ ] `[a]` - array containing **only** elements matching `a` type
- [ ] `<a:b>` - map containing **only** keys of type `a` and values of type `b` (*a or b can be ommited*) - [ ] `[a:b]` - map containing **only** keys of type `a` and values of type `b` (*a or b can be ommited*)
- [x] generic controllers implementation (shared objects) - [x] generic controllers implementation (shared objects)
- [x] response interface - [x] response interface
- [ ] ~~devmode watcher~~ : watch manifest, watch plugins to compile + hot reload them - [ ] log bound resources when building the aicra server
- [x] driver for Go plugins - [ ] fail on check for unimplemented resources at server boot.
- [x] controllers - [ ] fail on check for unavailable types in api.json at server boot.
- [x] middlewares
- [x] type checkers
- [x] driver working with any executable through standard input and output
- [x] controllers
- [x] middlewares
- [x] type checkers
- [x] project configuration file to select **driver**, source folders and whether to load default type checkers.
- [x] used to compile the project by the `aicra` command
- [x] used to create an API from `aicra.New()`

21
build/Jenkinsfile vendored
View File

@ -1,21 +0,0 @@
pipeline{
agent {
docker {
image 'golang:alpine'
}
}
stages {
stage('Test'){
environment {
GO111MODULE = 'on'
GOCACHE = '/tmp/.cache'
CGO_ENABLED = 0
}
steps {
sh 'go version'
sh 'go get -v ./...'
sh 'go test -cover -parallel 10 -v ./... -coverprofile=coverage.out'
}
}
}
}

112
http.go Normal file
View File

@ -0,0 +1,112 @@
package aicra
import (
"log"
"net/http"
"strings"
"git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/internal/reqdata"
)
// httpServer wraps the aicra server to allow handling http requests
type httpServer Server
// ServeHTTP implements http.Handler and has to be called on each request
func (s httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// 1. build API request from HTTP request
request, err := api.NewRequest(r)
if err != nil {
log.Fatal(err)
}
// 2. find a matching service for this path in the config
serviceDef, pathIndex := s.services.Browse(request.URI)
if serviceDef == nil {
return
}
servicePath := strings.Join(request.URI[:pathIndex], "/")
if !strings.HasPrefix(servicePath, "/") {
servicePath = "/" + servicePath
}
// 3. check if matching methodDef exists in config */
var methodDef = serviceDef.Method(r.Method)
if methodDef == nil {
response := api.NewResponse(api.ErrorUnknownMethod())
response.ServeHTTP(w, r)
logError(response)
return
}
// 4. parse every input data from the request
store := reqdata.New(request.URI[pathIndex:], r)
/* (4) Check parameters
---------------------------------------------------------*/
parameters, paramError := s.extractParameters(store, methodDef.Parameters)
// Fail if argument check failed
if paramError.Code != api.ErrorSuccess().Code {
response := api.NewResponse(paramError)
response.ServeHTTP(w, r)
logError(response)
return
}
request.Param = parameters
/* (5) Search a matching handler
---------------------------------------------------------*/
var serviceHandler *api.Handler
var serviceFound bool
for _, handler := range s.handlers {
if handler.GetPath() == servicePath {
serviceFound = true
if handler.GetMethod() == r.Method {
serviceHandler = handler
}
}
}
// fail if found no handler
if serviceHandler == nil {
if serviceFound {
response := api.NewResponse()
response.SetError(api.ErrorUncallableMethod(), servicePath, r.Method)
response.ServeHTTP(w, r)
logError(response)
return
}
response := api.NewResponse()
response.SetError(api.ErrorUncallableService(), servicePath)
response.ServeHTTP(w, r)
logError(response)
return
}
/* (6) Execute handler and return response
---------------------------------------------------------*/
// 1. feed request with configuration scope
request.Scope = methodDef.Scope
// 1. execute
response := api.NewResponse()
serviceHandler.Handle(*request, response)
// 2. apply headers
for key, values := range response.Headers {
for _, value := range values {
w.Header().Add(key, value)
}
}
// 3. write to response
response.ServeHTTP(w, r)
return
}

119
server.go
View File

@ -3,14 +3,11 @@ package aicra
import ( import (
"io" "io"
"log" "log"
"net/http"
"os" "os"
"strings"
"git.xdrm.io/go/aicra/api" "git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/internal/config"
"git.xdrm.io/go/aicra/internal/reqdata"
checker "git.xdrm.io/go/aicra/typecheck" checker "git.xdrm.io/go/aicra/typecheck"
) )
@ -48,109 +45,14 @@ func New(configPath string) (*Server, error) {
return nil, err return nil, err
} }
// 4. log configuration services
log.Printf("=== Aicra configuration ===\n")
logService(*i.services, "")
return i, nil return i, nil
} }
// ServeHTTP implements http.Handler and has to be called on each request
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// 1. build API request from HTTP request
request, err := api.NewRequest(r)
if err != nil {
log.Fatal(err)
}
// 2. find a matching service for this path in the config
serviceDef, pathIndex := s.services.Browse(request.URI)
if serviceDef == nil {
return
}
servicePath := strings.Join(request.URI[:pathIndex], "/")
if !strings.HasPrefix(servicePath, "/") {
servicePath = "/" + servicePath
}
// 3. check if matching methodDef exists in config */
var methodDef = serviceDef.Method(r.Method)
if methodDef == nil {
response := api.NewResponse(api.ErrorUnknownMethod())
response.ServeHTTP(w, r)
logError(response)
return
}
// 4. parse every input data from the request
store := reqdata.New(request.URI[pathIndex:], r)
/* (4) Check parameters
---------------------------------------------------------*/
parameters, paramError := s.extractParameters(store, methodDef.Parameters)
// Fail if argument check failed
if paramError.Code != api.ErrorSuccess().Code {
response := api.NewResponse(paramError)
response.ServeHTTP(w, r)
logError(response)
return
}
request.Param = parameters
/* (5) Search a matching handler
---------------------------------------------------------*/
var serviceHandler *api.Handler
var serviceFound bool
for _, handler := range s.handlers {
if handler.GetPath() == servicePath {
serviceFound = true
if handler.GetMethod() == r.Method {
serviceHandler = handler
}
}
}
// fail if found no handler
if serviceHandler == nil {
if serviceFound {
response := api.NewResponse()
response.SetError(api.ErrorUncallableMethod(), servicePath, r.Method)
response.ServeHTTP(w, r)
logError(response)
return
}
response := api.NewResponse()
response.SetError(api.ErrorUncallableService(), servicePath)
response.ServeHTTP(w, r)
logError(response)
return
}
/* (6) Execute handler and return response
---------------------------------------------------------*/
// 1. feed request with configuration scope
request.Scope = methodDef.Scope
// 1. execute
response := api.NewResponse()
serviceHandler.Handle(*request, response)
// 2. apply headers
for key, values := range response.Headers {
for _, value := range values {
w.Header().Add(key, value)
}
}
// 3. write to response
response.ServeHTTP(w, r)
return
}
// HandleFunc sets a new handler for an HTTP method to a path // HandleFunc sets a new handler for an HTTP method to a path
func (s *Server) HandleFunc(httpMethod, path string, handlerFunc api.HandlerFunc) { func (s *Server) HandleFunc(httpMethod, path string, handlerFunc api.HandlerFunc) {
handler := api.NewHandler(httpMethod, path, handlerFunc) handler := api.NewHandler(httpMethod, path, handlerFunc)
@ -161,3 +63,16 @@ func (s *Server) HandleFunc(httpMethod, path string, handlerFunc api.HandlerFunc
func (s *Server) Handle(handler *api.Handler) { func (s *Server) Handle(handler *api.Handler) {
s.handlers = append(s.handlers, handler) s.handlers = append(s.handlers, handler)
} }
// HTTP converts the server to a http server
func (s Server) HTTP() httpServer {
// 1. log available handlers
log.Printf("=== Mapped handlers ===\n")
for i := 0; i < len(s.handlers); i++ {
log.Printf("* [rest] %s\t'%s'\n", s.handlers[i].GetMethod(), s.handlers[i].GetPath())
}
// 2. cast to http server
return httpServer(s)
}

View File

@ -17,7 +17,28 @@ func (Bool) Checker(typeName string) typecheck.CheckerFunc {
return nil return nil
} }
return func(value interface{}) bool { return func(value interface{}) bool {
_, isBool := value.(bool) _, isBool := readBool(value)
return isBool return isBool
} }
} }
// readBool tries to read a serialized boolean and returns whether it succeeded.
func readBool(value interface{}) (bool, bool) {
switch cast := value.(type) {
case bool:
return cast, true
case string:
strVal := string(cast)
return strVal == "true", strVal == "true" || strVal == "false"
case []byte:
strVal := string(cast)
return strVal == "true", strVal == "true" || strVal == "false"
default:
return false, false
}
return false, false
}

View File

@ -0,0 +1,115 @@
package builtin_test
import (
"fmt"
"testing"
"git.xdrm.io/go/aicra/typecheck/builtin"
)
func TestBool_New(t *testing.T) {
t.Parallel()
inst := interface{}(builtin.NewBool())
switch cast := inst.(type) {
case *builtin.Bool:
return
default:
t.Errorf("expect %T ; got %T", &builtin.Bool{}, cast)
}
}
func TestBool_AvailableTypes(t *testing.T) {
t.Parallel()
inst := builtin.NewBool()
tests := []struct {
Type string
Handled bool
}{
{"bool", true},
{"Bool", false},
{"boolean", false},
{" bool", false},
{"bool ", false},
{" bool ", false},
}
for _, test := range tests {
t.Run(test.Type, func(t *testing.T) {
checker := inst.Checker(test.Type)
if checker == nil {
if test.Handled {
t.Errorf("expect %q to be handled", test.Type)
t.Fail()
}
return
}
if !test.Handled {
t.Errorf("expect %q NOT to be handled", test.Type)
t.Fail()
}
})
}
}
func TestBool_Values(t *testing.T) {
t.Parallel()
const typeName = "bool"
checker := builtin.NewBool().Checker(typeName)
if checker == nil {
t.Errorf("expect %q to be handled", typeName)
t.Fail()
}
tests := []struct {
Value interface{}
Valid bool
}{
{true, true},
{false, true},
{1, false},
{0, false},
{-1, false},
// json number
{"-1", false},
{"0", false},
{"1", false},
// json string
{"true", true},
{"false", true},
{[]byte("true"), true},
{[]byte("false"), true},
{"string", false},
{[]byte("bytes"), false},
{-0.1, false},
{0.1, false},
{nil, false},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if checker(test.Value) {
if !test.Valid {
t.Errorf("expect value to be invalid")
t.Fail()
}
return
}
if test.Valid {
t.Errorf("expect value to be valid")
t.Fail()
}
})
}
}

View File

@ -45,6 +45,11 @@ func readFloat(value interface{}) (float64, bool) {
floatVal, err := num.Float64() floatVal, err := num.Float64()
return floatVal, err == nil return floatVal, err == nil
case []byte:
num := json.Number(cast)
floatVal, err := num.Float64()
return floatVal, err == nil
// unknown type // unknown type
default: default:
return 0, false return 0, false

View File

@ -0,0 +1,127 @@
package builtin_test
import (
"fmt"
"math"
"testing"
"git.xdrm.io/go/aicra/typecheck/builtin"
)
func TestFloat64_New(t *testing.T) {
t.Parallel()
inst := interface{}(builtin.NewFloat64())
switch cast := inst.(type) {
case *builtin.Float64:
return
default:
t.Errorf("expect %T ; got %T", &builtin.Float64{}, cast)
}
}
func TestFloat64_AvailableTypes(t *testing.T) {
t.Parallel()
inst := builtin.NewFloat64()
tests := []struct {
Type string
Handled bool
}{
{"float", true},
{"float64", true},
{"Float", false},
{"Float64", false},
{"FLOAT", false},
{"FLOAT64", false},
{" float", false},
{"float ", false},
{" float ", false},
{" float64", false},
{"float64 ", false},
{" float64 ", false},
}
for _, test := range tests {
t.Run(test.Type, func(t *testing.T) {
checker := inst.Checker(test.Type)
if checker == nil {
if test.Handled {
t.Errorf("expect %q to be handled", test.Type)
t.Fail()
}
return
}
if !test.Handled {
t.Errorf("expect %q NOT to be handled", test.Type)
t.Fail()
}
})
}
}
func TestFloat64_Values(t *testing.T) {
t.Parallel()
const typeName = "float"
checker := builtin.NewFloat64().Checker(typeName)
if checker == nil {
t.Errorf("expect %q to be handled", typeName)
t.Fail()
}
tests := []struct {
Value interface{}
Valid bool
}{
{uint(0), true},
{uint(math.MaxInt64), true},
{uint(math.MaxUint64), true},
{-1, true},
{-math.MaxInt64, true},
{float64(math.MinInt64), true},
{float64(0), true},
{float64(math.MaxInt64), true},
// we cannot just compare because of how precision works
{float64(math.MaxUint64 - 1024), true},
{float64(math.MaxUint64 + 1), true},
// json number
{fmt.Sprintf("%f", -math.MaxFloat64), true},
{"-1", true},
{"0", true},
{"1", true},
{fmt.Sprintf("%d", math.MaxInt64), true},
{fmt.Sprintf("%d", uint(math.MaxUint64)), true},
{fmt.Sprintf("%f", float64(math.MaxFloat64)), true},
{"string", false},
{[]byte("bytes"), false},
{-0.1, true},
{0.1, true},
{nil, false},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if checker(test.Value) {
if !test.Valid {
t.Errorf("expect value to be invalid")
t.Fail()
}
return
}
if test.Valid {
t.Errorf("expect value to be valid")
t.Fail()
}
})
}
}

View File

@ -49,6 +49,12 @@ func readInt(value interface{}) (int, bool) {
num := json.Number(cast) num := json.Number(cast)
intVal, err := num.Int64() intVal, err := num.Int64()
return int(intVal), err == nil return int(intVal), err == nil
// serialized string -> try to convert to float
case []byte:
num := json.Number(cast)
intVal, err := num.Int64()
return int(intVal), err == nil
// unknown type // unknown type
default: default:

View File

@ -61,6 +61,12 @@ func (s String) Checker(typeName string) typecheck.CheckerFunc {
// check type // check type
strValue, isString := value.(string) strValue, isString := value.(string)
byteSliceValue, isByteSlice := value.([]byte)
if !isString && isByteSlice {
strValue = string(byteSliceValue)
isString = true
}
if !isString { if !isString {
return false return false
} }

View File

@ -87,7 +87,7 @@ func TestString_AnyLength(t *testing.T) {
Valid bool Valid bool
}{ }{
{"string", true}, {"string", true},
{[]byte("bytes"), false}, {[]byte("bytes"), true},
{1, false}, {1, false},
{0.1, false}, {0.1, false},
{nil, false}, {nil, false},

View File

@ -53,6 +53,15 @@ func readUint(value interface{}) (uint, bool) {
overflows := floatVal < 0 || floatVal > math.MaxUint64 overflows := floatVal < 0 || floatVal > math.MaxUint64
return uint(floatVal), !overflows return uint(floatVal), !overflows
case []byte:
num := json.Number(cast)
floatVal, err := num.Float64()
if err != nil {
return 0, false
}
overflows := floatVal < 0 || floatVal > math.MaxUint64
return uint(floatVal), !overflows
// unknown type // unknown type
default: default:
return 0, false return 0, false

24
util.go
View File

@ -2,6 +2,7 @@ package aicra
import ( import (
"log" "log"
"net/http"
"git.xdrm.io/go/aicra/api" "git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/internal/config"
@ -10,7 +11,7 @@ import (
// extractParameters extracts parameters for the request and checks // extractParameters extracts parameters for the request and checks
// every single one according to configuration options // every single one according to configuration options
func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) { func (s *httpServer) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) {
// init vars // init vars
parameters := make(map[string]interface{}) parameters := make(map[string]interface{})
@ -77,7 +78,28 @@ func (s *Server) extractParameters(store *reqdata.Store, methodParam map[string]
return parameters, api.ErrorSuccess() return parameters, api.ErrorSuccess()
} }
var handledMethods = []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete}
// Prints an error as HTTP response // Prints an error as HTTP response
func logError(res *api.Response) { func logError(res *api.Response) {
log.Printf("[http.fail] %v\n", res) log.Printf("[http.fail] %v\n", res)
} }
// logService logs a service details
func logService(s config.Service, path string) {
for _, method := range handledMethods {
if m := s.Method(method); m != nil {
if path == "" {
log.Printf("* [rest] %s\t'/'\n", method)
} else {
log.Printf("* [rest] %s\t'%s'\n", method, path)
}
}
}
if s.Children != nil {
for subPath, child := range s.Children {
logService(*child, path+"/"+subPath)
}
}
}