forked from forgejo/forgejo
Unified custom config creation (#16012)
* Unified custom config creation. * Fixed log message. Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
2a998048ef
commit
efe77eec85
3 changed files with 39 additions and 82 deletions
|
@ -793,27 +793,10 @@ func NewContext() {
|
|||
log.Fatal("error generating JWT secret: %v", err)
|
||||
return
|
||||
}
|
||||
cfg := ini.Empty()
|
||||
isFile, err := util.IsFile(CustomConf)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
|
||||
}
|
||||
if isFile {
|
||||
if err := cfg.Append(CustomConf); err != nil {
|
||||
log.Error("failed to load custom conf %s: %v", CustomConf, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
cfg.Section("oauth2").Key("JWT_SECRET").SetValue(OAuth2.JWTSecretBase64)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
|
||||
log.Fatal("failed to create '%s': %v", CustomConf, err)
|
||||
return
|
||||
}
|
||||
if err := cfg.SaveTo(CustomConf); err != nil {
|
||||
log.Fatal("error saving generating JWT secret to custom config: %v", err)
|
||||
return
|
||||
}
|
||||
CreateOrAppendToCustomConf(func(cfg *ini.File) {
|
||||
cfg.Section("oauth2").Key("JWT_SECRET").SetValue(OAuth2.JWTSecretBase64)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1075,26 +1058,9 @@ func loadOrGenerateInternalToken(sec *ini.Section) string {
|
|||
}
|
||||
|
||||
// Save secret
|
||||
cfgSave := ini.Empty()
|
||||
isFile, err := util.IsFile(CustomConf)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
|
||||
}
|
||||
if isFile {
|
||||
// Keeps custom settings if there is already something.
|
||||
if err := cfgSave.Append(CustomConf); err != nil {
|
||||
log.Error("Failed to load custom conf '%s': %v", CustomConf, err)
|
||||
}
|
||||
}
|
||||
|
||||
cfgSave.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
|
||||
log.Fatal("Failed to create '%s': %v", CustomConf, err)
|
||||
}
|
||||
if err := cfgSave.SaveTo(CustomConf); err != nil {
|
||||
log.Fatal("Error saving generated INTERNAL_TOKEN to custom config: %v", err)
|
||||
}
|
||||
CreateOrAppendToCustomConf(func(cfg *ini.File) {
|
||||
cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
|
||||
})
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
@ -1160,6 +1126,32 @@ func MakeManifestData(appName string, appURL string, absoluteAssetURL string) []
|
|||
return bytes
|
||||
}
|
||||
|
||||
// CreateOrAppendToCustomConf creates or updates the custom config.
|
||||
// Use the callback to set individual values.
|
||||
func CreateOrAppendToCustomConf(callback func(cfg *ini.File)) {
|
||||
cfg := ini.Empty()
|
||||
isFile, err := util.IsFile(CustomConf)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
|
||||
}
|
||||
if isFile {
|
||||
if err := cfg.Append(CustomConf); err != nil {
|
||||
log.Error("failed to load custom conf %s: %v", CustomConf, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
callback(cfg)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
|
||||
log.Fatal("failed to create '%s': %v", CustomConf, err)
|
||||
return
|
||||
}
|
||||
if err := cfg.SaveTo(CustomConf); err != nil {
|
||||
log.Fatal("error saving to custom config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// NewServices initializes the services
|
||||
func NewServices() {
|
||||
InitDBConfig()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue