Created
June 11, 2019 21:28
-
-
Save dmerrick/30e1750ce17f1f5a395968f5943d18c9 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 ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
func handle(w http.ResponseWriter, r *http.Request) { | |
if r.URL.Path != "/" { | |
http.Error(w, "404 not found.", http.StatusNotFound) | |
return | |
} | |
switch r.Method { | |
case "GET": | |
fmt.Fprintf(w, "Perhaps you meant to make a POST request?\n") | |
case "POST": | |
b, _ := ioutil.ReadAll(r.Body) | |
fmt.Println(string(b) + "\n") | |
fmt.Fprintf(w, "OK\n") | |
default: | |
fmt.Fprintf(w, "Only GET and POST methods are supported.\n") | |
} | |
} | |
func main() { | |
http.HandleFunc("/", handle) | |
if err := http.ListenAndServe(":8080", nil); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment