clarity rename: dynamic package to dynfunc
This commit is contained in:
parent
eb690cf862
commit
b1498e59c1
|
@ -1,4 +1,4 @@
|
||||||
package dynamic
|
package dynfunc
|
||||||
|
|
||||||
// cerr allows you to create constant "const" error with type boxing.
|
// cerr allows you to create constant "const" error with type boxing.
|
||||||
type cerr string
|
type cerr string
|
|
@ -1,4 +1,4 @@
|
||||||
package dynamic
|
package dynfunc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package dynamic
|
package dynfunc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package dynamic
|
package dynfunc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
|
@ -1,4 +1,4 @@
|
||||||
package dynamic
|
package dynfunc
|
||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|
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 interface{}) (*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
|
|
||||||
}
|
|
34
server.go
34
server.go
|
@ -6,13 +6,20 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.xdrm.io/go/aicra/datatype"
|
"git.xdrm.io/go/aicra/datatype"
|
||||||
|
"git.xdrm.io/go/aicra/dynfunc"
|
||||||
"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 []*apiHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
type apiHandler struct {
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
dynHandler *dynfunc.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a framework instance from a configuration file
|
// New creates a framework instance from a configuration file
|
||||||
|
@ -48,22 +55,29 @@ 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 interface{}) error {
|
func (s *Server) Handle(method, path string, fn interface{}) error {
|
||||||
// find associated service
|
// find associated service
|
||||||
var found *config.Service = nil
|
var service *config.Service
|
||||||
for _, service := range s.config.Services {
|
for _, s := range s.config.Services {
|
||||||
if method == service.Method && path == service.Pattern {
|
if method == s.Method && path == s.Pattern {
|
||||||
found = service
|
service = s
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if found == nil {
|
|
||||||
return fmt.Errorf("%s '%s': %w", method, path, ErrNoServiceForHandler)
|
if service == nil {
|
||||||
|
return fmt.Errorf("%s '%s': %w", method, path, ErrUnknownService)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler, err := createHandler(method, path, *found, fn)
|
dynHandler, err := dynfunc.Build(fn, *service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("%s '%s' handler: %w", method, path, err)
|
||||||
}
|
}
|
||||||
s.handlers = append(s.handlers, handler)
|
|
||||||
|
s.handlers = append(s.handlers, &apiHandler{
|
||||||
|
Path: path,
|
||||||
|
Method: method,
|
||||||
|
dynHandler: dynHandler,
|
||||||
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue