Skip to content

Instantly share code, notes, and snippets.

@wudi
Last active March 3, 2017 05:26
Show Gist options
  • Save wudi/8d1284b0c93eb65e63244c2595e42df4 to your computer and use it in GitHub Desktop.
Save wudi/8d1284b0c93eb65e63244c2595e42df4 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
var port = 8080
func main() {
flag.IntVar(&port, "port", port, "Listen port")
flag.Parse()
fmt.Printf("Simple Http Server listen on: %d\nOpen: http://127.0.0.1:%d\nAuthor: %s\n", port, port, "[email protected]")
fmt.Println("-------------------------------------------")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("------------------Header--------------------")
fmt.Printf("%s %s\n", r.Method, r.URL.String())
for key, val := range r.Header {
fmt.Printf("%s: %s\n", key, strings.Join(val, ";"))
}
fmt.Printf("\n")
fmt.Println("-------------------Body---------------------")
c, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", string(c))
fmt.Fprintf(w, "ok")
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment