update readme + comment

This commit is contained in:
Adrien Marquès 2018-09-13 10:21:35 +02:00
parent e13732c381
commit f76e3d35fa
7 changed files with 26 additions and 13 deletions

View File

@ -6,7 +6,7 @@
[![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)
**Aicra** is a self-working framework coded in *Go* that allows anyone to create a fully featured REST API. It features type checking, authentication management through middlewares, file upload, rich argument parsing (*i.e. url slash-separated, urlencoded, form-data, json*), nested routes, project builder (*i.e. aicra*), etc. **Aicra** is a self-working framework coded in *Go* that allows anyone to create a fully featured REST API. It features type checking, authentication management through middlewares, file upload, rich argument parsing (*i.e. url slash-separated, urlencoded, form-data, json*), nested routes, project compiler (*i.e. aicra*), etc.
@ -40,7 +40,7 @@ This framework is based over some of the following concepts.
#### I. Installation #### I. Installation
You need a recent machine with `go` installed. 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**.
@ -66,7 +66,7 @@ go install git.xdrm.io/go/aicra/cmd/aicra
> 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 symlink into `/usr/local/bin` or the PATH folder of your choice for less characters to type. > 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 symlink into `/usr/local/bin` for instance.
@ -105,17 +105,17 @@ The whole project behavior is described inside the `manifest.json` file. For a b
##### 2. Controllers ##### 2. Controllers
For each route, you'll have to place your implementation into the `controller` folder following the naming convention : add `/i.go` at the end of the route. For each route, you'll have to place your implementation into the `controller` folder following the naming convention : add `/main.go` at the end of the route.
> Example - `/path/to/some/uri` will be inside `controller/path/to/some/uri/i.go`. > Example - `/path/to/some/uri` will be inside `controller/path/to/some/uri/main.go`.
A fully working example is available [here](https://git.xdrm.io/example/aicra). A sample directory structure is available [here](https://git.xdrm.io/example/aicra/src/master/controller).
##### 3. Middlewares ##### 3. Middlewares
In order for your project to manage authentication, the best solution is to create middlewares, there are programs that updates a *Scope* according to internal or persistent (*i.e.* database) information and the actual http request. They are all run before each request it routed by aicra. The scope are used to match the `scope` field in the configuration file and automatically block non-authed requests. Scopes can also be used for implementation-specific behavior. In order for your project to manage authentication, the best solution is to create middlewares, there are programs that updates a *Scope* 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 scope are used to match the `scope` field in the configuration file and automatically block non-authenticated requests. Scopes can also be used for implementation-specific behavior.

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
) )
// NewDataset creates an empty request dataset
func NewDataset() *DataSet { func NewDataset() *DataSet {
return &DataSet{ return &DataSet{
URI: make([]*Parameter, 0), URI: make([]*Parameter, 0),

View File

@ -1,5 +1,6 @@
package apirequest package apirequest
// Request represents a request by its URI, controller path and data (uri, get, post)
type Request struct { type Request struct {
// corresponds to the list of uri components // corresponds to the list of uri components
// featuring in the request URI // featuring in the request URI
@ -12,6 +13,10 @@ type Request struct {
Data *DataSet Data *DataSet
} }
// DataSet represents all data that can be caught:
// - URI (guessed from the URI by removing the controller path)
// - GET (default url data)
// - POST (from json, form-data, url-encoded)
type DataSet struct { type DataSet struct {
// ordered values from the URI // ordered values from the URI

View File

@ -8,6 +8,7 @@ import (
var min *uint64 var min *uint64
var max *uint64 var max *uint64
// Match filters the parameter type format "varchar(min, max)"
func Match(name string) bool { func Match(name string) bool {
/* (1) Create regexp */ /* (1) Create regexp */
@ -45,6 +46,7 @@ func Match(name string) bool {
} }
// Check whether the given value fulfills the condition (min, max)
func Check(value interface{}) bool { func Check(value interface{}) bool {
/* (1) Check if string */ /* (1) Check if string */

View File

@ -5,8 +5,8 @@ import (
"strings" "strings"
) )
var title_index = 0 var titleIndex = 0
var align_offset = 30 var alignOffset = 30
// Warn returns a red warning ASCII sign. If a string is given // Warn returns a red warning ASCII sign. If a string is given
// as argument, it will print it after the warning sign // as argument, it will print it after the warning sign
@ -30,8 +30,8 @@ func Info(s ...string) string {
// Title prints a formatted title (auto-indexed from local counted) // Title prints a formatted title (auto-indexed from local counted)
func Title(s string) { func Title(s string) {
title_index++ titleIndex++
fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), title_index, s, Color(33, "<<", false)) fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false))
} }
@ -51,7 +51,7 @@ func Align(s string) {
size += len(strings.Split(s, "\033[0m")) - 1 size += len(strings.Split(s, "\033[0m")) - 1
// 4. print trailing spaces // 4. print trailing spaces
for i := size; i < align_offset; i++ { for i := size; i < alignOffset; i++ {
fmt.Printf(" ") fmt.Printf(" ")
} }
} }

View File

@ -4,6 +4,7 @@ import (
"git.xdrm.io/go/aicra/err" "git.xdrm.io/go/aicra/err"
) )
// New creates an empty response
func New() *Response { func New() *Response {
return &Response{ return &Response{
data: make(map[string]interface{}), data: make(map[string]interface{}),
@ -11,16 +12,19 @@ func New() *Response {
} }
} }
// Set adds/overrides a new response field
func (i *Response) Set(name string, value interface{}) { func (i *Response) Set(name string, value interface{}) {
i.data[name] = value i.data[name] = value
} }
// Get gets a reponse field
func (i *Response) Get(name string) interface{} { func (i *Response) Get(name string) interface{} {
value, _ := i.data[name] value, _ := i.data[name]
return value return value
} }
// Dump gets all key/value pairs
func (i *Response) Dump() map[string]interface{} { func (i *Response) Dump() map[string]interface{} {
return i.data return i.data
} }

View File

@ -4,9 +4,10 @@ import (
"git.xdrm.io/go/aicra/err" "git.xdrm.io/go/aicra/err"
) )
// Arguments contains all key-value arguments
type Arguments map[string]interface{} type Arguments map[string]interface{}
type Controller func(Arguments, *Response) Response
// Response represents an API response to be sent
type Response struct { type Response struct {
data map[string]interface{} data map[string]interface{}
Err err.Error Err err.Error