Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Last active January 4, 2017 02:40
Show Gist options
  • Save gnilchee/58e92a946f9350cde78fe918b14ade92 to your computer and use it in GitHub Desktop.
Save gnilchee/58e92a946f9350cde78fe918b14ade92 to your computer and use it in GitHub Desktop.
Simple http server using redirect handlers and func handlers via mux (no external middleware)
package main
import (
"log"
"net/http"
"io"
"fmt"
)
func main() {
mux := http.NewServeMux()
google := http.RedirectHandler("https://google.com", 307)
gmail := http.RedirectHandler("https://mail.google.com", 307)
mux.Handle("/google", google)
mux.Handle("/gmail", gmail)
mux.HandleFunc("/health", HealthCheckHandler)
mux.HandleFunc("/", WildcardHandler)
log.Println("Listening...")
http.ListenAndServe(":8080", mux)
}
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{ "healthy": true }`)
}
func WildcardHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, you tried to access URI: /%s", r.URL.Path[1:])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment