-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·53 lines (40 loc) · 849 Bytes
/
main.go
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
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"log"
"fmt"
"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)
requesterIP := r.RemoteAddr
log.Printf(
"%s\t\t%s\t\t%s\t\t%v",
r.Method,
r.RequestURI,
requesterIP,
time.Since(start),
)
})
}
func state() {
fmt.Println("Started Golang Webserver")
}
func main() {
go func() {
state()
}()
fileName := "./logs/log.txt"
logFile, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer logFile.Close()
log.SetOutput(logFile)
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("./public_html")))
log.Fatal(http.ListenAndServe(":8888", RequestLogger(mux)))
}