Created
February 22, 2019 11:25
-
-
Save jumping/ab0fb37719b93327e3eb2457b1f65525 to your computer and use it in GitHub Desktop.
Not using context
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 main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"github.com/apex/log" | |
"github.com/gorilla/mux" | |
) | |
// routeLog perform logging on a route, returning log.Interface if want to add more specific details | |
func routeLog(r *http.Request, info string) *log.Entry { | |
l := log.WithFields(log.Fields{ | |
"method": r.Method, | |
"requestURI": r.RequestURI, | |
}) | |
l.Info(info) | |
return l | |
} | |
func index(w http.ResponseWriter, r *http.Request) { | |
routeLog(r, "index") | |
fmt.Fprintf(w, "hello") | |
} | |
func about(w http.ResponseWriter, r *http.Request) { | |
routeLog(r, "about") | |
fmt.Fprintf(w, "about") | |
} | |
func main() { | |
app := mux.NewRouter() | |
app.HandleFunc("/", index) | |
app.HandleFunc("/about", about) | |
if err := http.ListenAndServe(":"+os.Getenv("PORT"), app); err != nil { | |
log.WithError(err).Fatal("error listening") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment