mirror of
https://github.com/go-gitea/gitea
synced 2024-12-25 18:14:28 +00:00
1bfb0a24d8
* refactor routers directory * move func used for web and api to common * make corsHandler a function to prohibit side efects * rm unused func Co-authored-by: 6543 <6543@obermui.de>
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package common
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/log"
|
|
)
|
|
|
|
// LoggerHandler is a handler that will log the routing to the default gitea log
|
|
func LoggerHandler(level log.Level) func(next http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
start := time.Now()
|
|
|
|
_ = log.GetLogger("router").Log(0, level, "Started %s %s for %s", log.ColoredMethod(req.Method), req.URL.RequestURI(), req.RemoteAddr)
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
var status int
|
|
if v, ok := w.(context.ResponseWriter); ok {
|
|
status = v.Status()
|
|
}
|
|
|
|
_ = log.GetLogger("router").Log(0, level, "Completed %s %s %v %s in %v", log.ColoredMethod(req.Method), req.URL.RequestURI(), log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(status)), log.ColoredTime(time.Since(start)))
|
|
})
|
|
}
|
|
}
|