Improve response logger and fix '/' route
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Melora Hugues 2023-10-15 17:49:33 +02:00
parent 4507728dae
commit 20fde2335e
6 changed files with 43 additions and 31 deletions

View file

@ -51,6 +51,7 @@ type BackendConfig struct {
type OpenConnectConfig struct { type OpenConnectConfig struct {
ClientConfigs []*storage.Client `json:"clients"` ClientConfigs []*storage.Client `json:"clients"`
BackendConfigs []*BackendConfig `json:"backends"` BackendConfigs []*BackendConfig `json:"backends"`
Issuer string `json:"issuer"`
} }
type jsonConf struct { 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) return fmt.Errorf("failed to parse server listening mode: %w", err)
} }
fmt.Println(jsonConf)
ac.ServerMode = lm ac.ServerMode = lm
ac.SockPath = jsonConf.Server.SockPath ac.SockPath = jsonConf.Server.SockPath
ac.Host = jsonConf.Server.Host ac.Host = jsonConf.Server.Host

View file

@ -24,11 +24,13 @@ func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type IndexController struct { 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{ 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) { func (ic *IndexController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/" { if r.RequestURI != "/" {
ic.l.Errorf("Unhandled route %q", r.RequestURI) ic.downstreamConstroller.ServeHTTP(w, r)
helpers.HandleResponse(w, r, http.StatusNotFound, nil, ic.l)
return return
} }
returncode, contentLength, err := ic.serveUI(w, r) returncode, contentLength, err := ic.serveUI(w, r)

View file

@ -52,7 +52,7 @@ func main() {
Theme: "default", Theme: "default",
}, },
Storage: memory.New(logger.L), Storage: memory.New(logger.L),
Issuer: "http://127.0.0.1:5000", Issuer: conf.OpenConnectConfig.Issuer,
SupportedResponseTypes: []string{"code"}, SupportedResponseTypes: []string{"code"},
SkipApprovalScreen: true, SkipApprovalScreen: true,
AllowedOrigins: []string{"*"}, AllowedOrigins: []string{"*"},

View file

@ -5,25 +5,46 @@ import (
"net/http" "net/http"
"time" "time"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/helpers"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
var defaultResponseInfo = helpers.ResponseInfo{ type loggedResponseWriter struct {
ReturnCode: -1, w http.ResponseWriter
ContentLength: -1, 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 { type LoggerMiddleware struct {
l *logrus.Logger l *logrus.Logger
h http.Handler
} }
func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
responseInfo, ok := r.Context().Value(helpers.ResponseInfoKey).(helpers.ResponseInfo) recorder := loggedResponseWriter{
if !ok { contentLength: -1,
lm.l.Errorf("Failed to read response info from context, got %v", r.Context().Value("response_info")) statusCode: -1,
responseInfo = defaultResponseInfo w: w,
} }
lm.h.ServeHTTP(&recorder, r)
method := r.Method method := r.Method
route := r.RequestURI route := r.RequestURI
currentTime := time.Now().UTC() currentTime := time.Now().UTC()
@ -35,5 +56,5 @@ func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
clientIP = "unknown" 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)
} }

View file

@ -6,18 +6,9 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type MiddlewareChains struct { func WithLogger(handler http.Handler, l *logrus.Logger) http.Handler {
handlers []http.Handler return &LoggerMiddleware{
} l: l,
h: 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}},
} }
} }

View file

@ -65,8 +65,7 @@ func New(appConf *config.AppConfig, dexSrv *dex_server.Server, logger *logrus.Lo
controllers := map[string]http.Handler{ controllers := map[string]http.Handler{
ui.StaticRoute: &ui.StaticController{}, ui.StaticRoute: &ui.StaticController{},
"/": middlewares.WithLogger(dexSrv, logger), "/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv), logger),
// "/": middlewares.WithLogger(ui.NewIndexController(logger), logger),
} }
m := http.NewServeMux() m := http.NewServeMux()