feat: add scope to request
This commit is contained in:
parent
cd2bcdd8bc
commit
98ae854251
|
@ -5,15 +5,15 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RequestParam defines input parameters of an api request
|
|
||||||
type RequestParam map[string]interface{}
|
|
||||||
|
|
||||||
// Request represents an API request i.e. HTTP
|
// Request represents an API request i.e. HTTP
|
||||||
type Request struct {
|
type Request struct {
|
||||||
// corresponds to the list of uri components
|
// corresponds to the list of uri components
|
||||||
// featured in the request URI
|
// featured in the request URI
|
||||||
URI []string
|
URI []string
|
||||||
|
|
||||||
|
// Scope from the configuration file of the current service
|
||||||
|
Scope [][]string
|
||||||
|
|
||||||
// original HTTP request
|
// original HTTP request
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ func NewRequest(req *http.Request) (*Request, error) {
|
||||||
// 3. Init request
|
// 3. Init request
|
||||||
inst := &Request{
|
inst := &Request{
|
||||||
URI: uriparts,
|
URI: uriparts,
|
||||||
|
Scope: nil,
|
||||||
Request: req,
|
Request: req,
|
||||||
Param: make(RequestParam),
|
Param: make(RequestParam),
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
17
server.go
17
server.go
|
@ -45,12 +45,6 @@ func New(configPath string) (*Server, error) {
|
||||||
return nil, err
|
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
|
return i, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -73,9 +67,9 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
servicePath := strings.Join(apiRequest.URI[:pathIndex], "/")
|
servicePath := strings.Join(apiRequest.URI[:pathIndex], "/")
|
||||||
|
|
||||||
// 3. check if matching method exists in config */
|
// 3. check if matching methodDef exists in config */
|
||||||
var method = serviceDef.Method(req.Method)
|
var methodDef = serviceDef.Method(req.Method)
|
||||||
if method == nil {
|
if methodDef == nil {
|
||||||
httpError(res, api.ErrorUnknownMethod())
|
httpError(res, api.ErrorUnknownMethod())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -85,7 +79,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
/* (4) Check parameters
|
/* (4) Check parameters
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
parameters, paramError := s.extractParameters(store, method.Parameters)
|
parameters, paramError := s.extractParameters(store, methodDef.Parameters)
|
||||||
|
|
||||||
// Fail if argument check failed
|
// Fail if argument check failed
|
||||||
if paramError.Code != api.ErrorSuccess().Code {
|
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
|
/* (6) Execute handler and return response
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
|
// 1. feed request with configuration scope
|
||||||
|
apiRequest.Scope = methodDef.Permission
|
||||||
|
|
||||||
// 1. execute
|
// 1. execute
|
||||||
apiResponse := api.NewResponse()
|
apiResponse := api.NewResponse()
|
||||||
serviceHandler.Handle(*apiRequest, apiResponse)
|
serviceHandler.Handle(*apiRequest, apiResponse)
|
||||||
|
|
Loading…
Reference in New Issue