Cleanup, label parameters, change method names
This commit is contained in:
parent
aa1da8078a
commit
069bbc84dd
34
prototype.go
34
prototype.go
@ -3,39 +3,39 @@ package router
|
|||||||
import "io"
|
import "io"
|
||||||
|
|
||||||
type RequestBuilder interface {
|
type RequestBuilder interface {
|
||||||
Allowed(method string) ErrorResponse
|
Allowed(method string) (errRes ErrorResponse)
|
||||||
Header(Header) ErrorResponse
|
Header(header Header) (errRes ErrorResponse)
|
||||||
Body(body io.ReadCloser) ErrorResponse
|
Body(body io.ReadCloser) (errRes ErrorResponse)
|
||||||
Values(values Values) ErrorResponse
|
Values(values Values) (errRes ErrorResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrototypeRequestBuilder interface {
|
type PrototypeRequestBuilder interface {
|
||||||
RequestBuilder() RequestBuilder
|
RequestBuilder() (req RequestBuilder)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response interface {
|
type Response interface {
|
||||||
Header() Header
|
Header() (header Header)
|
||||||
Response() (body []byte)
|
Bytes() (body []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrototypeResponse interface {
|
type PrototypeResponse interface {
|
||||||
Response() Response
|
Response() (res Response)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ErrorResponse interface {
|
type ErrorResponse interface {
|
||||||
Ok() bool
|
Ok() (ok bool)
|
||||||
HttpStatus() int
|
HttpStatus() (code int)
|
||||||
ErrorResponse() []byte
|
Bytes() (body []byte)
|
||||||
String() string
|
String() (out string)
|
||||||
Error() string
|
Error() (out string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrototypeErrorResponse interface {
|
type PrototypeErrorResponse interface {
|
||||||
ErrorResponse() ErrorResponse
|
ErrorResponse() (errRes ErrorResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Prototype struct {
|
type Prototype struct {
|
||||||
PrototypeRequestBuilder
|
req PrototypeRequestBuilder
|
||||||
PrototypeResponse
|
res PrototypeResponse
|
||||||
PrototypeErrorResponse
|
errRes PrototypeErrorResponse
|
||||||
}
|
}
|
||||||
|
10
server.go
10
server.go
@ -56,7 +56,7 @@ func (srv *server) PostServeFunc(serve ServeFunc) *server {
|
|||||||
func (srv *server) handleError(errRes ErrorResponse, w http.ResponseWriter) (ok bool) {
|
func (srv *server) handleError(errRes ErrorResponse, w http.ResponseWriter) (ok bool) {
|
||||||
if !errRes.Ok() {
|
if !errRes.Ok() {
|
||||||
w.WriteHeader(errRes.HttpStatus())
|
w.WriteHeader(errRes.HttpStatus())
|
||||||
w.Write(errRes.ErrorResponse())
|
w.Write(errRes.Bytes())
|
||||||
srv.logger.Logf(log.LevelError, errRes.String())
|
srv.logger.Logf(log.LevelError, errRes.String())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -64,8 +64,8 @@ func (srv *server) handleError(errRes ErrorResponse, w http.ResponseWriter) (ok
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
pro := srv.prototype
|
p := srv.prototype
|
||||||
req := pro.RequestBuilder()
|
req := p.req.RequestBuilder()
|
||||||
if ok := srv.handleError(req.Allowed(r.Method), w); !ok {
|
if ok := srv.handleError(req.Allowed(r.Method), w); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -79,7 +79,7 @@ func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if ok := srv.handleError(req.Values(Values(r.Form)), w); !ok {
|
if ok := srv.handleError(req.Values(Values(r.Form)), w); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res := pro.Response()
|
res := p.res.Response()
|
||||||
for _, stage := range srv.serve {
|
for _, stage := range srv.serve {
|
||||||
for _, s := range stage {
|
for _, s := range stage {
|
||||||
if ok := srv.handleError(s(req, res), w); !ok {
|
if ok := srv.handleError(s(req, res), w); !ok {
|
||||||
@ -95,5 +95,5 @@ func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.Write(res.Response())
|
w.Write(res.Bytes())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user