Compare commits
No commits in common. "4877d0ea23fa71ded2c0186702ffecbc9e984aef" and "cb7f22e03d8e8a54780a301431cd2df6fac086f9" have entirely different histories.
4877d0ea23
...
cb7f22e03d
|
@ -76,7 +76,7 @@ var errorReasons = map[Error]string{
|
||||||
ErrorUnknown: "unknown error",
|
ErrorUnknown: "unknown error",
|
||||||
ErrorSuccess: "all right",
|
ErrorSuccess: "all right",
|
||||||
ErrorFailure: "it failed",
|
ErrorFailure: "it failed",
|
||||||
ErrorNoMatchFound: "resource not found",
|
ErrorNoMatchFound: "resource found",
|
||||||
ErrorAlreadyExists: "already exists",
|
ErrorAlreadyExists: "already exists",
|
||||||
ErrorConfig: "configuration error",
|
ErrorConfig: "configuration error",
|
||||||
ErrorUpload: "upload failed",
|
ErrorUpload: "upload failed",
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandlerFn defines the handler signature
|
||||||
|
type HandlerFn func(req Request, res *Response) Error
|
||||||
|
|
||||||
|
// Handler is an API handler ready to be bound
|
||||||
|
type Handler struct {
|
||||||
|
path string
|
||||||
|
method string
|
||||||
|
Fn HandlerFn
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHandler builds a handler from its http method and path
|
||||||
|
func NewHandler(method, path string, fn HandlerFn) (*Handler, error) {
|
||||||
|
return &Handler{
|
||||||
|
path: path,
|
||||||
|
method: strings.ToUpper(method),
|
||||||
|
Fn: fn,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMethod returns the handler's HTTP method
|
||||||
|
func (h *Handler) GetMethod() string {
|
||||||
|
return h.method
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPath returns the handler's path
|
||||||
|
func (h *Handler) GetPath() string {
|
||||||
|
return h.path
|
||||||
|
}
|
|
@ -1,19 +1,10 @@
|
||||||
package builtin
|
package builtin
|
||||||
|
|
||||||
import (
|
import "git.xdrm.io/go/aicra/datatype"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AnyDataType is what its name tells
|
// AnyDataType is what its name tells
|
||||||
type AnyDataType struct{}
|
type AnyDataType struct{}
|
||||||
|
|
||||||
// Type returns the type of data
|
|
||||||
func (AnyDataType) Type() reflect.Type {
|
|
||||||
return reflect.TypeOf(interface{}(nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build returns the validator
|
// Build returns the validator
|
||||||
func (AnyDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
func (AnyDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||||
// nothing if type not handled
|
// nothing if type not handled
|
||||||
|
|
|
@ -1,19 +1,10 @@
|
||||||
package builtin
|
package builtin
|
||||||
|
|
||||||
import (
|
import "git.xdrm.io/go/aicra/datatype"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BoolDataType is what its name tells
|
// BoolDataType is what its name tells
|
||||||
type BoolDataType struct{}
|
type BoolDataType struct{}
|
||||||
|
|
||||||
// Type returns the type of data
|
|
||||||
func (BoolDataType) Type() reflect.Type {
|
|
||||||
return reflect.TypeOf(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build returns the validator
|
// Build returns the validator
|
||||||
func (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
func (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||||
// nothing if type not handled
|
// nothing if type not handled
|
||||||
|
|
|
@ -2,7 +2,6 @@ package builtin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
"git.xdrm.io/go/aicra/datatype"
|
||||||
)
|
)
|
||||||
|
@ -10,11 +9,6 @@ import (
|
||||||
// FloatDataType is what its name tells
|
// FloatDataType is what its name tells
|
||||||
type FloatDataType struct{}
|
type FloatDataType struct{}
|
||||||
|
|
||||||
// Type returns the type of data
|
|
||||||
func (FloatDataType) Type() reflect.Type {
|
|
||||||
return reflect.TypeOf(float64(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build returns the validator
|
// Build returns the validator
|
||||||
func (FloatDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
func (FloatDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||||
// nothing if type not handled
|
// nothing if type not handled
|
||||||
|
|
|
@ -3,7 +3,6 @@ package builtin
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
"git.xdrm.io/go/aicra/datatype"
|
||||||
)
|
)
|
||||||
|
@ -11,11 +10,6 @@ import (
|
||||||
// IntDataType is what its name tells
|
// IntDataType is what its name tells
|
||||||
type IntDataType struct{}
|
type IntDataType struct{}
|
||||||
|
|
||||||
// Type returns the type of data
|
|
||||||
func (IntDataType) Type() reflect.Type {
|
|
||||||
return reflect.TypeOf(int(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build returns the validator
|
// Build returns the validator
|
||||||
func (IntDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
func (IntDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||||
// nothing if type not handled
|
// nothing if type not handled
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package builtin
|
package builtin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -14,11 +13,6 @@ var variableLengthRegex = regexp.MustCompile(`^string\((\d+), ?(\d+)\)$`)
|
||||||
// StringDataType is what its name tells
|
// StringDataType is what its name tells
|
||||||
type StringDataType struct{}
|
type StringDataType struct{}
|
||||||
|
|
||||||
// Type returns the type of data
|
|
||||||
func (StringDataType) Type() reflect.Type {
|
|
||||||
return reflect.TypeOf(string(""))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build returns the validator.
|
// Build returns the validator.
|
||||||
// availables type names are : `string`, `string(length)` and `string(minLength, maxLength)`.
|
// availables type names are : `string`, `string(length)` and `string(minLength, maxLength)`.
|
||||||
func (s StringDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
func (s StringDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package builtin
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
"git.xdrm.io/go/aicra/datatype"
|
||||||
)
|
)
|
||||||
|
@ -11,11 +10,6 @@ import (
|
||||||
// UintDataType is what its name tells
|
// UintDataType is what its name tells
|
||||||
type UintDataType struct{}
|
type UintDataType struct{}
|
||||||
|
|
||||||
// Type returns the type of data
|
|
||||||
func (UintDataType) Type() reflect.Type {
|
|
||||||
return reflect.TypeOf(uint(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build returns the validator
|
// Build returns the validator
|
||||||
func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||||
// nothing if type not handled
|
// nothing if type not handled
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package datatype
|
package datatype
|
||||||
|
|
||||||
import "reflect"
|
|
||||||
|
|
||||||
// Validator returns whether a given value fulfills a datatype
|
// Validator returns whether a given value fulfills a datatype
|
||||||
// and casts the value into a compatible type
|
// and casts the value into a compatible type
|
||||||
type Validator func(value interface{}) (cast interface{}, valid bool)
|
type Validator func(value interface{}) (cast interface{}, valid bool)
|
||||||
|
@ -10,6 +8,5 @@ type Validator func(value interface{}) (cast interface{}, valid bool)
|
||||||
// definition does not match this T ; the registry is passed for recursive datatypes (e.g. slices, structs, etc)
|
// definition does not match this T ; the registry is passed for recursive datatypes (e.g. slices, structs, etc)
|
||||||
// to be able to access other datatypes
|
// to be able to access other datatypes
|
||||||
type T interface {
|
type T interface {
|
||||||
Type() reflect.Type
|
|
||||||
Build(typeDefinition string, registry ...T) Validator
|
Build(typeDefinition string, registry ...T) Validator
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
package dynamic
|
|
||||||
|
|
||||||
// cerr allows you to create constant "const" error with type boxing.
|
|
||||||
type cerr string
|
|
||||||
|
|
||||||
// Error implements the error builtin interface.
|
|
||||||
func (err cerr) Error() string {
|
|
||||||
return string(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrHandlerNotFunc - handler is not a func
|
|
||||||
const ErrHandlerNotFunc = cerr("handler must be a func")
|
|
||||||
|
|
||||||
// ErrNoServiceForHandler - no service matching this handler
|
|
||||||
const ErrNoServiceForHandler = cerr("no service found for this handler")
|
|
||||||
|
|
||||||
// ErrMissingHandlerArgumentParam - missing params arguments for handler
|
|
||||||
const ErrMissingHandlerArgumentParam = cerr("missing handler argument : parameter struct")
|
|
||||||
|
|
||||||
// ErrMissingHandlerOutput - missing output for handler
|
|
||||||
const ErrMissingHandlerOutput = cerr("handler must have at least 1 output")
|
|
||||||
|
|
||||||
// ErrMissingHandlerOutputError - missing error output for handler
|
|
||||||
const ErrMissingHandlerOutputError = cerr("handler must have its last output of type api.Error")
|
|
||||||
|
|
||||||
// ErrMissingRequestArgument - missing request argument for handler
|
|
||||||
const ErrMissingRequestArgument = cerr("handler first argument must be of type api.Request")
|
|
||||||
|
|
||||||
// ErrMissingParamArgument - missing parameters argument for handler
|
|
||||||
const ErrMissingParamArgument = cerr("handler second argument must be a struct")
|
|
||||||
|
|
||||||
// ErrMissingParamOutput - missing output argument for handler
|
|
||||||
const ErrMissingParamOutput = cerr("handler first output must be a *struct")
|
|
||||||
|
|
||||||
// ErrMissingParamFromConfig - missing a parameter in handler struct
|
|
||||||
const ErrMissingParamFromConfig = cerr("missing a parameter from configuration")
|
|
||||||
|
|
||||||
// ErrMissingOutputFromConfig - missing a parameter in handler struct
|
|
||||||
const ErrMissingOutputFromConfig = cerr("missing a parameter from configuration")
|
|
||||||
|
|
||||||
// ErrWrongParamTypeFromConfig - a configuration parameter type is invalid in the handler param struct
|
|
||||||
const ErrWrongParamTypeFromConfig = cerr("invalid struct field type")
|
|
||||||
|
|
||||||
// ErrWrongOutputTypeFromConfig - a configuration output type is invalid in the handler output struct
|
|
||||||
const ErrWrongOutputTypeFromConfig = cerr("invalid struct field type")
|
|
||||||
|
|
||||||
// ErrMissingHandlerErrorOutput - missing handler output error
|
|
||||||
const ErrMissingHandlerErrorOutput = cerr("last output must be of type api.Error")
|
|
|
@ -1,90 +0,0 @@
|
||||||
package dynamic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/api"
|
|
||||||
"git.xdrm.io/go/aicra/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Build a handler from a service configuration and a HandlerFn
|
|
||||||
//
|
|
||||||
// a HandlerFn must have as a signature : `func(api.Request, inputStruct) (outputStruct, api.Error)`
|
|
||||||
// - `inputStruct` is a struct{} containing a field for each service input (with valid reflect.Type)
|
|
||||||
// - `outputStruct` is a struct{} containing a field for each service output (with valid reflect.Type)
|
|
||||||
//
|
|
||||||
// Special cases:
|
|
||||||
// - it there is no input, `inputStruct` can be omitted
|
|
||||||
// - it there is no output, `outputStruct` can be omitted
|
|
||||||
func Build(fn HandlerFn, service config.Service) (*Handler, error) {
|
|
||||||
h := &Handler{
|
|
||||||
spec: makeSpec(service),
|
|
||||||
fn: fn,
|
|
||||||
}
|
|
||||||
|
|
||||||
fnv := reflect.ValueOf(fn)
|
|
||||||
|
|
||||||
if fnv.Type().Kind() != reflect.Func {
|
|
||||||
return nil, ErrHandlerNotFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := h.spec.checkInput(fnv); err != nil {
|
|
||||||
return nil, fmt.Errorf("input: %w", err)
|
|
||||||
}
|
|
||||||
if err := h.spec.checkOutput(fnv); err != nil {
|
|
||||||
return nil, fmt.Errorf("output: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return h, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle binds input @data into HandleFn and returns map output
|
|
||||||
func (h *Handler) Handle(data map[string]interface{}) (map[string]interface{}, api.Error) {
|
|
||||||
fnv := reflect.ValueOf(h.fn)
|
|
||||||
|
|
||||||
callArgs := []reflect.Value{}
|
|
||||||
|
|
||||||
// bind input data
|
|
||||||
if fnv.Type().NumIn() > 0 {
|
|
||||||
// create zero value struct
|
|
||||||
callStructPtr := reflect.New(fnv.Type().In(0))
|
|
||||||
callStruct := callStructPtr.Elem()
|
|
||||||
|
|
||||||
// set each field
|
|
||||||
for name := range h.spec.Input {
|
|
||||||
field := callStruct.FieldByName(name)
|
|
||||||
if !field.CanSet() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// get value from @data
|
|
||||||
value, inData := data[name]
|
|
||||||
if !inData {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
field.Set(reflect.ValueOf(value).Convert(field.Type()))
|
|
||||||
}
|
|
||||||
callArgs = append(callArgs, callStruct)
|
|
||||||
}
|
|
||||||
|
|
||||||
// call the HandlerFn
|
|
||||||
output := fnv.Call(callArgs)
|
|
||||||
|
|
||||||
// no output OR pointer to output struct is nil
|
|
||||||
outdata := make(map[string]interface{})
|
|
||||||
if len(h.spec.Output) < 1 || output[0].IsNil() {
|
|
||||||
return outdata, api.Error(output[len(output)-1].Int())
|
|
||||||
}
|
|
||||||
|
|
||||||
// extract struct from pointer
|
|
||||||
returnStruct := output[0].Elem()
|
|
||||||
|
|
||||||
for name := range h.spec.Output {
|
|
||||||
field := returnStruct.FieldByName(name)
|
|
||||||
outdata[name] = field.Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
// extract api.Error
|
|
||||||
return outdata, api.Error(output[len(output)-1].Int())
|
|
||||||
}
|
|
119
dynamic/spec.go
119
dynamic/spec.go
|
@ -1,119 +0,0 @@
|
||||||
package dynamic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/api"
|
|
||||||
"git.xdrm.io/go/aicra/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// builds a spec from the configuration service
|
|
||||||
func makeSpec(service config.Service) spec {
|
|
||||||
spec := spec{
|
|
||||||
Input: make(map[string]reflect.Type),
|
|
||||||
Output: make(map[string]reflect.Type),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, param := range service.Input {
|
|
||||||
// make a pointer if optional
|
|
||||||
if param.Optional {
|
|
||||||
spec.Input[param.Rename] = reflect.PtrTo(param.ExtractType)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
spec.Input[param.Rename] = param.ExtractType
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, param := range service.Output {
|
|
||||||
spec.Output[param.Rename] = param.ExtractType
|
|
||||||
}
|
|
||||||
|
|
||||||
return spec
|
|
||||||
}
|
|
||||||
|
|
||||||
// checks for HandlerFn input arguments
|
|
||||||
func (s spec) checkInput(fnv reflect.Value) error {
|
|
||||||
fnt := fnv.Type()
|
|
||||||
|
|
||||||
// no input -> ok
|
|
||||||
if len(s.Input) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if fnt.NumIn() != 1 {
|
|
||||||
return ErrMissingHandlerArgumentParam
|
|
||||||
}
|
|
||||||
|
|
||||||
// arg must be a struct
|
|
||||||
structArg := fnt.In(0)
|
|
||||||
if structArg.Kind() != reflect.Struct {
|
|
||||||
return ErrMissingParamArgument
|
|
||||||
}
|
|
||||||
|
|
||||||
// check for invlaid param
|
|
||||||
for name, ptype := range s.Input {
|
|
||||||
field, exists := structArg.FieldByName(name)
|
|
||||||
if !exists {
|
|
||||||
return fmt.Errorf("%s: %w", name, ErrMissingParamFromConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ptype.AssignableTo(field.Type) {
|
|
||||||
return fmt.Errorf("%s: %w (%s instead of %s)", name, ErrWrongParamTypeFromConfig, field.Type, ptype)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// checks for HandlerFn output arguments
|
|
||||||
func (s spec) checkOutput(fnv reflect.Value) error {
|
|
||||||
fnt := fnv.Type()
|
|
||||||
if fnt.NumOut() < 1 {
|
|
||||||
return ErrMissingHandlerOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
// last output must be api.Error
|
|
||||||
errOutput := fnt.Out(fnt.NumOut() - 1)
|
|
||||||
if !errOutput.AssignableTo(reflect.TypeOf(api.ErrorUnknown)) {
|
|
||||||
return ErrMissingHandlerErrorOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
// no output -> ok
|
|
||||||
if len(s.Output) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if fnt.NumOut() != 2 {
|
|
||||||
return ErrMissingParamOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
// fail if first output is not a pointer to struct
|
|
||||||
structOutputPtr := fnt.Out(0)
|
|
||||||
if structOutputPtr.Kind() != reflect.Ptr {
|
|
||||||
return ErrMissingParamOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
structOutput := structOutputPtr.Elem()
|
|
||||||
if structOutput.Kind() != reflect.Struct {
|
|
||||||
return ErrMissingParamOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
// fail on invalid output
|
|
||||||
for name, ptype := range s.Output {
|
|
||||||
field, exists := structOutput.FieldByName(name)
|
|
||||||
if !exists {
|
|
||||||
return fmt.Errorf("%s: %w", name, ErrMissingOutputFromConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignore types evalutating to nil
|
|
||||||
if ptype == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ptype.ConvertibleTo(field.Type) {
|
|
||||||
return fmt.Errorf("%s: %w (%s instead of %s)", name, ErrWrongOutputTypeFromConfig, field.Type, ptype)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
package dynamic
|
|
||||||
|
|
||||||
import "reflect"
|
|
||||||
|
|
||||||
// HandlerFn defines a dynamic handler function
|
|
||||||
type HandlerFn interface{}
|
|
||||||
|
|
||||||
// Handler represents a dynamic api handler
|
|
||||||
type Handler struct {
|
|
||||||
spec spec
|
|
||||||
fn HandlerFn
|
|
||||||
}
|
|
||||||
|
|
||||||
type spec struct {
|
|
||||||
Input map[string]reflect.Type
|
|
||||||
Output map[string]reflect.Type
|
|
||||||
}
|
|
15
errors.go
15
errors.go
|
@ -1,15 +0,0 @@
|
||||||
package aicra
|
|
||||||
|
|
||||||
// cerr allows you to create constant "const" error with type boxing.
|
|
||||||
type cerr string
|
|
||||||
|
|
||||||
// Error implements the error builtin interface.
|
|
||||||
func (err cerr) Error() string {
|
|
||||||
return string(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrNoServiceForHandler - no service matching this handler
|
|
||||||
const ErrNoServiceForHandler = cerr("no service found for this handler")
|
|
||||||
|
|
||||||
// ErrNoHandlerForService - no handler matching this service
|
|
||||||
const ErrNoHandlerForService = cerr("no handler found for this service")
|
|
32
handler.go
32
handler.go
|
@ -1,32 +0,0 @@
|
||||||
package aicra
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/dynamic"
|
|
||||||
"git.xdrm.io/go/aicra/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type handler struct {
|
|
||||||
Method string
|
|
||||||
Path string
|
|
||||||
dynHandler *dynamic.Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
// createHandler builds a handler from its http method and path
|
|
||||||
// also it checks whether the function signature is valid
|
|
||||||
func createHandler(method, path string, service config.Service, fn dynamic.HandlerFn) (*handler, error) {
|
|
||||||
method = strings.ToUpper(method)
|
|
||||||
|
|
||||||
dynHandler, err := dynamic.Build(fn, service)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s '%s' handler: %w", method, path, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &handler{
|
|
||||||
Path: path,
|
|
||||||
Method: method,
|
|
||||||
dynHandler: dynHandler,
|
|
||||||
}, nil
|
|
||||||
}
|
|
18
http.go
18
http.go
|
@ -55,11 +55,11 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. find a matching handler
|
// 6. find a matching handler
|
||||||
var foundHandler *handler
|
var foundHandler *api.Handler
|
||||||
var found bool
|
var found bool
|
||||||
|
|
||||||
for _, handler := range server.handlers {
|
for _, handler := range server.handlers {
|
||||||
if handler.Method == service.Method && handler.Path == service.Pattern {
|
if handler.GetMethod() == service.Method && handler.GetPath() == service.Pattern {
|
||||||
foundHandler = handler
|
foundHandler = handler
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
|
@ -91,17 +91,9 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
apireq.Param = dataset.Data
|
apireq.Param = dataset.Data
|
||||||
|
|
||||||
// 10. execute
|
// 10. execute
|
||||||
returned, apiErr := foundHandler.dynHandler.Handle(dataset.Data)
|
response := api.EmptyResponse()
|
||||||
response := api.EmptyResponse().WithError(apiErr)
|
apiErr := foundHandler.Fn(*apireq, response)
|
||||||
for key, value := range returned {
|
response.WithError(apiErr)
|
||||||
|
|
||||||
// find original name from rename
|
|
||||||
for name, param := range service.Output {
|
|
||||||
if param.Rename == key {
|
|
||||||
response.SetData(name, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 11. apply headers
|
// 11. apply headers
|
||||||
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
|
|
@ -481,46 +481,6 @@ func TestParseParameters(t *testing.T) {
|
||||||
]`,
|
]`,
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
// missing rename
|
|
||||||
{
|
|
||||||
`[
|
|
||||||
{
|
|
||||||
"method": "GET",
|
|
||||||
"path": "/{uri}",
|
|
||||||
"info": "info",
|
|
||||||
"in": {
|
|
||||||
"{uri}": { "info": "valid", "type": "any" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]`,
|
|
||||||
ErrMandatoryRename,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
`[
|
|
||||||
{
|
|
||||||
"method": "GET",
|
|
||||||
"path": "/",
|
|
||||||
"info": "info",
|
|
||||||
"in": {
|
|
||||||
"GET@abc": { "info": "valid", "type": "any" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]`,
|
|
||||||
ErrMandatoryRename,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
`[
|
|
||||||
{
|
|
||||||
"method": "GET",
|
|
||||||
"path": "/",
|
|
||||||
"info": "info",
|
|
||||||
"in": {
|
|
||||||
"GET@abc": { "info": "valid", "type": "any", "name": "abc" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]`,
|
|
||||||
nil,
|
|
||||||
},
|
|
||||||
|
|
||||||
{ // URI parameter
|
{ // URI parameter
|
||||||
`[
|
`[
|
||||||
|
@ -656,7 +616,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "string", "name": "c" }
|
"{c}": { "info":"info", "type": "string" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -669,7 +629,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -682,7 +642,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}/d",
|
{ "method": "GET", "path": "/a/{c}/d",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "string", "name": "c" }
|
"{c}": { "info":"info", "type": "string" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -695,7 +655,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "string", "name": "c" }
|
"{c}": { "info":"info", "type": "string" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -708,7 +668,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "string", "name": "c" }
|
"{c}": { "info":"info", "type": "string" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -721,7 +681,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}/d",
|
{ "method": "GET", "path": "/a/{c}/d",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "string", "name": "c" }
|
"{c}": { "info":"info", "type": "string" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -734,7 +694,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -747,7 +707,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -760,7 +720,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}/d",
|
{ "method": "GET", "path": "/a/{c}/d",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -773,7 +733,7 @@ func TestServiceCollision(t *testing.T) {
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}/d",
|
{ "method": "GET", "path": "/a/{c}/d",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -783,12 +743,12 @@ func TestServiceCollision(t *testing.T) {
|
||||||
`[
|
`[
|
||||||
{ "method": "GET", "path": "/a/{b}",
|
{ "method": "GET", "path": "/a/{b}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{b}": { "info":"info", "type": "uint", "name": "b" }
|
"{b}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ "method": "GET", "path": "/a/{c}",
|
{ "method": "GET", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -798,12 +758,12 @@ func TestServiceCollision(t *testing.T) {
|
||||||
`[
|
`[
|
||||||
{ "method": "GET", "path": "/a/{b}",
|
{ "method": "GET", "path": "/a/{b}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{b}": { "info":"info", "type": "uint", "name": "b" }
|
"{b}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ "method": "PUT", "path": "/a/{c}",
|
{ "method": "PUT", "path": "/a/{c}",
|
||||||
"info": "info", "in": {
|
"info": "info", "in": {
|
||||||
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
"{c}": { "info":"info", "type": "uint" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]`,
|
]`,
|
||||||
|
@ -890,8 +850,7 @@ func TestMatchSimple(t *testing.T) {
|
||||||
"in": {
|
"in": {
|
||||||
"{id}": {
|
"{id}": {
|
||||||
"info": "info",
|
"info": "info",
|
||||||
"type": "bool",
|
"type": "bool"
|
||||||
"name": "id"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} ]`,
|
} ]`,
|
||||||
|
@ -906,8 +865,7 @@ func TestMatchSimple(t *testing.T) {
|
||||||
"in": {
|
"in": {
|
||||||
"{id}": {
|
"{id}": {
|
||||||
"info": "info",
|
"info": "info",
|
||||||
"type": "int",
|
"type": "int"
|
||||||
"name": "id"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} ]`,
|
} ]`,
|
||||||
|
@ -922,8 +880,7 @@ func TestMatchSimple(t *testing.T) {
|
||||||
"in": {
|
"in": {
|
||||||
"{valid}": {
|
"{valid}": {
|
||||||
"info": "info",
|
"info": "info",
|
||||||
"type": "bool",
|
"type": "bool"
|
||||||
"name": "valid"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} ]`,
|
} ]`,
|
||||||
|
@ -938,8 +895,7 @@ func TestMatchSimple(t *testing.T) {
|
||||||
"in": {
|
"in": {
|
||||||
"{valid}": {
|
"{valid}": {
|
||||||
"info": "info",
|
"info": "info",
|
||||||
"type": "bool",
|
"type": "bool"
|
||||||
"name": "valid"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} ]`,
|
} ]`,
|
||||||
|
|
|
@ -29,9 +29,6 @@ const ErrInvalidPatternBraceCapture = cerr("invalid uri capturing braces")
|
||||||
// ErrUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern
|
// ErrUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern
|
||||||
const ErrUnspecifiedBraceCapture = cerr("capturing brace missing in the path")
|
const ErrUnspecifiedBraceCapture = cerr("capturing brace missing in the path")
|
||||||
|
|
||||||
// ErrMandatoryRename - capture/query parameters must have a rename
|
|
||||||
const ErrMandatoryRename = cerr("capture and query parameters must have a 'name'")
|
|
||||||
|
|
||||||
// ErrUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters
|
// ErrUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters
|
||||||
const ErrUndefinedBraceCapture = cerr("capturing brace missing input definition")
|
const ErrUndefinedBraceCapture = cerr("capturing brace missing input definition")
|
||||||
|
|
||||||
|
@ -41,9 +38,6 @@ const ErrMissingDescription = cerr("missing description")
|
||||||
// ErrIllegalOptionalURIParam - an URI parameter cannot be optional
|
// ErrIllegalOptionalURIParam - an URI parameter cannot be optional
|
||||||
const ErrIllegalOptionalURIParam = cerr("URI parameter cannot be optional")
|
const ErrIllegalOptionalURIParam = cerr("URI parameter cannot be optional")
|
||||||
|
|
||||||
// ErrOptionalOption - an output is optional
|
|
||||||
const ErrOptionalOption = cerr("output cannot be optional")
|
|
||||||
|
|
||||||
// ErrMissingParamDesc - a parameter is missing its description
|
// ErrMissingParamDesc - a parameter is missing its description
|
||||||
const ErrMissingParamDesc = cerr("missing parameter description")
|
const ErrMissingParamDesc = cerr("missing parameter description")
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import "git.xdrm.io/go/aicra/datatype"
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Validate implements the validator interface
|
// Validate implements the validator interface
|
||||||
func (param *Parameter) Validate(datatypes ...datatype.T) error {
|
func (param *Parameter) Validate(datatypes ...datatype.T) error {
|
||||||
|
@ -22,17 +20,5 @@ func (param *Parameter) Validate(datatypes ...datatype.T) error {
|
||||||
param.Type = param.Type[1:]
|
param.Type = param.Type[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign the datatype
|
|
||||||
for _, dtype := range datatypes {
|
|
||||||
param.Validator = dtype.Build(param.Type, datatypes...)
|
|
||||||
param.ExtractType = dtype.Type()
|
|
||||||
if param.Validator != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if param.Validator == nil {
|
|
||||||
return ErrUnknownDataType
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,12 +108,6 @@ func (svc *Service) Validate(datatypes ...datatype.T) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check output
|
|
||||||
err = svc.validateOutput(datatypes)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("field 'out': %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +183,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail if brace capture does not exists in pattern
|
// fail if brace capture does not exists in pattern
|
||||||
var iscapture, isquery bool
|
iscapture := false
|
||||||
if matches := braceRegex.FindAllStringSubmatch(paramName, -1); len(matches) > 0 && len(matches[0]) > 1 {
|
if matches := braceRegex.FindAllStringSubmatch(paramName, -1); len(matches) > 0 && len(matches[0]) > 1 {
|
||||||
braceName := matches[0][1]
|
braceName := matches[0][1]
|
||||||
|
|
||||||
|
@ -215,7 +209,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
svc.Query = make(map[string]*Parameter)
|
svc.Query = make(map[string]*Parameter)
|
||||||
}
|
}
|
||||||
svc.Query[queryName] = param
|
svc.Query[queryName] = param
|
||||||
isquery = true
|
|
||||||
} else {
|
} else {
|
||||||
if svc.Form == nil {
|
if svc.Form == nil {
|
||||||
svc.Form = make(map[string]*Parameter)
|
svc.Form = make(map[string]*Parameter)
|
||||||
|
@ -223,17 +217,12 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
svc.Form[paramName] = param
|
svc.Form[paramName] = param
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail if capture or query without rename
|
|
||||||
if len(param.Rename) < 1 && (iscapture || isquery) {
|
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrMandatoryRename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// use param name if no rename
|
// use param name if no rename
|
||||||
if len(param.Rename) < 1 {
|
if len(param.Rename) < 1 {
|
||||||
param.Rename = paramName
|
param.Rename = paramName
|
||||||
}
|
}
|
||||||
|
|
||||||
err := param.Validate(types...)
|
err := param.Validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s: %w", paramName, err)
|
return fmt.Errorf("%s: %w", paramName, err)
|
||||||
}
|
}
|
||||||
|
@ -243,7 +232,20 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrIllegalOptionalURIParam)
|
return fmt.Errorf("%s: %w", paramName, ErrIllegalOptionalURIParam)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fail on name/rename conflict
|
// assign the datatype
|
||||||
|
datatypeFound := false
|
||||||
|
for _, dtype := range types {
|
||||||
|
param.Validator = dtype.Build(param.Type, types...)
|
||||||
|
if param.Validator != nil {
|
||||||
|
datatypeFound = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !datatypeFound {
|
||||||
|
return fmt.Errorf("%s: %w", paramName, ErrUnknownDataType)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for name/rename conflict
|
||||||
for paramName2, param2 := range svc.Input {
|
for paramName2, param2 := range svc.Input {
|
||||||
// ignore self
|
// ignore self
|
||||||
if paramName == paramName2 {
|
if paramName == paramName2 {
|
||||||
|
@ -263,52 +265,3 @@ func (svc *Service) validateInput(types []datatype.T) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *Service) validateOutput(types []datatype.T) error {
|
|
||||||
|
|
||||||
// ignore no parameter
|
|
||||||
if svc.Output == nil || len(svc.Output) < 1 {
|
|
||||||
svc.Output = make(map[string]*Parameter, 0)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// for each parameter
|
|
||||||
for paramName, param := range svc.Output {
|
|
||||||
if len(paramName) < 1 {
|
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrIllegalParamName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// use param name if no rename
|
|
||||||
if len(param.Rename) < 1 {
|
|
||||||
param.Rename = paramName
|
|
||||||
}
|
|
||||||
|
|
||||||
err := param.Validate(types...)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%s: %w", paramName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if param.Optional {
|
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrOptionalOption)
|
|
||||||
}
|
|
||||||
|
|
||||||
// fail on name/rename conflict
|
|
||||||
for paramName2, param2 := range svc.Output {
|
|
||||||
// ignore self
|
|
||||||
if paramName == paramName2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3.2.1. Same rename field
|
|
||||||
// 3.2.2. Not-renamed field matches a renamed field
|
|
||||||
// 3.2.3. Renamed field matches name
|
|
||||||
if param.Rename == param2.Rename || paramName == param2.Rename || paramName2 == param.Rename {
|
|
||||||
return fmt.Errorf("%s: %w", paramName, ErrParamNameConflict)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
"git.xdrm.io/go/aicra/datatype"
|
||||||
)
|
)
|
||||||
|
@ -27,7 +26,8 @@ type Service struct {
|
||||||
Scope [][]string `json:"scope"`
|
Scope [][]string `json:"scope"`
|
||||||
Description string `json:"info"`
|
Description string `json:"info"`
|
||||||
Input map[string]*Parameter `json:"in"`
|
Input map[string]*Parameter `json:"in"`
|
||||||
Output map[string]*Parameter `json:"out"`
|
// Download *bool `json:"download"`
|
||||||
|
// Output map[string]*Parameter `json:"out"`
|
||||||
|
|
||||||
// references to url parameters
|
// references to url parameters
|
||||||
// format: '/uri/{param}'
|
// format: '/uri/{param}'
|
||||||
|
@ -46,8 +46,6 @@ type Parameter struct {
|
||||||
Description string `json:"info"`
|
Description string `json:"info"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Rename string `json:"name,omitempty"`
|
Rename string `json:"name,omitempty"`
|
||||||
// ExtractType is the type of data the datatype returns
|
|
||||||
ExtractType reflect.Type
|
|
||||||
// Optional is set to true when the type is prefixed with '?'
|
// Optional is set to true when the type is prefixed with '?'
|
||||||
Optional bool
|
Optional bool
|
||||||
|
|
||||||
|
|
31
server.go
31
server.go
|
@ -5,15 +5,15 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.xdrm.io/go/aicra/api"
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
"git.xdrm.io/go/aicra/datatype"
|
||||||
"git.xdrm.io/go/aicra/dynamic"
|
|
||||||
"git.xdrm.io/go/aicra/internal/config"
|
"git.xdrm.io/go/aicra/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server represents an AICRA instance featuring: type checkers, services
|
// Server represents an AICRA instance featuring: type checkers, services
|
||||||
type Server struct {
|
type Server struct {
|
||||||
config *config.Server
|
config *config.Server
|
||||||
handlers []*handler
|
handlers []*api.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a framework instance from a configuration file
|
// New creates a framework instance from a configuration file
|
||||||
|
@ -26,7 +26,7 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) {
|
||||||
// 1. init instance
|
// 1. init instance
|
||||||
var i = &Server{
|
var i = &Server{
|
||||||
config: nil,
|
config: nil,
|
||||||
handlers: make([]*handler, 0),
|
handlers: make([]*api.Handler, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. open config file
|
// 2. open config file
|
||||||
|
@ -46,26 +46,13 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle 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) Handle(method, path string, fn dynamic.HandlerFn) error {
|
func (s *Server) Handle(httpMethod, path string, fn api.HandlerFn) {
|
||||||
// find associated service
|
handler, err := api.NewHandler(httpMethod, path, fn)
|
||||||
var found *config.Service = nil
|
|
||||||
for _, service := range s.config.Services {
|
|
||||||
if method == service.Method && path == service.Pattern {
|
|
||||||
found = service
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if found == nil {
|
|
||||||
return fmt.Errorf("%s '%s': %w", method, path, ErrNoServiceForHandler)
|
|
||||||
}
|
|
||||||
|
|
||||||
handler, err := createHandler(method, path, *found, fn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
panic(err)
|
||||||
}
|
}
|
||||||
s.handlers = append(s.handlers, handler)
|
s.handlers = append(s.handlers, handler)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToHTTPServer converts the server to a http server
|
// ToHTTPServer converts the server to a http server
|
||||||
|
@ -75,13 +62,13 @@ func (s Server) ToHTTPServer() (*httpServer, error) {
|
||||||
for _, service := range s.config.Services {
|
for _, service := range s.config.Services {
|
||||||
found := false
|
found := false
|
||||||
for _, handler := range s.handlers {
|
for _, handler := range s.handlers {
|
||||||
if handler.Method == service.Method && handler.Path == service.Pattern {
|
if handler.GetMethod() == service.Method && handler.GetPath() == service.Pattern {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
return nil, fmt.Errorf("%s '%s': %w", service.Method, service.Pattern, ErrNoHandlerForService)
|
return nil, fmt.Errorf("missing handler for %s '%s'", service.Method, service.Pattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue