diff --git a/api/request.go b/api/request.go index 6fd8cc2..0461ed8 100644 --- a/api/request.go +++ b/api/request.go @@ -5,15 +5,15 @@ import ( "strings" ) -// RequestParam defines input parameters of an api request -type RequestParam map[string]interface{} - // Request represents an API request i.e. HTTP type Request struct { // corresponds to the list of uri components // featured in the request URI URI []string + // Scope from the configuration file of the current service + Scope [][]string + // original HTTP request Request *http.Request @@ -31,6 +31,7 @@ func NewRequest(req *http.Request) (*Request, error) { // 3. Init request inst := &Request{ URI: uriparts, + Scope: nil, Request: req, Param: make(RequestParam), } diff --git a/api/request.param.go b/api/request.param.go new file mode 100644 index 0000000..7eff378 --- /dev/null +++ b/api/request.param.go @@ -0,0 +1,33 @@ +package api + +// ConstError is a wrapper to set constant errors +type ConstError string + +// Error implements error +func (err ConstError) Error() string { + return string(err) +} + +// ErrReqParamNotFound is thrown when a request parameter is not found +const ErrReqParamNotFound = ConstError("request parameter not found") + +// ErrReqParamNotType is thrown when a request parameter is not asked with the right type +const ErrReqParamNotType = ConstError("request parameter does not fulfills type") + +// RequestParam defines input parameters of an api request +type RequestParam map[string]interface{} + +// GetString returns a string and an error if not found or string +func (rp RequestParam) GetString(key string) (string, error) { + rawValue, found := rp[key] + if !found { + return "", ErrReqParamNotFound + } + + convertedValue, canConvert := rawValue.(string) + if !canConvert { + return "", ErrReqParamNotType + } + + return convertedValue, nil +} diff --git a/server.go b/server.go index 18b7584..9ab6d85 100644 --- a/server.go +++ b/server.go @@ -45,12 +45,6 @@ func New(configPath string) (*Server, error) { return nil, err } - /* 3. Load type registry */ - // TODO: add methods on the checker to set types programmatically - - /* 4. Load middleware registry */ - // TODO: add methods to set them manually - return i, nil } @@ -73,9 +67,9 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { } servicePath := strings.Join(apiRequest.URI[:pathIndex], "/") - // 3. check if matching method exists in config */ - var method = serviceDef.Method(req.Method) - if method == nil { + // 3. check if matching methodDef exists in config */ + var methodDef = serviceDef.Method(req.Method) + if methodDef == nil { httpError(res, api.ErrorUnknownMethod()) return } @@ -85,7 +79,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { /* (4) Check parameters ---------------------------------------------------------*/ - parameters, paramError := s.extractParameters(store, method.Parameters) + parameters, paramError := s.extractParameters(store, methodDef.Parameters) // Fail if argument check failed if paramError.Code != api.ErrorSuccess().Code { @@ -121,6 +115,9 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { /* (6) Execute handler and return response ---------------------------------------------------------*/ + // 1. feed request with configuration scope + apiRequest.Scope = methodDef.Permission + // 1. execute apiResponse := api.NewResponse() serviceHandler.Handle(*apiRequest, apiResponse)