aicra/internal/request/request.go

38 lines
860 B
Go
Raw Normal View History

package request
2018-06-01 08:51:51 +00:00
import (
"git.xdrm.io/go/aicra/driver"
"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"
)
// 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
}
// 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')
func (i *Request) RunController(_method string, _driver driver.Driver) (func(response.Arguments) response.Response, err.Error) {
2018-06-01 08:51:51 +00:00
return _driver.RunController(i.Path, _method)
2018-06-01 08:51:51 +00:00
}