Last active
January 4, 2017 02:40
-
-
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)
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 ( | |
"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