aicra/server.go

164 lines
3.7 KiB
Go
Raw Normal View History

package aicra
import (
2019-05-01 19:30:16 +00:00
"io"
2019-04-30 22:02:28 +00:00
"log"
"net/http"
"os"
2019-04-30 22:02:28 +00:00
"strings"
"git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/internal/config"
"git.xdrm.io/go/aicra/internal/reqdata"
checker "git.xdrm.io/go/aicra/typecheck"
)
// Server represents an AICRA instance featuring: type checkers, services
type Server struct {
services *config.Service
2019-05-01 13:14:49 +00:00
Checkers *checker.Set
handlers []*api.Handler
}
// New creates a framework instance from a configuration file
func New(configPath string) (*Server, error) {
2019-05-01 19:30:16 +00:00
var (
err error
configFile io.ReadCloser
)
// 1. init instance
var i = &Server{
services: nil,
2019-05-01 13:14:49 +00:00
Checkers: checker.New(),
handlers: make([]*api.Handler, 0),
}
// 2. open config file
2019-05-01 19:30:16 +00:00
configFile, err = os.Open(configPath)
if err != nil {
return nil, err
}
defer configFile.Close()
// 3. load configuration
i.services, err = config.Parse(configFile)
if err != nil {
return nil, err
}
return i, nil
}
// ServeHTTP implements http.Handler and has to be called on each request
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
2018-10-02 15:14:44 +00:00
// 1. build API request from HTTP request
request, err := api.NewRequest(r)
2018-07-07 17:21:00 +00:00
if err != nil {
log.Fatal(err)
}
// 2. find a matching service for this path in the config
serviceDef, pathIndex := s.services.Browse(request.URI)
if serviceDef == nil {
return
}
servicePath := strings.Join(request.URI[:pathIndex], "/")
if !strings.HasPrefix(servicePath, "/") {
servicePath = "/" + servicePath
}
2019-05-01 13:56:18 +00:00
// 3. check if matching methodDef exists in config */
var methodDef = serviceDef.Method(r.Method)
2019-05-01 13:56:18 +00:00
if methodDef == nil {
response := api.NewResponse(api.ErrorUnknownMethod())
response.ServeHTTP(w, r)
logError(response)
return
}
// 4. parse every input data from the request
store := reqdata.New(request.URI[pathIndex:], r)
/* (4) Check parameters
---------------------------------------------------------*/
2019-05-01 13:56:18 +00:00
parameters, paramError := s.extractParameters(store, methodDef.Parameters)
2018-07-07 17:21:00 +00:00
// Fail if argument check failed
if paramError.Code != api.ErrorSuccess().Code {
response := api.NewResponse(paramError)
response.ServeHTTP(w, r)
logError(response)
2018-07-07 17:21:00 +00:00
return
}
request.Param = parameters
/* (5) Search a matching handler
---------------------------------------------------------*/
var serviceHandler *api.Handler
var serviceFound bool
for _, handler := range s.handlers {
if handler.GetPath() == servicePath {
serviceFound = true
if handler.GetMethod() == r.Method {
serviceHandler = handler
}
}
}
// fail if found no handler
if serviceHandler == nil {
if serviceFound {
response := api.NewResponse()
response.SetError(api.ErrorUncallableMethod(), servicePath, r.Method)
response.ServeHTTP(w, r)
logError(response)
return
}
response := api.NewResponse()
response.SetError(api.ErrorUncallableService(), servicePath)
response.ServeHTTP(w, r)
logError(response)
2018-07-07 17:21:00 +00:00
return
}
/* (6) Execute handler and return response
2018-07-07 17:21:00 +00:00
---------------------------------------------------------*/
2019-05-01 13:56:18 +00:00
// 1. feed request with configuration scope
request.Scope = methodDef.Scope
2019-05-01 13:56:18 +00:00
// 1. execute
response := api.NewResponse()
serviceHandler.Handle(*request, response)
// 2. apply headers
for key, values := range response.Headers {
for _, value := range values {
w.Header().Add(key, value)
2018-07-07 17:21:00 +00:00
}
}
// 3. write to response
response.ServeHTTP(w, r)
2018-07-07 17:21:00 +00:00
return
}
// HandleFunc sets a new handler for an HTTP method to a path
func (s *Server) HandleFunc(httpMethod, path string, handlerFunc api.HandlerFunc) {
handler := api.NewHandler(httpMethod, path, handlerFunc)
s.handlers = append(s.handlers, handler)
}
// Handle sets a new handler
func (s *Server) Handle(handler *api.Handler) {
s.handlers = append(s.handlers, handler)
}