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"
|
||||
|
||||
type RequestBuilder interface {
|
||||
Allowed(method string) ErrorResponse
|
||||
Header(Header) ErrorResponse
|
||||
Body(body io.ReadCloser) ErrorResponse
|
||||
Values(values Values) ErrorResponse
|
||||
Allowed(method string) (errRes ErrorResponse)
|
||||
Header(header Header) (errRes ErrorResponse)
|
||||
Body(body io.ReadCloser) (errRes ErrorResponse)
|
||||
Values(values Values) (errRes ErrorResponse)
|
||||
}
|
||||
|
||||
type PrototypeRequestBuilder interface {
|
||||
RequestBuilder() RequestBuilder
|
||||
RequestBuilder() (req RequestBuilder)
|
||||
}
|
||||
|
||||
type Response interface {
|
||||
Header() Header
|
||||
Response() (body []byte)
|
||||
Header() (header Header)
|
||||
Bytes() (body []byte)
|
||||
}
|
||||
|
||||
type PrototypeResponse interface {
|
||||
Response() Response
|
||||
Response() (res Response)
|
||||
}
|
||||
|
||||
type ErrorResponse interface {
|
||||
Ok() bool
|
||||
HttpStatus() int
|
||||
ErrorResponse() []byte
|
||||
String() string
|
||||
Error() string
|
||||
Ok() (ok bool)
|
||||
HttpStatus() (code int)
|
||||
Bytes() (body []byte)
|
||||
String() (out string)
|
||||
Error() (out string)
|
||||
}
|
||||
|
||||
type PrototypeErrorResponse interface {
|
||||
ErrorResponse() ErrorResponse
|
||||
ErrorResponse() (errRes ErrorResponse)
|
||||
}
|
||||
|
||||
type Prototype struct {
|
||||
PrototypeRequestBuilder
|
||||
PrototypeResponse
|
||||
PrototypeErrorResponse
|
||||
req PrototypeRequestBuilder
|
||||
res PrototypeResponse
|
||||
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) {
|
||||
if !errRes.Ok() {
|
||||
w.WriteHeader(errRes.HttpStatus())
|
||||
w.Write(errRes.ErrorResponse())
|
||||
w.Write(errRes.Bytes())
|
||||
srv.logger.Logf(log.LevelError, errRes.String())
|
||||
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) {
|
||||
pro := srv.prototype
|
||||
req := pro.RequestBuilder()
|
||||
p := srv.prototype
|
||||
req := p.req.RequestBuilder()
|
||||
if ok := srv.handleError(req.Allowed(r.Method), w); !ok {
|
||||
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 {
|
||||
return
|
||||
}
|
||||
res := pro.Response()
|
||||
res := p.res.Response()
|
||||
for _, stage := range srv.serve {
|
||||
for _, s := range stage {
|
||||
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