mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-14 04:07:23 +01:00
Follow golang convention for constants
This commit is contained in:
parent
6e1ad53a3b
commit
cfb86fe124
14
api/api.go
14
api/api.go
@ -16,9 +16,9 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"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$`,
|
`^(?: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 {
|
if err != nil {
|
||||||
return internalServerError(err).AsJSON()
|
return internalServerError(err).AsJSON()
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", APPLICATION_JSON)
|
w.Header().Set("Content-Type", jsonMediaType)
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return internalServerError(err).AsJSON()
|
return internalServerError(err).AsJSON()
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", APPLICATION_JSON)
|
w.Header().Set("Content-Type", jsonMediaType)
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
return nil
|
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 {
|
func (a *API) NotFoundHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
err := notFound(nil).WithMessage("404 page not found")
|
err := notFound(nil).WithMessage("404 page not found")
|
||||||
if r.Header.Get("accept") == APPLICATION_JSON {
|
if r.Header.Get("accept") == jsonMediaType {
|
||||||
err = err.AsJSON()
|
err = err.AsJSON()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func cliMatcher(r *http.Request, rm *mux.RouteMatch) bool {
|
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
|
type appHandler func(http.ResponseWriter, *http.Request) *appError
|
||||||
@ -224,7 +224,7 @@ func (a *API) Handlers() http.Handler {
|
|||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
|
||||||
// JSON
|
// 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")
|
r.Handle("/json", appHandler(a.JSONHandler)).Methods("GET")
|
||||||
|
|
||||||
// CLI
|
// CLI
|
||||||
|
@ -26,7 +26,7 @@ func badRequest(err error) *appError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *appError) AsJSON() *appError {
|
func (e *appError) AsJSON() *appError {
|
||||||
e.ContentType = APPLICATION_JSON
|
e.ContentType = jsonMediaType
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,5 +36,5 @@ func (e *appError) WithMessage(message string) *appError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *appError) IsJSON() bool {
|
func (e *appError) IsJSON() bool {
|
||||||
return e.ContentType == APPLICATION_JSON
|
return e.ContentType == jsonMediaType
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user