-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
38 lines (32 loc) · 887 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
package main
import (
"github.com/wwgberlin/repl2go/handler"
"log"
"net/http"
"time"
)
// logging middleware that logs the incoming requests
func logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// log request
log.Printf("%s %s on %s%s\n", r.Proto, r.Method, r.Host, r.URL)
// hand over to next handler
next.ServeHTTP(w, r)
// log response
end := time.Now()
duration := end.Sub(start)
log.Printf("done in %fs!", duration.Seconds())
})
}
func main() {
mux := http.NewServeMux()
finalHandler := http.HandlerFunc(handler.RunHandler)
mux.Handle("/run", logger(finalHandler))
mux.Handle("/", logger(http.NotFoundHandler()))
log.Println("Starting server on localhost:8000")
err := http.ListenAndServe(":8000", mux)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}