implementable controller working + HTTP response writing
This commit is contained in:
parent
ca05b7ff29
commit
364481d3f3
|
@ -5,7 +5,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller func(map[string]interface{}) *Response
|
type Arguments map[string]interface{}
|
||||||
|
type Controller func(map[string]interface{}, *Response) Response
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
data map[string]interface{}
|
data map[string]interface{}
|
||||||
|
|
|
@ -93,10 +93,10 @@ func FetchFormData(req *http.Request) map[string]interface{} {
|
||||||
|
|
||||||
// LoadController tries to load a controller from its uri
|
// LoadController tries to load a controller from its uri
|
||||||
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
||||||
func (i *Request) LoadController(method string) (implement.Controller, error) {
|
func (i *Request) LoadController(method string) (func(implement.Arguments, *implement.Response) implement.Response, error) {
|
||||||
|
|
||||||
/* (1) Build controller path */
|
/* (1) Build controller path */
|
||||||
path := fmt.Sprintf("%si.so", i.Path)
|
path := fmt.Sprintf("./controllers/%si.so", strings.Join(i.Path, "/"))
|
||||||
|
|
||||||
/* (2) Format url */
|
/* (2) Format url */
|
||||||
tmp := []byte(strings.ToLower(method))
|
tmp := []byte(strings.ToLower(method))
|
||||||
|
@ -104,7 +104,6 @@ func (i *Request) LoadController(method string) (implement.Controller, error) {
|
||||||
method = string(tmp)
|
method = string(tmp)
|
||||||
|
|
||||||
fmt.Printf("method is '%s'\n", method)
|
fmt.Printf("method is '%s'\n", method)
|
||||||
return nil, nil
|
|
||||||
|
|
||||||
/* (2) Try to load plugin */
|
/* (2) Try to load plugin */
|
||||||
p, err2 := plugin.Open(path)
|
p, err2 := plugin.Open(path)
|
||||||
|
@ -119,10 +118,11 @@ func (i *Request) LoadController(method string) (implement.Controller, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (4) Check signature */
|
/* (4) Check signature */
|
||||||
callable, validSignature := m.(implement.Controller)
|
callable, validSignature := m.(func(implement.Arguments, *implement.Response) implement.Response)
|
||||||
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
|
||||||
|
|
||||||
|
|
24
router.go
24
router.go
|
@ -1,9 +1,11 @@
|
||||||
package gfw
|
package gfw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"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/request"
|
"git.xdrm.io/xdrm-brackets/gfw/request"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -121,15 +123,21 @@ func (s *Server) route(res http.ResponseWriter, httpReq *http.Request) {
|
||||||
|
|
||||||
/* (6) Execute and get response
|
/* (6) Execute and get response
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
resp := callable(parameters)
|
/* (1) Execute */
|
||||||
if resp != nil {
|
responseBarebone := implement.NewResponse()
|
||||||
fmt.Printf("-- OUT --\n")
|
response := callable(parameters, responseBarebone)
|
||||||
for name, value := range resp.Dump() {
|
|
||||||
fmt.Printf(" $%s = %v\n", name, value)
|
/* (2) Build JSON response */
|
||||||
}
|
formattedResponse := response.Dump()
|
||||||
eJSON, _ := resp.Err.MarshalJSON()
|
formattedResponse["error"] = response.Err.Code
|
||||||
fmt.Printf("-- ERR --\n%s\n", eJSON)
|
formattedResponse["reason"] = response.Err.Reason
|
||||||
|
if response.Err.Arguments != nil && len(response.Err.Arguments) > 0 {
|
||||||
|
formattedResponse["args"] = response.Err.Arguments
|
||||||
}
|
}
|
||||||
|
jsonResponse, _ := json.Marshal(formattedResponse)
|
||||||
|
|
||||||
|
res.Header().Add("Content-Type", "application/json")
|
||||||
|
res.Write(jsonResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue