Skip to content

Instantly share code, notes, and snippets.



Created Nov 29, 2020
What would you like to do?
package main
import (
func main() {
http.Handle("/", handle(home))
http.HandleFunc("/metrics", handle(metricsPage))
http.HandleFunc("/articles", handle(articles))
log.Fatal(http.ListenAndServe(":8080", nil))
func handle(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
metrics.GetOrCreateCounter(fmt.Sprintf(`requests_total{path=%q}`, r.URL.Path)).Inc()
h.ServeHTTP(w, r)
func metricsPage(w http.ResponseWriter, r *http.Request) {
metrics.WritePrometheus(w, true)
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome!")
func articles(w http.ResponseWriter, r *http.Request) {
resp, err := http.DefaultClient.Get("")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
data, _ := ioutil.ReadAll(resp.Body)
fmt.Fprint(w, string(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment