Add full and configurable access logging
This commit is contained in:
parent
c16aa1603d
commit
3623fb8e8e
3 changed files with 24 additions and 4 deletions
|
|
@ -21,8 +21,8 @@ func main() {
|
||||||
router := http.NewServeMux()
|
router := http.NewServeMux()
|
||||||
notesRouter := views.GetRoutes("/notes")
|
notesRouter := views.GetRoutes("/notes")
|
||||||
|
|
||||||
router.Handle("/", http.RedirectHandler("/notes/", http.StatusFound))
|
router.Handle("/", logger(http.RedirectHandler("/notes/", http.StatusFound)))
|
||||||
router.Handle("/notes/", http.StripPrefix("/notes", notesRouter))
|
router.Handle("/notes/", logger(http.StripPrefix("/notes", notesRouter)))
|
||||||
router.Handle(
|
router.Handle(
|
||||||
"/static/",
|
"/static/",
|
||||||
logger(
|
logger(
|
||||||
|
|
@ -37,9 +37,27 @@ func main() {
|
||||||
log.Fatal(http.Serve(listener, router))
|
log.Fatal(http.Serve(listener, router))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type loggingResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
statusCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
||||||
|
return &loggingResponseWriter{w, http.StatusOK}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *loggingResponseWriter) WriteHeader(code int) {
|
||||||
|
w.statusCode = code
|
||||||
|
w.ResponseWriter.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
func logger(next http.Handler) http.Handler {
|
func logger(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Print(r.URL.Path)
|
lwr := NewLoggingResponseWriter(w)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(lwr, r)
|
||||||
|
|
||||||
|
if conf.Conf.LogAccess {
|
||||||
|
log.Printf("%s %s %s%s %d", r.RemoteAddr, r.Method, r.Host, r.URL.Path, lwr.statusCode)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,3 +2,4 @@ extension = "md"
|
||||||
notesdir = "/var/lib/gonotes/saved_notes"
|
notesdir = "/var/lib/gonotes/saved_notes"
|
||||||
address = ":8080"
|
address = ":8080"
|
||||||
protocol = "tcp"
|
protocol = "tcp"
|
||||||
|
logAccess = false
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ type Config struct {
|
||||||
Protocol string
|
Protocol string
|
||||||
Extension string
|
Extension string
|
||||||
NotesDir string
|
NotesDir string
|
||||||
|
LogAccess bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue