Skip to content

Commit 79ce91f

Browse files
lunnylafriks
andauthored
Movde dependents on macaron from modules/setting (#10050)
Co-authored-by: Lauris BH <[email protected]>
1 parent c09e020 commit 79ce91f

File tree

6 files changed

+74
-35
lines changed

6 files changed

+74
-35
lines changed

modules/cache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"code.gitea.io/gitea/modules/setting"
1212

1313
mc "gitea.com/macaron/cache"
14+
15+
_ "gitea.com/macaron/cache/memcache" // memcache plugin for cache
16+
_ "gitea.com/macaron/cache/redis"
1417
)
1518

1619
var conn mc.Cache

modules/setting/cache.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import (
99
"time"
1010

1111
"code.gitea.io/gitea/modules/log"
12-
13-
_ "gitea.com/macaron/cache/memcache" // memcache plugin for cache
14-
_ "gitea.com/macaron/cache/redis"
1512
)
1613

1714
// Cache represents cache settings

modules/setting/cors.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,31 @@ import (
88
"time"
99

1010
"code.gitea.io/gitea/modules/log"
11-
12-
"gitea.com/macaron/cors"
1311
)
1412

1513
var (
1614
// CORSConfig defines CORS settings
17-
CORSConfig cors.Options
18-
// EnableCORS defines whether CORS settings is enabled or not
19-
EnableCORS bool
15+
CORSConfig = struct {
16+
Enabled bool
17+
Scheme string
18+
AllowDomain []string
19+
AllowSubdomain bool
20+
Methods []string
21+
MaxAge time.Duration
22+
AllowCredentials bool
23+
}{
24+
Enabled: false,
25+
MaxAge: 10 * time.Minute,
26+
}
2027
)
2128

