2018-05-21 10:02:24 +00:00
|
|
|
package gfw
|
|
|
|
|
|
|
|
import (
|
2018-06-01 09:07:34 +00:00
|
|
|
"encoding/json"
|
2018-06-15 13:28:28 +00:00
|
|
|
"git.xdrm.io/go/xb-api/config"
|
|
|
|
"git.xdrm.io/go/xb-api/err"
|
|
|
|
"git.xdrm.io/go/xb-api/implement"
|
|
|
|
"git.xdrm.io/go/xb-api/request"
|
2018-05-21 10:02:24 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2018-06-01 08:51:51 +00:00
|
|
|
func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) {
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
/* (1) Build request
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Try to build request */
|
2018-06-01 08:51:51 +00:00
|
|
|
req, err2 := request.Build(httpReq)
|
2018-06-01 07:09:26 +00:00
|
|
|
if err2 != nil {
|
2018-06-01 08:51:51 +00:00
|
|
|
log.Fatal(err2)
|
2018-05-21 10:02:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
/* (2) Find a controller
|
|
|
|
---------------------------------------------------------*/
|
2018-06-01 08:51:51 +00:00
|
|
|
controller := s.findController(req)
|
2018-05-22 07:10:10 +00:00
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
/* (3) Check method
|
|
|
|
---------------------------------------------------------*/
|
2018-06-01 08:51:51 +00:00
|
|
|
var method *config.Method
|
|
|
|
if method = controller.Method(httpReq.Method); method == nil {
|
2018-06-01 07:09:26 +00:00
|
|
|
Json, _ := err.UnknownMethod.MarshalJSON()
|
2018-05-21 11:02:15 +00:00
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write(Json)
|
2018-06-01 07:09:26 +00:00
|
|
|
log.Printf("[err] %s\n", err.UnknownMethod.Reason)
|
2018-05-21 11:02:15 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-21 10:02:24 +00:00
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
/* (4) Check parameters
|
2018-05-21 11:02:15 +00:00
|
|
|
---------------------------------------------------------*/
|
2018-06-01 07:09:26 +00:00
|
|
|
var paramError err.Error = err.Success
|
2018-05-29 17:01:20 +00:00
|
|
|
parameters := make(map[string]interface{})
|
2018-05-28 16:25:17 +00:00
|
|
|
for name, param := range method.Parameters {
|
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (1) Extract value */
|
2018-06-01 08:51:51 +00:00
|
|
|
p, isset := req.Data.Set[name]
|
2018-05-28 16:25:17 +00:00
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (2) Required & missing */
|
|
|
|
if !isset && !param.Optional {
|
2018-06-01 07:09:26 +00:00
|
|
|
paramError = err.MissingParam
|
2018-05-29 17:01:20 +00:00
|
|
|
paramError.BindArgument(name)
|
|
|
|
break
|
|
|
|
}
|
2018-05-28 16:25:17 +00:00
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (3) Optional & missing: set default value */
|
2018-05-29 17:01:20 +00:00
|
|
|
if !isset {
|
2018-06-01 08:51:51 +00:00
|
|
|
p = &request.Parameter{
|
2018-05-29 17:01:20 +00:00
|
|
|
Parsed: true,
|
|
|
|
File: param.Type == "FILE",
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
if param.Default != nil {
|
|
|
|
p.Value = *param.Default
|
2018-05-28 16:25:17 +00:00
|
|
|
}
|
2018-06-03 09:32:44 +00:00
|
|
|
|
|
|
|
// we are done
|
2018-06-03 13:08:47 +00:00
|
|
|
parameters[param.Rename] = p.Value
|
2018-06-03 09:32:44 +00:00
|
|
|
continue
|
2018-05-28 16:25:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (4) Parse parameter if not file */
|
2018-06-01 08:51:51 +00:00
|
|
|
if !p.File {
|
|
|
|
p.Parse()
|
2018-05-29 14:00:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (5) Fail on unexpected multipart file */
|
2018-05-29 17:01:20 +00:00
|
|
|
waitFile, gotFile := param.Type == "FILE", p.File
|
|
|
|
if gotFile && !waitFile || !gotFile && waitFile {
|
2018-06-01 07:09:26 +00:00
|
|
|
paramError = err.InvalidParam
|
2018-06-03 13:08:47 +00:00
|
|
|
paramError.BindArgument(param.Rename)
|
2018-05-29 17:01:20 +00:00
|
|
|
paramError.BindArgument("FILE")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (6) Do not check if file */
|
2018-05-29 17:01:20 +00:00
|
|
|
if gotFile {
|
2018-06-03 13:08:47 +00:00
|
|
|
parameters[param.Rename] = p.Value
|
2018-05-29 17:01:20 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
/* (7) Check type */
|
2018-05-29 17:01:20 +00:00
|
|
|
if s.Checker.Run(param.Type, p.Value) != nil {
|
|
|
|
|
2018-06-01 07:09:26 +00:00
|
|
|
paramError = err.InvalidParam
|
2018-06-03 13:08:47 +00:00
|
|
|
paramError.BindArgument(param.Rename)
|
2018-05-28 16:25:17 +00:00
|
|
|
paramError.BindArgument(param.Type)
|
2018-05-29 13:42:41 +00:00
|
|
|
paramError.BindArgument(p.Value)
|
2018-05-28 16:25:17 +00:00
|
|
|
break
|
2018-05-29 17:01:20 +00:00
|
|
|
|
2018-05-28 16:25:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:47 +00:00
|
|
|
parameters[param.Rename] = p.Value
|
2018-05-29 17:01:20 +00:00
|
|
|
|
2018-05-22 07:28:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 16:25:17 +00:00
|
|
|
// Fail if argument check failed
|
2018-06-01 07:09:26 +00:00
|
|
|
if paramError.Code != err.Success.Code {
|
2018-05-28 16:25:17 +00:00
|
|
|
Json, _ := paramError.MarshalJSON()
|
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write(Json)
|
|
|
|
log.Printf("[err] %s\n", paramError.Reason)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-01 07:09:26 +00:00
|
|
|
/* (5) Load controller
|
|
|
|
---------------------------------------------------------*/
|
2018-06-01 08:51:51 +00:00
|
|
|
callable, err := req.LoadController(httpReq.Method)
|
2018-06-01 07:09:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[err] %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Execute and get response
|
|
|
|
---------------------------------------------------------*/
|
2018-06-16 20:15:05 +00:00
|
|
|
/* (1) Add optional Authorization header */
|
|
|
|
authHeader := httpReq.Header.Get("Authorization")
|
|
|
|
if len(authHeader) > 0 {
|
|
|
|
parameters["_AUTHORIZATION_"] = authHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Execute */
|
2018-06-01 09:07:34 +00:00
|
|
|
responseBarebone := implement.NewResponse()
|
|
|
|
response := callable(parameters, responseBarebone)
|
|
|
|
|
2018-06-16 20:15:05 +00:00
|
|
|
/* (3) Extract http headers */
|
2018-06-16 16:36:03 +00:00
|
|
|
for k, v := range response.Dump() {
|
|
|
|
if k == "_REDIRECT_" {
|
|
|
|
redir, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res.Header().Add("Location", redir)
|
|
|
|
res.WriteHeader(308) // permanent redirect
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 20:15:05 +00:00
|
|
|
/* (4) Build JSON response */
|
2018-06-01 09:07:34 +00:00
|
|
|
formattedResponse := response.Dump()
|
|
|
|
formattedResponse["error"] = response.Err.Code
|
|
|
|
formattedResponse["reason"] = response.Err.Reason
|
|
|
|
if response.Err.Arguments != nil && len(response.Err.Arguments) > 0 {
|
|
|
|
formattedResponse["args"] = response.Err.Arguments
|
2018-06-01 07:09:26 +00:00
|
|
|
}
|
2018-06-01 09:07:34 +00:00
|
|
|
jsonResponse, _ := json.Marshal(formattedResponse)
|
|
|
|
|
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write(jsonResponse)
|
2018-05-21 11:02:15 +00:00
|
|
|
return
|
2018-05-21 10:02:24 +00:00
|
|
|
}
|
2018-06-01 07:09:26 +00:00
|
|
|
|
2018-06-01 08:51:51 +00:00
|
|
|
func (s *Server) findController(req *request.Request) *config.Controller {
|
2018-06-01 07:09:26 +00:00
|
|
|
|
2018-06-01 08:51:51 +00:00
|
|
|
/* (1) Try to browse by URI */
|
|
|
|
pathi, ctl := s.config.Browse(req.Uri)
|
2018-06-01 07:09:26 +00:00
|
|
|
|
2018-06-01 08:51:51 +00:00
|
|
|
/* (2) Set controller uri */
|
|
|
|
req.Path = make([]string, 0, pathi)
|
|
|
|
req.Path = append(req.Path, req.Uri[:pathi]...)
|
2018-06-01 07:09:26 +00:00
|
|
|
|
|
|
|
/* (3) Extract & store URI params */
|
2018-06-01 08:51:51 +00:00
|
|
|
req.Data.SetUri(req.Uri[pathi:])
|
2018-06-01 07:09:26 +00:00
|
|
|
|
|
|
|
/* (4) Return controller */
|
|
|
|
return ctl
|
|
|
|
|
|
|
|
}
|