fix: semantic (comments) renaming : 'controller' to 'service', more explicit names
This commit is contained in:
parent
6afedf190f
commit
189d3b40a6
|
@ -1,6 +1,4 @@
|
||||||
*.so
|
*.so
|
||||||
/types
|
|
||||||
/controllers
|
|
||||||
/test
|
/test
|
||||||
/aicra
|
/aicra
|
||||||
/.vscode
|
/.vscode
|
|
@ -1,7 +1,7 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrorSuccess represents a generic successful controller execution
|
// ErrorSuccess represents a generic successful service execution
|
||||||
ErrorSuccess = func() Error { return Error{0, "all right", nil} }
|
ErrorSuccess = func() Error { return Error{0, "all right", nil} }
|
||||||
|
|
||||||
// ErrorFailure is the most generic error
|
// ErrorFailure is the most generic error
|
||||||
|
@ -29,16 +29,16 @@ var (
|
||||||
ErrorDownload = func() Error { return Error{101, "download failed", nil} }
|
ErrorDownload = func() Error { return Error{101, "download failed", nil} }
|
||||||
|
|
||||||
// MissingDownloadHeaders has to be set when the implementation
|
// MissingDownloadHeaders has to be set when the implementation
|
||||||
// of a controller of type 'download' (which returns a file instead of
|
// of a service of type 'download' (which returns a file instead of
|
||||||
// a set or output fields) is missing its HEADER field
|
// a set or output fields) is missing its HEADER field
|
||||||
MissingDownloadHeaders = func() Error { return Error{102, "download headers are missing", nil} }
|
MissingDownloadHeaders = func() Error { return Error{102, "download headers are missing", nil} }
|
||||||
|
|
||||||
// ErrorMissingDownloadBody has to be set when the implementation
|
// ErrorMissingDownloadBody has to be set when the implementation
|
||||||
// of a controller of type 'download' (which returns a file instead of
|
// of a service of type 'download' (which returns a file instead of
|
||||||
// a set or output fields) is missing its BODY field
|
// a set or output fields) is missing its BODY field
|
||||||
ErrorMissingDownloadBody = func() Error { return Error{103, "download body is missing", nil} }
|
ErrorMissingDownloadBody = func() Error { return Error{103, "download body is missing", nil} }
|
||||||
|
|
||||||
// ErrorUnknownService is set when there is no controller matching
|
// ErrorUnknownService is set when there is no service matching
|
||||||
// the http request URI.
|
// the http request URI.
|
||||||
ErrorUnknownService = func() Error { return Error{200, "unknown service", nil} }
|
ErrorUnknownService = func() Error { return Error{200, "unknown service", nil} }
|
||||||
|
|
||||||
|
@ -46,11 +46,11 @@ var (
|
||||||
// request's http method
|
// request's http method
|
||||||
ErrorUnknownMethod = func() Error { return Error{201, "unknown method", nil} }
|
ErrorUnknownMethod = func() Error { return Error{201, "unknown method", nil} }
|
||||||
|
|
||||||
// ErrorUncallableService is set when there the requested controller's
|
// ErrorUncallableService is set when there the requested service's
|
||||||
// implementation (plugin file) is not found/callable
|
// implementation (plugin file) is not found/callable
|
||||||
ErrorUncallableService = func() Error { return Error{202, "uncallable service", nil} }
|
ErrorUncallableService = func() Error { return Error{202, "uncallable service", nil} }
|
||||||
|
|
||||||
// ErrorUncallableMethod is set when there the requested controller's
|
// ErrorUncallableMethod is set when there the requested service's
|
||||||
// implementation does not features the requested method
|
// implementation does not features the requested method
|
||||||
ErrorUncallableMethod = func() Error { return Error{203, "uncallable method", nil} }
|
ErrorUncallableMethod = func() Error { return Error{203, "uncallable method", nil} }
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Error represents an http response error following the api format.
|
// Error represents an http response error following the api format.
|
||||||
// These are used by the controllers to set the *execution status*
|
// These are used by the services to set the *execution status*
|
||||||
// directly into the response as JSON alongside response output fields.
|
// directly into the response as JSON alongside response output fields.
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
|
|
|
@ -86,12 +86,12 @@ func (svc *Service) checkAndFormat(servicePath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. for each controller */
|
// 2. for each service */
|
||||||
for childService, ctl := range svc.Children {
|
for childService, ctl := range svc.Children {
|
||||||
|
|
||||||
// 3. invalid name */
|
// 3. invalid name */
|
||||||
if strings.ContainsAny(childService, "/-") {
|
if strings.ContainsAny(childService, "/-") {
|
||||||
return fmt.Errorf("Controller '%s' must not contain any slash '/' nor '-' symbols", childService)
|
return fmt.Errorf("service '%s' must not contain any slash '/' nor '-' symbols", childService)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. check recursively */
|
// 4. check recursively */
|
||||||
|
|
|
@ -5,7 +5,7 @@ package reqdata
|
||||||
type Parameter struct {
|
type Parameter struct {
|
||||||
// whether the value has been json-parsed
|
// whether the value has been json-parsed
|
||||||
// for optimisation purpose, parameters are only parsed
|
// for optimisation purpose, parameters are only parsed
|
||||||
// if they are required by the current controller
|
// if they are required by the current service
|
||||||
Parsed bool
|
Parsed bool
|
||||||
|
|
||||||
// whether the value is a file
|
// whether the value is a file
|
||||||
|
|
|
@ -12,13 +12,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Store represents all data that can be caught:
|
// Store represents all data that can be caught:
|
||||||
// - URI (guessed from the URI by removing the controller path)
|
// - URI (guessed from the URI by removing the service path)
|
||||||
// - GET (default url data)
|
// - GET (default url data)
|
||||||
// - POST (from json, form-data, url-encoded)
|
// - POST (from json, form-data, url-encoded)
|
||||||
type Store struct {
|
type Store struct {
|
||||||
|
|
||||||
// ordered values from the URI
|
// ordered values from the URI
|
||||||
// catches all after the controller path
|
// catches all after the service path
|
||||||
//
|
//
|
||||||
// points to Store.Data
|
// points to Store.Data
|
||||||
URI []*Parameter
|
URI []*Parameter
|
||||||
|
@ -58,7 +58,7 @@ func New(uriParams []string, req *http.Request) *Store {
|
||||||
ds.setURIParams(uriParams)
|
ds.setURIParams(uriParams)
|
||||||
|
|
||||||
// 2. GET (query) data
|
// 2. GET (query) data
|
||||||
ds.fetchGet(req)
|
ds.readQuery(req)
|
||||||
|
|
||||||
// 3. We are done if GET method
|
// 3. We are done if GET method
|
||||||
if req.Method == http.MethodGet {
|
if req.Method == http.MethodGet {
|
||||||
|
@ -66,7 +66,7 @@ func New(uriParams []string, req *http.Request) *Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. POST (body) data
|
// 4. POST (body) data
|
||||||
ds.fetchForm(req)
|
ds.readForm(req)
|
||||||
|
|
||||||
return ds
|
return ds
|
||||||
}
|
}
|
||||||
|
@ -92,19 +92,19 @@ func (i *Store) setURIParams(orderedUParams []string) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchGet stores data from the QUERY (in url parameters)
|
// readQuery stores data from the QUERY (in url parameters)
|
||||||
func (i *Store) fetchGet(req *http.Request) {
|
func (i *Store) readQuery(req *http.Request) {
|
||||||
|
|
||||||
for name, value := range req.URL.Query() {
|
for name, value := range req.URL.Query() {
|
||||||
|
|
||||||
// prevent invalid names
|
// prevent invalid names
|
||||||
if !validName(name) {
|
if !isNameValid(name) {
|
||||||
log.Printf("invalid variable name: '%s'\n", name)
|
log.Printf("invalid variable name: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent injections
|
// prevent injections
|
||||||
if nameInjection(name) {
|
if hasNameInjection(name) {
|
||||||
log.Printf("get.injection: '%s'\n", name)
|
log.Printf("get.injection: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -125,12 +125,12 @@ func (i *Store) fetchGet(req *http.Request) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchForm stores FORM data
|
// readForm stores FORM data
|
||||||
//
|
//
|
||||||
// - parse 'form-data' if not supported (not POST requests)
|
// - parse 'form-data' if not supported (not POST requests)
|
||||||
// - parse 'x-www-form-urlencoded'
|
// - parse 'x-www-form-urlencoded'
|
||||||
// - parse 'application/json'
|
// - parse 'application/json'
|
||||||
func (i *Store) fetchForm(req *http.Request) {
|
func (i *Store) readForm(req *http.Request) {
|
||||||
|
|
||||||
contentType := req.Header.Get("Content-Type")
|
contentType := req.Header.Get("Content-Type")
|
||||||
|
|
||||||
|
@ -173,13 +173,13 @@ func (i *Store) parseJSON(req *http.Request) {
|
||||||
for name, value := range parsed {
|
for name, value := range parsed {
|
||||||
|
|
||||||
// prevent invalid names
|
// prevent invalid names
|
||||||
if !validName(name) {
|
if !isNameValid(name) {
|
||||||
log.Printf("invalid variable name: '%s'\n", name)
|
log.Printf("invalid variable name: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent injections
|
// prevent injections
|
||||||
if nameInjection(name) {
|
if hasNameInjection(name) {
|
||||||
log.Printf("post.injection: '%s'\n", name)
|
log.Printf("post.injection: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -210,13 +210,13 @@ func (i *Store) parseUrlencoded(req *http.Request) {
|
||||||
for name, value := range req.PostForm {
|
for name, value := range req.PostForm {
|
||||||
|
|
||||||
// prevent invalid names
|
// prevent invalid names
|
||||||
if !validName(name) {
|
if !isNameValid(name) {
|
||||||
log.Printf("invalid variable name: '%s'\n", name)
|
log.Printf("invalid variable name: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent injections
|
// prevent injections
|
||||||
if nameInjection(name) {
|
if hasNameInjection(name) {
|
||||||
log.Printf("post.injection: '%s'\n", name)
|
log.Printf("post.injection: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -254,13 +254,13 @@ func (i *Store) parseMultipart(req *http.Request) {
|
||||||
for name, data := range mpr.Data {
|
for name, data := range mpr.Data {
|
||||||
|
|
||||||
// prevent invalid names
|
// prevent invalid names
|
||||||
if !validName(name) {
|
if !isNameValid(name) {
|
||||||
log.Printf("invalid variable name: '%s'\n", name)
|
log.Printf("invalid variable name: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent injections
|
// prevent injections
|
||||||
if nameInjection(name) {
|
if hasNameInjection(name) {
|
||||||
log.Printf("post.injection: '%s'\n", name)
|
log.Printf("post.injection: '%s'\n", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -281,16 +281,16 @@ func (i *Store) parseMultipart(req *http.Request) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// nameInjection returns whether there is
|
// hasNameInjection returns whether there is
|
||||||
// a parameter name injection:
|
// a parameter name injection:
|
||||||
// - inferred GET parameters
|
// - inferred GET parameters
|
||||||
// - inferred URL parameters
|
// - inferred URL parameters
|
||||||
func nameInjection(pName string) bool {
|
func hasNameInjection(pName string) bool {
|
||||||
return strings.HasPrefix(pName, "GET@") || strings.HasPrefix(pName, "URL#")
|
return strings.HasPrefix(pName, "GET@") || strings.HasPrefix(pName, "URL#")
|
||||||
}
|
}
|
||||||
|
|
||||||
// validName returns whether a parameter name (without the GET@ or URL# prefix) is valid
|
// isNameValid returns whether a parameter name (without the GET@ or URL# prefix) is valid
|
||||||
// if fails if the name begins/ends with underscores
|
// if fails if the name begins/ends with underscores
|
||||||
func validName(pName string) bool {
|
func isNameValid(pName string) bool {
|
||||||
return strings.Trim(pName, "_") == pName
|
return strings.Trim(pName, "_") == pName
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue