Created
September 2, 2016 14:45
-
-
Save zgiber/eb178a2a7351fac788399b83c58a2ff4 to your computer and use it in GitHub Desktop.
Http Server Logging request bodies.
This file contains 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" | |
"log" | |
"net/http" | |
"os" | |
"os/signal" | |
"time" | |
) | |
func main() { | |
d, err := os.Getwd() | |
if err != nil { | |
log.Fatal(err) | |
} | |
f, err := os.Create("last_request.log") | |
if err != nil { | |
log.Fatal(err) | |
} | |
handleRequests := func(w http.ResponseWriter, r *http.Request) { | |
n, err := io.Copy(f, r.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Println(n, "bytes written") | |
} | |
defer f.Close() | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt) | |
go func() { | |
<-c | |
f.Close() | |
os.Exit(0) | |
}() | |
http.HandleFunc("/logme", handleRequests) | |
http.Handle("/", http.FileServer(http.Dir(d))) | |
http.ListenAndServe(":8765", http.DefaultServeMux) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment