Created
November 28, 2016 16:28
-
-
Save kaneshin/4683eb3baa9d4d15a3a9aa4725168b28 to your computer and use it in GitHub Desktop.
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" | |
) | |
func pingHandler() func(http.ResponseWriter, *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/plain") | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte("pong")) | |
} | |
} | |
func echoHandler() func(http.ResponseWriter, *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/plain") | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte(r.URL.Query().Get("msg"))) | |
} | |
} | |
func init() { | |
http.HandleFunc("/ping", pingHandler()) | |
http.HandleFunc("/echo", echoHandler()) | |
} | |
func main() { | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment