test dynfunc package; standardize and refactor api #14
|
@ -8,16 +8,16 @@ import (
|
||||||
"git.xdrm.io/go/aicra/internal/config"
|
"git.xdrm.io/go/aicra/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Build a handler from a service configuration and a HandlerFn
|
// Build a handler from a service configuration and a dynamic function
|
||||||
//
|
//
|
||||||
// a HandlerFn must have as a signature : `func(api.Request, inputStruct) (outputStruct, api.Error)`
|
// @fn must have as a signature : `func(inputStruct) (*outputStruct, api.Error)`
|
||||||
// - `inputStruct` is a struct{} containing a field for each service input (with valid reflect.Type)
|
// - `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)
|
// - `outputStruct` is a struct{} containing a field for each service output (with valid reflect.Type)
|
||||||
//
|
//
|
||||||
// Special cases:
|
// Special cases:
|
||||||
// - it there is no input, `inputStruct` can be omitted
|
// - it there is no input, `inputStruct` must be omitted
|
||||||
// - it there is no output, `outputStruct` can be omitted
|
// - it there is no output, `outputStruct` must be omitted
|
||||||
func Build(fn HandlerFn, service config.Service) (*Handler, error) {
|
func Build(fn interface{}, service config.Service) (*Handler, error) {
|
||||||
h := &Handler{
|
h := &Handler{
|
||||||
spec: makeSpec(service),
|
spec: makeSpec(service),
|
||||||
fn: fn,
|
fn: fn,
|
||||||
|
@ -39,7 +39,7 @@ func Build(fn HandlerFn, service config.Service) (*Handler, error) {
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle binds input @data into HandleFn and returns map output
|
// Handle binds input @data into the dynamic function and returns map output
|
||||||
func (h *Handler) Handle(data map[string]interface{}) (map[string]interface{}, api.Error) {
|
func (h *Handler) Handle(data map[string]interface{}) (map[string]interface{}, api.Error) {
|
||||||
fnv := reflect.ValueOf(h.fn)
|
fnv := reflect.ValueOf(h.fn)
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,10 @@ package dynamic
|
||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|
||||||
// HandlerFn defines a dynamic handler function
|
|
||||||
type HandlerFn interface{}
|
|
||||||
|
|
||||||
// Handler represents a dynamic api handler
|
// Handler represents a dynamic api handler
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
spec spec
|
spec spec
|
||||||
fn HandlerFn
|
fn interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type spec struct {
|
type spec struct {
|
||||||
|
|
|
@ -16,7 +16,7 @@ type handler struct {
|
||||||
|
|
||||||
// createHandler builds a handler from its http method and path
|
// createHandler builds a handler from its http method and path
|
||||||
// also it checks whether the function signature is valid
|
// also it checks whether the function signature is valid
|
||||||
func createHandler(method, path string, service config.Service, fn dynamic.HandlerFn) (*handler, error) {
|
func createHandler(method, path string, service config.Service, fn interface{}) (*handler, error) {
|
||||||
method = strings.ToUpper(method)
|
method = strings.ToUpper(method)
|
||||||
|
|
||||||
dynHandler, err := dynamic.Build(fn, service)
|
dynHandler, err := dynamic.Build(fn, service)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,7 +46,7 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle sets a new handler for an HTTP method to a path
|
// Handle 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(method, path string, fn interface{}) error {
|
||||||
// find associated service
|
// find associated service
|
||||||
var found *config.Service = nil
|
var found *config.Service = nil
|
||||||
for _, service := range s.config.Services {
|
for _, service := range s.config.Services {
|
||||||
|
|
Loading…
Reference in New Issue