feat: allow to override ClientIP logic in gin

This commit is contained in:
Paul Pan 2024-01-05 00:38:28 +08:00
parent 86220fed14
commit 3a6dbf8595
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
5 changed files with 15 additions and 5 deletions

View File

@ -2,6 +2,7 @@ WebServer:
Address: ${WEB_SERVER_ADDRESS} Address: ${WEB_SERVER_ADDRESS}
Port: ${WEB_SERVER_PORT} Port: ${WEB_SERVER_PORT}
PublicBase: ${WEB_SERVER_PUBLIC_BASE} PublicBase: ${WEB_SERVER_PUBLIC_BASE}
TrustedPlatform: ${WEB_SERVER_TRUSTED_PLATFORM}
JwtSigningKey: ${WEB_SERVER_JWT_SIGNING_KEY} JwtSigningKey: ${WEB_SERVER_JWT_SIGNING_KEY}
JwtExpireHour: ${WEB_SERVER_JWT_EXPIRE_HOUR} JwtExpireHour: ${WEB_SERVER_JWT_EXPIRE_HOUR}
OAuthDomain: ${WEB_SERVER_OAUTH_DOMAIN} OAuthDomain: ${WEB_SERVER_OAUTH_DOMAIN}

View File

@ -37,6 +37,7 @@ function check_env() {
check_env "WEB_SERVER_ADDRESS" "0.0.0.0" true check_env "WEB_SERVER_ADDRESS" "0.0.0.0" true
check_env "WEB_SERVER_PORT" 8000 false check_env "WEB_SERVER_PORT" 8000 false
check_env "WEB_SERVER_PUBLIC_BASE" "http://127.0.0.1:8000" true check_env "WEB_SERVER_PUBLIC_BASE" "http://127.0.0.1:8000" true
check_env "WEB_SERVER_TRUSTED_PLATFORM" "" true
check_env "WEB_SERVER_JWT_SIGNING_KEY" "$(head -n 10 /dev/urandom | md5sum | cut -c 1-32)" true check_env "WEB_SERVER_JWT_SIGNING_KEY" "$(head -n 10 /dev/urandom | md5sum | cut -c 1-32)" true
check_env "WEB_SERVER_JWT_EXPIRE_HOUR" 12 false check_env "WEB_SERVER_JWT_EXPIRE_HOUR" 12 false
check_env "WEB_SERVER_OAUTH_DOMAIN" "" true check_env "WEB_SERVER_OAUTH_DOMAIN" "" true

View File

@ -4,6 +4,7 @@ type ConfigWebServer struct {
Address string `yaml:"Address"` Address string `yaml:"Address"`
Port int `yaml:"Port"` Port int `yaml:"Port"`
PublicBase string `yaml:"PublicBase"` PublicBase string `yaml:"PublicBase"`
TrustedPlatform string `yaml:"TrustedPlatform"`
JwtSigningKey string `yaml:"JwtSigningKey"` JwtSigningKey string `yaml:"JwtSigningKey"`
JwtExpireHour int `yaml:"JwtExpireHour"` JwtExpireHour int `yaml:"JwtExpireHour"`
OAuthDomain string `yaml:"OAuthDomain"` OAuthDomain string `yaml:"OAuthDomain"`

View File

@ -64,15 +64,20 @@ func (s *service) initRouters(conf *model.Config, injector *do.Injector) *gin.En
gin.SetMode(utils.If[string](conf.Development, gin.DebugMode, gin.ReleaseMode)) gin.SetMode(utils.If[string](conf.Development, gin.DebugMode, gin.ReleaseMode))
r := gin.New() r := gin.New()
r.MaxMultipartMemory = 8 << 20 // 8MB
// +--------------+
// |Configurations|
// +--------------+
if conf.WebServer.TrustedPlatform != "" {
// Extract Origin IP
r.TrustedPlatform = conf.WebServer.TrustedPlatform
}
// +-----------+ // +-----------+
// |Middlewares| // |Middlewares|
// +-----------+ // +-----------+
// static files - must before sentry
r.Use(static.Serve("/", static.LocalFile("./resource/frontend", true)))
// Sentry middleware // Sentry middleware
r.Use(sentrygin.New(sentrygin.Options{Repanic: true})) r.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
@ -141,6 +146,9 @@ func (s *service) initRouters(conf *model.Config, injector *do.Injector) *gin.En
r.GET(s.oauth.GetCallbackPath(), s.oauth.CallbackHandler()) r.GET(s.oauth.GetCallbackPath(), s.oauth.CallbackHandler())
} }
// static files
r.Use(static.Serve("/", static.LocalFile("./resource/frontend", true)))
// fallback to frontend // fallback to frontend
r.NoRoute(func(c *gin.Context) { r.NoRoute(func(c *gin.Context) {
c.File("./resource/frontend/index.html") c.File("./resource/frontend/index.html")

View File

@ -1 +0,0 @@
package web