You've already forked ionscale
mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-04-05 12:32:58 +01:00
chore: improve echo handlers
This commit is contained in:
+17
-14
@@ -3,8 +3,8 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/labstack/echo-contrib/prometheus"
|
||||
"github.com/labstack/echo/v4"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -36,22 +36,25 @@ func EchoLogger(logger hclog.Logger) echo.MiddlewareFunc {
|
||||
}
|
||||
|
||||
func EchoRecover(logger hclog.Logger) echo.MiddlewareFunc {
|
||||
httpLogger := logger.Named("http")
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err, ok := r.(error)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%v", r)
|
||||
apply := func() (topErr error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err, ok := r.(error)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%v", r)
|
||||
}
|
||||
topErr = err
|
||||
}
|
||||
stack := make([]byte, 4<<10) // 4 KB
|
||||
length := runtime.Stack(stack, false)
|
||||
httpLogger.Error("panic handling request", "err", err, "stack", string(stack[:length]))
|
||||
c.Error(err)
|
||||
}
|
||||
}()
|
||||
return next(c)
|
||||
}()
|
||||
return next(c)
|
||||
}
|
||||
return apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func EchoMetrics(p *prometheus.Prometheus) echo.MiddlewareFunc {
|
||||
return p.HandlerFunc
|
||||
}
|
||||
|
||||
@@ -133,18 +133,14 @@ func Start(c *config.Config) error {
|
||||
p.SetMetricsPath(metricsHandler)
|
||||
|
||||
nonTlsAppHandler := echo.New()
|
||||
nonTlsAppHandler.Use(EchoRecover(logger))
|
||||
nonTlsAppHandler.Use(EchoLogger(logger))
|
||||
nonTlsAppHandler.Use(p.HandlerFunc)
|
||||
nonTlsAppHandler.Use(EchoMetrics(p), EchoLogger(logger), EchoRecover(logger))
|
||||
nonTlsAppHandler.POST("/ts2021", noiseHandlers.Upgrade)
|
||||
nonTlsAppHandler.Any("/*", handlers.HttpRedirectHandler(c.Tls))
|
||||
|
||||
tlsAppHandler := echo.New()
|
||||
tlsAppHandler.Pre(handlers.HttpsRedirect(c.Tls))
|
||||
tlsAppHandler.Renderer = templates.NewTemplates()
|
||||
tlsAppHandler.Use(EchoRecover(logger))
|
||||
tlsAppHandler.Use(EchoLogger(logger))
|
||||
tlsAppHandler.Use(p.HandlerFunc)
|
||||
tlsAppHandler.Pre(handlers.HttpsRedirect(c.Tls))
|
||||
tlsAppHandler.Use(EchoMetrics(p), EchoLogger(logger), EchoRecover(logger))
|
||||
|
||||
tlsAppHandler.Any("/*", handlers.IndexHandler(http.StatusNotFound))
|
||||
tlsAppHandler.Any("/", handlers.IndexHandler(http.StatusOK))
|
||||
|
||||
Reference in New Issue
Block a user