2229
func newCORSService() {
2330
sec := Cfg.Section("cors")
24-
// Check cors setting.
25-
EnableCORS = sec.Key("ENABLED").MustBool(false)
26-
27-
maxAge := sec.Key("MAX_AGE").MustDuration(10 * time.Minute)
28-
29-
CORSConfig = cors.Options{
30-
Scheme: sec.Key("SCHEME").String(),
31-
AllowDomain: sec.Key("ALLOW_DOMAIN").Strings(","),
32-
AllowSubdomain: sec.Key("ALLOW_SUBDOMAIN").MustBool(),
33-
Methods: sec.Key("METHODS").Strings(","),
34-
MaxAgeSeconds: int(maxAge.Seconds()),
35-
AllowCredentials: sec.Key("ALLOW_CREDENTIALS").MustBool(),
31+
if err := sec.MapTo(&CORSConfig); err != nil {
32+
log.Fatal("Failed to map cors settings: %v", err)
3633
}
3734

38-
if EnableCORS {
35+
if CORSConfig.Enabled {
3936
log.Info("CORS Service Enabled")
4037
}
4138
}

modules/setting/session.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,47 @@ import (
1111
"strings"
1212

1313
"code.gitea.io/gitea/modules/log"
14-
15-
"gitea.com/macaron/session"
1614
)
1715

1816
var (
1917
// SessionConfig difines Session settings
20-
SessionConfig session.Options
18+
SessionConfig = struct {
19+
Provider string
20+
// Provider configuration, it's corresponding to provider.
21+
ProviderConfig string
22+
// Cookie name to save session ID. Default is "MacaronSession".
23+
CookieName string
24+
// Cookie path to store. Default is "/".
25+
CookiePath string
26+
// GC interval time in seconds. Default is 3600.
27+
Gclifetime int64
28+
// Max life time in seconds. Default is whatever GC interval time is.
29+
Maxlifetime int64
30+
// Use HTTPS only. Default is false.
31+
Secure bool
32+
// Cookie domain name. Default is empty.
33+
Domain string
34+
}{
35+
CookieName: "i_like_gitea",
36+
Gclifetime: 86400,
37+
Maxlifetime: 86400,
38+
}
2139
)
2240

2341
func newSessionService() {
24-
SessionConfig.Provider = Cfg.Section("session").Key("PROVIDER").In("memory",
42+
sec := Cfg.Section("session")
43+
SessionConfig.Provider = sec.Key("PROVIDER").In("memory",
2544
[]string{"memory", "file", "redis", "mysql", "postgres", "couchbase", "memcache", "nodb"})
26-
SessionConfig.ProviderConfig = strings.Trim(Cfg.Section("session").Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
45+
SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
2746
if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
2847
SessionConfig.ProviderConfig = path.Join(AppWorkPath, SessionConfig.ProviderConfig)
2948
}
30-
SessionConfig.CookieName = Cfg.Section("session").Key("COOKIE_NAME").MustString("i_like_gitea")
49+
SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
3150
SessionConfig.CookiePath = AppSubURL
32-
SessionConfig.Secure = Cfg.Section("session").Key("COOKIE_SECURE").MustBool(false)
33-
SessionConfig.Gclifetime = Cfg.Section("session").Key("GC_INTERVAL_TIME").MustInt64(86400)
34-
SessionConfig.Maxlifetime = Cfg.Section("session").Key("SESSION_LIFE_TIME").MustInt64(86400)
35-
SessionConfig.Domain = Cfg.Section("session").Key("DOMAIN").String()
51+
SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(false)
52+
SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
53+
SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
54+
SessionConfig.Domain = sec.Key("DOMAIN").String()
3655

3756
shadowConfig, err := json.Marshal(SessionConfig)
3857
if err != nil {

routers/admin/admin.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,14 @@ func Config(ctx *context.Context) {
319319
if err := json.Unmarshal([]byte(sessionCfg.ProviderConfig), &realSession); err != nil {
320320
log.Error("Unable to unmarshall session config for virtualed provider config: %s\nError: %v", sessionCfg.ProviderConfig, err)
321321
}
322-
sessionCfg = realSession
322+
sessionCfg.Provider = realSession.Provider
323+
sessionCfg.ProviderConfig = realSession.ProviderConfig
324+
sessionCfg.CookieName = realSession.CookieName
325+
sessionCfg.CookiePath = realSession.CookiePath
326+
sessionCfg.Gclifetime = realSession.Gclifetime
327+
sessionCfg.Maxlifetime = realSession.Maxlifetime
328+
sessionCfg.Secure = realSession.Secure
329+
sessionCfg.Domain = realSession.Domain
323330
}
324331
sessionCfg.ProviderConfig = shadowPassword(sessionCfg.Provider, sessionCfg.ProviderConfig)
325332
ctx.Data["SessionConfig"] = sessionCfg

routers/routes/routes.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,16 @@ func NewMacaron() *macaron.Macaron {
201201
m.Use(captcha.Captchaer(captcha.Options{
202202
SubURL: setting.AppSubURL,
203203
}))
204-
m.Use(session.Sessioner(setting.SessionConfig))
204+
m.Use(session.Sessioner(session.Options{
205+
Provider: setting.SessionConfig.Provider,
206+
ProviderConfig: setting.SessionConfig.ProviderConfig,
207+
CookieName: setting.SessionConfig.CookieName,
208+
CookiePath: setting.SessionConfig.CookiePath,
209+
Gclifetime: setting.SessionConfig.Gclifetime,
210+
Maxlifetime: setting.SessionConfig.Maxlifetime,
211+
Secure: setting.SessionConfig.Secure,
212+
Domain: setting.SessionConfig.Domain,
213+
}))
205214
m.Use(csrf.Csrfer(csrf.Options{
206215
Secret: setting.SecretKey,
207216
Cookie: setting.CSRFCookieName,
@@ -963,8 +972,15 @@ func RegisterRoutes(m *macaron.Macaron) {
963972
}
964973

965974
var handlers []macaron.Handler
966-
if setting.EnableCORS {
967-
handlers = append(handlers, cors.CORS(setting.CORSConfig))
975+
if setting.CORSConfig.Enabled {
976+
handlers = append(handlers, cors.CORS(cors.Options{
977+
Scheme: setting.CORSConfig.Scheme,
978+
AllowDomain: setting.CORSConfig.AllowDomain,
979+
AllowSubdomain: setting.CORSConfig.AllowSubdomain,
980+
Methods: setting.CORSConfig.Methods,
981+
MaxAgeSeconds: int(setting.CORSConfig.MaxAge.Seconds()),
982+
AllowCredentials: setting.CORSConfig.AllowCredentials,
983+
}))
968984
}
969985
handlers = append(handlers, ignSignIn)
970986
m.Group("/api", func() {

0 commit comments

Comments
 (0)