Skip to content

Instantly share code, notes, and snippets.

@daumie daumie/main.go
Last active Aug 6, 2019

Embed
What would you like to do?
A simple go application with a metrics endpoint.
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// RequestLogger : Logs HTTP requests from specified routes
func RequestLogger(targetMux http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
targetMux.ServeHTTP(w, r)
// log request by who(IP address)
requesterIP := r.RemoteAddr
log.Printf(
"%s\t\t%s\t\t%s\t\t%v",
r.Method,
r.RequestURI,
requesterIP,
time.Since(start),
)
})
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
if status == http.StatusNotFound {
fmt.Fprint(w, "404 Page Not Found")
}
}
func helloWorld(w http.ResponseWriter, r *http.Request) {
html := "Hello World, It's your boy Dominic. How can I help you today"
if r.URL.Path != "/" {
errorHandler(w, r, http.StatusNotFound)
return
}
w.Write([]byte(html))
}
func byeWorld(w http.ResponseWriter, r *http.Request) {
html := "Bye World!"
if r.URL.Path != "/bye" {
errorHandler(w, r, http.StatusNotFound)
return
}
w.Write([]byte(html))
}
func main() {
metrics := promhttp.Handler()
mux := http.NewServeMux()
mux.HandleFunc("/", helloWorld)
mux.HandleFunc("/bye", byeWorld)
mux.Handle("/metrics", metrics)
http.ListenAndServe(":8080", RequestLogger(mux))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.