DRY some code, add helper function handleHTTPAs

master
Buddy Sandidge 8 years ago
parent b0b44a992f
commit 27f0424765

@ -88,6 +88,12 @@ func (s *Server) getResponseType(req *http.Request) responseFormat {
return unknownResponse
}
func (s *Server) handleHTTPAs(responseType responseFormat) func(http.ResponseWriter, *http.Request) {
return func(resp http.ResponseWriter, req *http.Request) {
s.handleHTTP(resp, req, responseType)
}
}
func (s *Server) sendResponse(resp http.ResponseWriter, ip *IP, responseType responseFormat) {
s.logger.Printf("Request from %s %s\n", ip.version, ip)

@ -76,17 +76,9 @@ func getHandler(headers []string) (http.Handler, *Server) {
router := pat.New()
router.Get("/json", func(resp http.ResponseWriter, req *http.Request) {
service.handleHTTP(resp, req, jsonResponse)
})
router.Get("/text", func(resp http.ResponseWriter, req *http.Request) {
service.handleHTTP(resp, req, textResponse)
})
router.Get("/html", func(resp http.ResponseWriter, req *http.Request) {
service.handleHTTP(resp, req, htmlResponse)
})
router.Get("/html", service.handleHTTPAs(htmlResponse))
router.Get("/json", service.handleHTTPAs(jsonResponse))
router.Get("/text", service.handleHTTPAs(textResponse))
router.Get("/", func(resp http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {

Loading…
Cancel
Save