2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-21 15:53:19 +00:00
|
|
|
"fmt"
|
2019-05-01 19:30:16 +00:00
|
|
|
"io"
|
2019-05-01 11:44:45 +00:00
|
|
|
"os"
|
2019-04-30 22:02:28 +00:00
|
|
|
|
2020-03-21 13:49:36 +00:00
|
|
|
"git.xdrm.io/go/aicra/datatype"
|
2020-03-29 13:04:12 +00:00
|
|
|
"git.xdrm.io/go/aicra/dynamic"
|
2018-10-01 15:43:18 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
2018-05-21 10:02:24 +00:00
|
|
|
)
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// Server represents an AICRA instance featuring: type checkers, services
|
2018-07-10 23:36:42 +00:00
|
|
|
type Server struct {
|
2020-03-21 13:49:36 +00:00
|
|
|
config *config.Server
|
2020-03-29 13:04:12 +00:00
|
|
|
handlers []*handler
|
2018-07-10 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 22:32:19 +00:00
|
|
|
// New creates a framework instance from a configuration file
|
2020-03-21 13:49:36 +00:00
|
|
|
func New(configPath string, dtypes ...datatype.T) (*Server, error) {
|
2019-05-01 19:30:16 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
configFile io.ReadCloser
|
|
|
|
)
|
2018-09-28 13:58:30 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 1. init instance
|
2018-07-10 23:51:10 +00:00
|
|
|
var i = &Server{
|
2020-03-03 17:36:52 +00:00
|
|
|
config: nil,
|
2020-03-29 13:04:12 +00:00
|
|
|
handlers: make([]*handler, 0),
|
2018-07-10 23:51:10 +00:00
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 2. open config file
|
2019-05-01 19:30:16 +00:00
|
|
|
configFile, err = os.Open(configPath)
|
2018-07-06 08:49:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-01 11:44:45 +00:00
|
|
|
defer configFile.Close()
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 3. load configuration
|
2020-03-21 13:49:36 +00:00
|
|
|
i.config, err = config.Parse(configFile, dtypes...)
|
2019-05-01 11:44:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-10-01 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 17:03:37 +00:00
|
|
|
return i, nil
|
2018-07-07 17:21:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:04:12 +00:00
|
|
|
// Handle sets a new handler for an HTTP method to a path
|
|
|
|
func (s *Server) Handle(method, path string, fn dynamic.HandlerFn) error {
|
|
|
|
// find associated service
|
|
|
|
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)
|
2020-03-28 13:57:28 +00:00
|
|
|
if err != nil {
|
2020-03-29 13:04:12 +00:00
|
|
|
return err
|
2020-03-28 13:57:28 +00:00
|
|
|
}
|
2019-05-01 11:44:45 +00:00
|
|
|
s.handlers = append(s.handlers, handler)
|
2020-03-29 13:04:12 +00:00
|
|
|
return nil
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
2019-09-26 17:03:37 +00:00
|
|
|
|
2020-03-21 15:53:19 +00:00
|
|
|
// ToHTTPServer converts the server to a http server
|
|
|
|
func (s Server) ToHTTPServer() (*httpServer, error) {
|
2019-09-26 17:03:37 +00:00
|
|
|
|
2020-03-21 15:53:19 +00:00
|
|
|
// check if handlers are missing
|
|
|
|
for _, service := range s.config.Services {
|
|
|
|
found := false
|
|
|
|
for _, handler := range s.handlers {
|
2020-03-29 13:04:12 +00:00
|
|
|
if handler.Method == service.Method && handler.Path == service.Pattern {
|
2020-03-21 15:53:19 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-03-29 13:04:12 +00:00
|
|
|
return nil, fmt.Errorf("%s '%s': %w", service.Method, service.Pattern, ErrNoHandlerForService)
|
2020-03-21 15:53:19 +00:00
|
|
|
}
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. cast to http server
|
2020-03-21 15:53:19 +00:00
|
|
|
httpServer := httpServer(s)
|
|
|
|
return &httpServer, nil
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|