2018-09-28 06:10:41 +00:00
|
|
|
package request
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-27 11:43:36 +00:00
|
|
|
"git.xdrm.io/go/aicra/driver"
|
2018-07-08 23:00:45 +00:00
|
|
|
"git.xdrm.io/go/aicra/err"
|
2018-07-08 22:15:29 +00:00
|
|
|
"git.xdrm.io/go/aicra/response"
|
2018-06-01 08:51:51 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-07-11 17:02:33 +00:00
|
|
|
// FromHTTP builds an interface request from a http.Request
|
|
|
|
func FromHTTP(req *http.Request) (*Request, error) {
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
/* (1) Get useful data */
|
2018-07-08 23:34:21 +00:00
|
|
|
uri := normaliseURI(req.URL.Path)
|
2018-06-01 08:51:51 +00:00
|
|
|
uriparts := strings.Split(uri, "/")
|
|
|
|
|
|
|
|
/* (2) Init request */
|
|
|
|
inst := &Request{
|
2018-07-08 23:34:21 +00:00
|
|
|
URI: uriparts,
|
2018-06-01 08:51:51 +00:00
|
|
|
Path: make([]string, 0, len(uriparts)),
|
|
|
|
Data: NewDataset(),
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Build dataset */
|
|
|
|
inst.Data.Build(req)
|
|
|
|
|
|
|
|
return inst, nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:58:30 +00:00
|
|
|
// RunController tries to load a controller from its uri
|
2018-06-01 08:51:51 +00:00
|
|
|
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
2018-09-28 13:58:30 +00:00
|
|
|
func (i *Request) RunController(_method string, _driver driver.Driver) (func(response.Arguments) response.Response, err.Error) {
|
2018-06-01 08:51:51 +00:00
|
|
|
|
2018-09-28 08:54:13 +00:00
|
|
|
return _driver.RunController(i.Path, _method)
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
}
|