You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
synctv/server/middlewares/init.go

34 lines
919 B
Go

package middlewares
import (
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/synctv-org/synctv/internal/conf"
limiter "github.com/ulule/limiter/v3"
)
func Init(e *gin.Engine) {
w := log.StandardLogger().Writer()
e.
Use(gin.LoggerWithWriter(w), gin.RecoveryWithWriter(w)).
Use(NewCors())
if conf.Conf.RateLimit.Enable {
d, err := time.ParseDuration(conf.Conf.RateLimit.Period)
if err != nil {
log.Fatal(err)
}
options := []limiter.Option{
limiter.WithTrustForwardHeader(conf.Conf.RateLimit.TrustForwardHeader),
}
if conf.Conf.RateLimit.TrustedClientIPHeader != "" {
options = append(options, limiter.WithClientIPHeader(conf.Conf.RateLimit.TrustedClientIPHeader))
}
e.Use(NewLimiter(d, conf.Conf.RateLimit.Limit, options...))
}
if conf.Conf.Server.Quic && conf.Conf.Server.CertPath != "" && conf.Conf.Server.KeyPath != "" {
e.Use(NewQuic())
}
}