From 13dc619abe9bf938b45dfdd7b8dab46e80a05957 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 3 Jun 2018 11:32:44 +0200 Subject: [PATCH] remove debug | add parameter 'rename' feature --- config/controller.go | 3 ++- request/request.go | 3 --- router.go | 35 ++++++++++++++++++----------------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/config/controller.go b/config/controller.go index 771c616..c7ce182 100644 --- a/config/controller.go +++ b/config/controller.go @@ -247,7 +247,8 @@ func (c *Controller) setDefaults() { // 2. Default 'rename': same as name if param.Rename == nil { - param.Rename = &name + param.Rename = new(string) + *param.Rename = name } } diff --git a/request/request.go b/request/request.go index 7ac9024..3fdff53 100644 --- a/request/request.go +++ b/request/request.go @@ -103,8 +103,6 @@ func (i *Request) LoadController(method string) (func(implement.Arguments, *impl tmp[0] = tmp[0] - ('a' - 'A') method = string(tmp) - fmt.Printf("method is '%s'\n", method) - /* (2) Try to load plugin */ p, err2 := plugin.Open(path) if err2 != nil { @@ -122,7 +120,6 @@ func (i *Request) LoadController(method string) (func(implement.Arguments, *impl if !validSignature { return nil, fmt.Errorf("Invalid signature for method %s", method) } - fmt.Printf("callable is %v\n", callable) return callable, nil diff --git a/router.go b/router.go index ae1717d..4c16630 100644 --- a/router.go +++ b/router.go @@ -2,14 +2,12 @@ package gfw import ( "encoding/json" - "fmt" "git.xdrm.io/xdrm-brackets/gfw/config" "git.xdrm.io/xdrm-brackets/gfw/err" "git.xdrm.io/xdrm-brackets/gfw/implement" "git.xdrm.io/xdrm-brackets/gfw/request" "log" "net/http" - "strings" ) 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{}) for name, param := range method.Parameters { - /* (1) Extract value */ + /* (1) Rename */ + rename := *param.Rename + + /* (2) Extract value */ p, isset := req.Data.Set[name] - /* (2) Required & missing */ + /* (3) Required & missing */ if !isset && !*param.Optional { paramError = err.MissingParam paramError.BindArgument(name) break } - /* (3) Optional & missing: set default value */ + /* (4) Optional & missing: set default value */ if !isset { p = &request.Parameter{ Parsed: true, @@ -63,40 +64,44 @@ func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) { if param.Default != nil { 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 { p.Parse() } - /* (4) Fail on unexpected multipart file */ + /* (6) Fail on unexpected multipart file */ waitFile, gotFile := param.Type == "FILE", p.File if gotFile && !waitFile || !gotFile && waitFile { paramError = err.InvalidParam - paramError.BindArgument(name) + paramError.BindArgument(rename) paramError.BindArgument("FILE") break } - /* (5) Do not check if file */ + /* (7) Do not check if file */ if gotFile { - parameters[name] = p.Value + parameters[rename] = p.Value continue } - /* (6) Check type */ + /* (8) Check type */ if s.Checker.Run(param.Type, p.Value) != nil { paramError = err.InvalidParam - paramError.BindArgument(name) + paramError.BindArgument(rename) paramError.BindArgument(param.Type) paramError.BindArgument(p.Value) 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) 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 ---------------------------------------------------------*/