diff --git a/polyculeconnect/config/config.go b/polyculeconnect/config/config.go index d39b699..a44f379 100644 --- a/polyculeconnect/config/config.go +++ b/polyculeconnect/config/config.go @@ -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 diff --git a/polyculeconnect/controller/ui/static.go b/polyculeconnect/controller/ui/static.go index e16d2ff..5ede25c 100644 --- a/polyculeconnect/controller/ui/static.go +++ b/polyculeconnect/controller/ui/static.go @@ -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) diff --git a/polyculeconnect/main.go b/polyculeconnect/main.go index c670e2b..55a79b4 100644 --- a/polyculeconnect/main.go +++ b/polyculeconnect/main.go @@ -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{"*"}, diff --git a/polyculeconnect/middlewares/logger.go b/polyculeconnect/middlewares/logger.go index 79217b6..6c82ac3 100644 --- a/polyculeconnect/middlewares/logger.go +++ b/polyculeconnect/middlewares/logger.go @@ -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) } diff --git a/polyculeconnect/middlewares/middlewarechain.go b/polyculeconnect/middlewares/middlewarechain.go index ac5070d..f1eb3ea 100644 --- a/polyculeconnect/middlewares/middlewarechain.go +++ b/polyculeconnect/middlewares/middlewarechain.go @@ -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, } } diff --git a/polyculeconnect/server/server.go b/polyculeconnect/server/server.go index d57d9a6..2b6ee78 100644 --- a/polyculeconnect/server/server.go +++ b/polyculeconnect/server/server.go @@ -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()