Improve response logger and fix '/' route
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
4507728dae
commit
20fde2335e
6 changed files with 43 additions and 31 deletions
|
@ -51,6 +51,7 @@ type BackendConfig struct {
|
|||
type OpenConnectConfig struct {
|
||||
ClientConfigs []*storage.Client `json:"clients"`
|
||||
BackendConfigs []*BackendConfig `json:"backends"`
|
||||
Issuer string `json:"issuer"`
|
||||
}
|
||||
|
||||
type jsonConf struct {
|
||||
|
@ -96,7 +97,6 @@ func (ac *AppConfig) UnmarshalJSON(data []byte) error {
|
|||
return fmt.Errorf("failed to parse server listening mode: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println(jsonConf)
|
||||
ac.ServerMode = lm
|
||||
ac.SockPath = jsonConf.Server.SockPath
|
||||
ac.Host = jsonConf.Server.Host
|
||||
|
|
|
@ -23,12 +23,14 @@ func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
type IndexController struct {
|
||||
l *logrus.Logger
|
||||
l *logrus.Logger
|
||||
downstreamConstroller http.Handler
|
||||
}
|
||||
|
||||
func NewIndexController(l *logrus.Logger) *IndexController {
|
||||
func NewIndexController(l *logrus.Logger, downstream http.Handler) *IndexController {
|
||||
return &IndexController{
|
||||
l: l,
|
||||
l: l,
|
||||
downstreamConstroller: downstream,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,8 +55,7 @@ func (ic IndexController) serveUI(w http.ResponseWriter, r *http.Request) (int,
|
|||
|
||||
func (ic *IndexController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.RequestURI != "/" {
|
||||
ic.l.Errorf("Unhandled route %q", r.RequestURI)
|
||||
helpers.HandleResponse(w, r, http.StatusNotFound, nil, ic.l)
|
||||
ic.downstreamConstroller.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
returncode, contentLength, err := ic.serveUI(w, r)
|
||||
|
|
|
@ -52,7 +52,7 @@ func main() {
|
|||
Theme: "default",
|
||||
},
|
||||
Storage: memory.New(logger.L),
|
||||
Issuer: "http://127.0.0.1:5000",
|
||||
Issuer: conf.OpenConnectConfig.Issuer,
|
||||
SupportedResponseTypes: []string{"code"},
|
||||
SkipApprovalScreen: true,
|
||||
AllowedOrigins: []string{"*"},
|
||||
|
|
|
@ -5,25 +5,46 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/helpers"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var defaultResponseInfo = helpers.ResponseInfo{
|
||||
ReturnCode: -1,
|
||||
ContentLength: -1,
|
||||
type loggedResponseWriter struct {
|
||||
w http.ResponseWriter
|
||||
statusCode int
|
||||
contentLength int
|
||||
}
|
||||
|
||||
func (lr *loggedResponseWriter) Header() http.Header {
|
||||
return lr.w.Header()
|
||||
}
|
||||
|
||||
func (lr *loggedResponseWriter) Write(dat []byte) (int, error) {
|
||||
if lr.statusCode < 100 {
|
||||
lr.statusCode = http.StatusOK
|
||||
}
|
||||
res, err := lr.w.Write(dat)
|
||||
lr.contentLength += res
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (lr *loggedResponseWriter) WriteHeader(statusCode int) {
|
||||
lr.statusCode = statusCode
|
||||
lr.w.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
type LoggerMiddleware struct {
|
||||
l *logrus.Logger
|
||||
h http.Handler
|
||||
}
|
||||
|
||||
func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
responseInfo, ok := r.Context().Value(helpers.ResponseInfoKey).(helpers.ResponseInfo)
|
||||
if !ok {
|
||||
lm.l.Errorf("Failed to read response info from context, got %v", r.Context().Value("response_info"))
|
||||
responseInfo = defaultResponseInfo
|
||||
recorder := loggedResponseWriter{
|
||||
contentLength: -1,
|
||||
statusCode: -1,
|
||||
w: w,
|
||||
}
|
||||
lm.h.ServeHTTP(&recorder, r)
|
||||
|
||||
method := r.Method
|
||||
route := r.RequestURI
|
||||
currentTime := time.Now().UTC()
|
||||
|
@ -35,5 +56,5 @@ func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
clientIP = "unknown"
|
||||
}
|
||||
|
||||
lm.l.Infof(`%s - [%s] "%s %s %s" %d %d`, clientIP, currentTime.Format(time.RFC3339), method, route, httpVersion, responseInfo.ReturnCode, responseInfo.ContentLength)
|
||||
lm.l.Infof(`%s - [%s] "%s %s %s" %d %d`, clientIP, currentTime.Format(time.RFC3339), method, route, httpVersion, recorder.statusCode, recorder.contentLength)
|
||||
}
|
||||
|
|
|
@ -6,18 +6,9 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type MiddlewareChains struct {
|
||||
handlers []http.Handler
|
||||
}
|
||||
|
||||
func (mc *MiddlewareChains) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
for _, h := range mc.handlers {
|
||||
h.ServeHTTP(rw, r)
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(handler http.Handler, l *logrus.Logger) *MiddlewareChains {
|
||||
return &MiddlewareChains{
|
||||
handlers: []http.Handler{handler, &LoggerMiddleware{l}},
|
||||
func WithLogger(handler http.Handler, l *logrus.Logger) http.Handler {
|
||||
return &LoggerMiddleware{
|
||||
l: l,
|
||||
h: handler,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,8 +65,7 @@ func New(appConf *config.AppConfig, dexSrv *dex_server.Server, logger *logrus.Lo
|
|||
|
||||
controllers := map[string]http.Handler{
|
||||
ui.StaticRoute: &ui.StaticController{},
|
||||
"/": middlewares.WithLogger(dexSrv, logger),
|
||||
// "/": middlewares.WithLogger(ui.NewIndexController(logger), logger),
|
||||
"/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv), logger),
|
||||
}
|
||||
|
||||
m := http.NewServeMux()
|
||||
|
|
Loading…
Reference in a new issue