Skip to content

Commit

Permalink
feat: add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlaltdev committed Jun 18, 2021
1 parent 07a3e2d commit edf10eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ To response another status than 202:
```sh
srv42 -s 204
```

To have the full details of the request:
```sh
srv42 -v
```
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"time"
)

const (
FormatPattern = "HTTP - %s - - [%s] \"%s %s %s\" %d %d \"%s\" %d\n"
FormatLog = "HTTP - %s - - [%s] \"%s %s %s\" %d %d \"%s\" %d\n"
)

var (
port = flag.Int("p", 1337, "port to use")
status = flag.Int("s", 202, "status code to return")
port = flag.Int("p", 1337, "port to use")
status = flag.Int("s", 202, "status code to return")
verbose = flag.Bool("v", false, "should srv42 print the full path and body")
)

func Handler(w http.ResponseWriter, r *http.Request) {
t := time.Now()

w.WriteHeader(*status)

fmt.Printf(FormatPattern,
fmt.Printf(FormatLog,
r.RemoteAddr,
t.Format("02/Jan/2006:15:04:05 -0700"),
r.Method,
Expand All @@ -32,6 +34,14 @@ func Handler(w http.ResponseWriter, r *http.Request) {
r.UserAgent(),
time.Since(t),
)

if *verbose {
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s\n\n", string(requestDump))
}
}

func Serve() {
Expand Down

0 comments on commit edf10eb

Please sign in to comment.