Follow golang convention for constants

This commit is contained in:
Martin Polden 2016-05-26 21:38:10 +02:00
parent 6e1ad53a3b
commit cfb86fe124
2 changed files with 9 additions and 9 deletions

View File

@ -16,9 +16,9 @@ import (
"github.com/gorilla/mux"
)
const APPLICATION_JSON = "application/json"
const jsonMediaType = "application/json"
var USER_AGENT_RE = regexp.MustCompile(
var userAgentPattern = regexp.MustCompile(
`^(?:curl|Wget|fetch\slibfetch|ddclient|Go-http-client|HTTPie)\/.*|Go\s1\.1\spackage\shttp$`,
)
@ -143,7 +143,7 @@ func (a *API) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
if err != nil {
return internalServerError(err).AsJSON()
}
w.Header().Set("Content-Type", APPLICATION_JSON)
w.Header().Set("Content-Type", jsonMediaType)
w.Write(b)
return nil
}
@ -157,7 +157,7 @@ func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
if err != nil {
return internalServerError(err).AsJSON()
}
w.Header().Set("Content-Type", APPLICATION_JSON)
w.Header().Set("Content-Type", jsonMediaType)
w.Write(b)
return nil
}
@ -183,14 +183,14 @@ func (a *API) DefaultHandler(w http.ResponseWriter, r *http.Request) *appError {
func (a *API) NotFoundHandler(w http.ResponseWriter, r *http.Request) *appError {
err := notFound(nil).WithMessage("404 page not found")
if r.Header.Get("accept") == APPLICATION_JSON {
if r.Header.Get("accept") == jsonMediaType {
err = err.AsJSON()
}
return err
}
func cliMatcher(r *http.Request, rm *mux.RouteMatch) bool {
return USER_AGENT_RE.MatchString(r.UserAgent())
return userAgentPattern.MatchString(r.UserAgent())
}
type appHandler func(http.ResponseWriter, *http.Request) *appError
@ -224,7 +224,7 @@ func (a *API) Handlers() http.Handler {
r := mux.NewRouter()
// JSON
r.Handle("/", appHandler(a.JSONHandler)).Methods("GET").Headers("Accept", APPLICATION_JSON)
r.Handle("/", appHandler(a.JSONHandler)).Methods("GET").Headers("Accept", jsonMediaType)
r.Handle("/json", appHandler(a.JSONHandler)).Methods("GET")
// CLI

View File

@ -26,7 +26,7 @@ func badRequest(err error) *appError {
}
func (e *appError) AsJSON() *appError {
e.ContentType = APPLICATION_JSON
e.ContentType = jsonMediaType
return e
}
@ -36,5 +36,5 @@ func (e *appError) WithMessage(message string) *appError {
}
func (e *appError) IsJSON() bool {
return e.ContentType == APPLICATION_JSON
return e.ContentType == jsonMediaType
}