remove debug | add parameter 'rename' feature

This commit is contained in:
Adrien Marquès 2018-06-03 11:32:44 +02:00
parent 446d1f5e70
commit 13dc619abe
3 changed files with 20 additions and 21 deletions

View File

@ -247,7 +247,8 @@ func (c *Controller) setDefaults() {
// 2. Default 'rename': same as name // 2. Default 'rename': same as name
if param.Rename == nil { if param.Rename == nil {
param.Rename = &name param.Rename = new(string)
*param.Rename = name
} }
} }

View File

@ -103,8 +103,6 @@ func (i *Request) LoadController(method string) (func(implement.Arguments, *impl
tmp[0] = tmp[0] - ('a' - 'A') tmp[0] = tmp[0] - ('a' - 'A')
method = string(tmp) method = string(tmp)
fmt.Printf("method is '%s'\n", method)
/* (2) Try to load plugin */ /* (2) Try to load plugin */
p, err2 := plugin.Open(path) p, err2 := plugin.Open(path)
if err2 != nil { if err2 != nil {
@ -122,7 +120,6 @@ func (i *Request) LoadController(method string) (func(implement.Arguments, *impl
if !validSignature { if !validSignature {
return nil, fmt.Errorf("Invalid signature for method %s", method) return nil, fmt.Errorf("Invalid signature for method %s", method)
} }
fmt.Printf("callable is %v\n", callable)
return callable, nil return callable, nil

View File

@ -2,14 +2,12 @@ package gfw
import ( import (
"encoding/json" "encoding/json"
"fmt"
"git.xdrm.io/xdrm-brackets/gfw/config" "git.xdrm.io/xdrm-brackets/gfw/config"
"git.xdrm.io/xdrm-brackets/gfw/err" "git.xdrm.io/xdrm-brackets/gfw/err"
"git.xdrm.io/xdrm-brackets/gfw/implement" "git.xdrm.io/xdrm-brackets/gfw/implement"
"git.xdrm.io/xdrm-brackets/gfw/request" "git.xdrm.io/xdrm-brackets/gfw/request"
"log" "log"
"net/http" "net/http"
"strings"
) )
func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) { func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) {
@ -43,17 +41,20 @@ func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) {
parameters := make(map[string]interface{}) parameters := make(map[string]interface{})
for name, param := range method.Parameters { for name, param := range method.Parameters {
/* (1) Extract value */ /* (1) Rename */
rename := *param.Rename
/* (2) Extract value */
p, isset := req.Data.Set[name] p, isset := req.Data.Set[name]
/* (2) Required & missing */ /* (3) Required & missing */
if !isset && !*param.Optional { if !isset && !*param.Optional {
paramError = err.MissingParam paramError = err.MissingParam
paramError.BindArgument(name) paramError.BindArgument(name)
break break
} }
/* (3) Optional & missing: set default value */ /* (4) Optional & missing: set default value */
if !isset { if !isset {
p = &request.Parameter{ p = &request.Parameter{
Parsed: true, Parsed: true,
@ -63,40 +64,44 @@ func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) {
if param.Default != nil { if param.Default != nil {
p.Value = *param.Default p.Value = *param.Default
} }
// we are done
parameters[rename] = p.Value
continue
} }
/* (4) Parse parameter if not file */ /* (5) Parse parameter if not file */
if !p.File { if !p.File {
p.Parse() p.Parse()
} }
/* (4) Fail on unexpected multipart file */ /* (6) Fail on unexpected multipart file */
waitFile, gotFile := param.Type == "FILE", p.File waitFile, gotFile := param.Type == "FILE", p.File
if gotFile && !waitFile || !gotFile && waitFile { if gotFile && !waitFile || !gotFile && waitFile {
paramError = err.InvalidParam paramError = err.InvalidParam
paramError.BindArgument(name) paramError.BindArgument(rename)
paramError.BindArgument("FILE") paramError.BindArgument("FILE")
break break
} }
/* (5) Do not check if file */ /* (7) Do not check if file */
if gotFile { if gotFile {
parameters[name] = p.Value parameters[rename] = p.Value
continue continue
} }
/* (6) Check type */ /* (8) Check type */
if s.Checker.Run(param.Type, p.Value) != nil { if s.Checker.Run(param.Type, p.Value) != nil {
paramError = err.InvalidParam paramError = err.InvalidParam
paramError.BindArgument(name) paramError.BindArgument(rename)
paramError.BindArgument(param.Type) paramError.BindArgument(param.Type)
paramError.BindArgument(p.Value) paramError.BindArgument(p.Value)
break break
} }
parameters[name] = p.Value parameters[rename] = p.Value
} }
@ -116,10 +121,6 @@ func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) {
log.Printf("[err] %s\n", err) log.Printf("[err] %s\n", err)
return return
} }
fmt.Printf("OK\nplugin: '%si.so'\n", strings.Join(req.Path, "/"))
for name, value := range parameters {
fmt.Printf(" $%s = %v\n", name, value)
}
/* (6) Execute and get response /* (6) Execute and get response
---------------------------------------------------------*/ ---------------------------------------------------------*/