Skip to content

Commit

Permalink
nsq*: cleanup HTTP routing (+httprouter)
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Jul 17, 2015
1 parent ff587b3 commit 3775cde
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 486 deletions.
1 change: 1 addition & 0 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ github.com/mreiferson/go-options 2cf7eb1fdd83e2bb3375fef6fdadb04c3ad564da
github.com/mreiferson/go-snappystream 028eae7ab5c4c9e2d1cb4c4ca1e53259bbe7e504 # v0.2.3
github.com/bitly/timer_metrics afad1794bb13e2a094720aeb27c088aa64564895
github.com/blang/semver 9bf7bff48b0388cb75991e58c6df7d13e982f1f2
github.com/julienschmidt/httprouter 6aacfd5ab513e34f7e64ea9627ab9670371b34e7
51 changes: 25 additions & 26 deletions internal/http_api/api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"fmt"
"net/http"
"strconv"

"github.com/julienschmidt/httprouter"
)

type APIHandler func(http.ResponseWriter, *http.Request, httprouter.Params) (interface{}, error)

type Err struct {
Code int
Text string
Expand All @@ -24,40 +28,35 @@ func acceptVersion(req *http.Request) int {
return 0
}

func RequirePOST(req *http.Request, f func() (interface{}, error)) func() (interface{}, error) {
if req.Method != "POST" {
return func() (interface{}, error) {
return nil, Err{405, "INVALID_REQUEST"}
func NegotiateVersion(f APIHandler) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
data, err := f(w, req, ps)
if err != nil {
if acceptVersion(req) == 1 {
RespondV1(w, err.(Err).Code, err)
} else {
// this handler always returns 500 for backwards compatibility
Respond(w, 500, err.Error(), nil)
}
return
}
}
return f
}

func NegotiateVersionWrapper(w http.ResponseWriter, req *http.Request, f func() (interface{}, error)) {
data, err := f()
if err != nil {
if acceptVersion(req) == 1 {
RespondV1(w, err.(Err).Code, err)
RespondV1(w, 200, data)
} else {
// this handler always returns 500 for backwards compatibility
Respond(w, 500, err.Error(), nil)
Respond(w, 200, "OK", data)
}
return
}
if acceptVersion(req) == 1 {
RespondV1(w, 200, data)
} else {
Respond(w, 200, "OK", data)
}
}

func V1Wrapper(w http.ResponseWriter, req *http.Request, f func() (interface{}, error)) {
data, err := f()
if err != nil {
RespondV1(w, err.(Err).Code, err)
return
func V1(f APIHandler) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
data, err := f(w, req, ps)
if err != nil {
RespondV1(w, err.(Err).Code, err)
return
}
RespondV1(w, 200, data)
}
RespondV1(w, 200, data)
}

func Respond(w http.ResponseWriter, statusCode int, statusTxt string, data interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion internal/http_api/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/bitly/nsq/internal/app"
)

func Serve(listener net.Listener, handler http.Handler, l app.Logger, proto string) {
func Serve(listener net.Listener, handler http.Handler, proto string, l app.Logger) {
l.Output(2, fmt.Sprintf("%s: listening on %s", proto, listener.Addr()))

server := &http.Server{
Expand Down
Loading

0 comments on commit 3775cde

Please sign in to comment.