Last active
September 2, 2015 15:34
-
-
Save dblooman/5aa84b82518e248f312c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package logger | |
import ( | |
"net/http" | |
log "github.com/Sirupsen/logrus" | |
) | |
func Log(inner http.Handler, name string) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
inner.ServeHTTP(w, r) | |
log.SetFormatter(&log.JSONFormatter{}) | |
log.WithFields(log.Fields{ | |
"URL": r.RequestURI, | |
"METHOD": r.Method, | |
}).Info(name) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package routes | |
import ( | |
"net/http" | |
"github.com/gorilla/mux" | |
"github.com/DaveBlooman/api/src/logger" | |
) | |
func MozartRouter() *mux.Router { | |
router := mux.NewRouter().StrictSlash(true) | |
for _, route := range routes { | |
var handler http.Handler | |
handler = route.HandlerFunc | |
handler = logger.Log(handler, route.Name) | |
router. | |
Methods(route.Method). | |
Path(route.Pattern). | |
Name(route.Name). | |
Handler(handler) | |
} | |
return router | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package routes | |
import ( | |
"net/http" | |
"github.com/DaveBlooman/api/src/controllers" | |
) | |
type Route struct { | |
Name string | |
Method string | |
Pattern string | |
HandlerFunc http.HandlerFunc | |
} | |
type Routes []Route | |
var routes = Routes{ | |
Route{ | |
"Status", | |
"GET", | |
"/status", | |
controllers.HandleStatusRequest, | |
}, | |
Route{ | |
"Product", | |
"GET", | |
"/v1/env/{env}/product/{product}/page/{page}", | |
controllers.HandleGetRequest, | |
}, | |
Route{ | |
"Product", | |
"POST", | |
"/v1/env/{env}/product/{product}/page/{page}", | |
controllers.HandlePostRequest, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment