-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecho.go
More file actions
42 lines (38 loc) · 942 Bytes
/
echo.go
File metadata and controls
42 lines (38 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"sort"
"time"
)
func main() {
var port *int = flag.Int("Port", 8090, "Listen on this port")
var help *bool = flag.Bool("Help", false, "Print help")
flag.Parse()
if *help {
flag.CommandLine.Usage()
os.Exit(1)
}
http.HandleFunc("/", handler)
fmt.Printf("Starting listener on :%d\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s %s %s \n", r.Method, r.URL, r.Proto)
//Iterate over all header fields
fmt.Fprintf(w, "[Headers]\n")
keys := []string{}
for k := range r.Header {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(w, "%s=%s\n", k, r.Header[k])
}
fmt.Fprintf(w, "Host = %q\n", r.Host)
fmt.Fprintf(w, "RemoteAddr= %q\n", r.RemoteAddr)
fmt.Fprintf(w, "\n\n-----\n(c) %d Philipp Ritter\n", time.Now().Year())
}