Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykjadamczyk authored Sep 11, 2019
1 parent 474691b commit 3abb9fb
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"time"
)

func requestLogger(targetMux http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

targetMux.ServeHTTP(w, r)

// log request by who(IP address)
requesterIP := r.RemoteAddr
fmt.Println(start, "Received Request", r.Method, r.RequestURI, requesterIP)

log.Printf(
"%s\t\t%s\t\t%s\t\t%v",
r.Method,
r.RequestURI,
requesterIP,
time.Since(start),
)
log.Printf(
"DATA: %s",
r.Body,
)
})
}

func logRoute(w http.ResponseWriter, r *http.Request) {
html := ""
w.Write([]byte(html))
}

func main() {
fileName := "webrequests.log"

fmt.Println("Making log file ready", "Logfile: ", fileName)
// https://www.socketloop.com/tutorials/golang-how-to-save-log-messages-to-file
logFile, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)

if err != nil {
panic(err)
}

defer logFile.Close()

// direct all log messages to webrequests.log
log.SetOutput(logFile)

mux := http.NewServeMux()
mux.HandleFunc("/", logRoute)

fmt.Println("Starting Go Logging Server on port 7777")
http.ListenAndServe(":7777", requestLogger(mux))
}

0 comments on commit 3abb9fb

Please sign in to comment.