Created
August 27, 2020 10:37
-
-
Save toufik-airane/be3a94e7595611607e33a52c0bce9484 to your computer and use it in GitHub Desktop.
Display HTTP requests
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 ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
) | |
const ( | |
black = "\033[30m%s\033[0m" | |
red = "\033[31m%s\033[0m" | |
green = "\033[32m%s\033[0m" | |
yellow = "\033[33m%s\033[0m" | |
blue = "\033[34m%s\033[0m" | |
magenta = "\033[35m%s\033[0m" | |
cyan = "\033[36m%s\033[0m" | |
white = "\033[97m%s\033[0m" | |
) | |
func parse() string { | |
var port string | |
flag.StringVar(&port, "port", "", "Specify a port to start.") | |
flag.Parse() | |
flag.Usage = func() { | |
fmt.Fprintln(os.Stderr, "🌍 gohttp") | |
flag.PrintDefaults() | |
} | |
if len(port) == 0 { | |
flag.Usage() | |
log.Fatal() | |
} | |
return port | |
} | |
func main() { | |
port := parse() | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("OK")) | |
body, _ := ioutil.ReadAll(r.Body) | |
fmt.Printf(red+" ", r.RemoteAddr) | |
fmt.Printf(green+" ", r.Method) | |
fmt.Printf(yellow+" ", r.URL.EscapedPath()) | |
fmt.Printf(blue+" ", r.UserAgent()) | |
fmt.Printf(magenta+" ", string(body)) | |
fmt.Println("") | |
}) | |
http.ListenAndServe(":"+port, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment