Skip to content

Commit 1e17763

Browse files
unknwonachilleas-k
authored andcommitted
conf: overhaul database settings
1 parent 760d040 commit 1e17763

File tree

15 files changed

+155
-138
lines changed

15 files changed

+155
-138
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ All notable changes to Gogs are documented in this file.
2020
- Configuration option `APP_NAME` is deprecated and will end support in 0.13.0, please start using `BRAND_NAME`.
2121
- Configuration option `[server] ROOT_URL` is deprecated and will end support in 0.13.0, please start using `[server] EXTERNAL_URL`.
2222
- Configuration option `[server] LANDING_PAGE` is deprecated and will end support in 0.13.0, please start using `[server] LANDING_URL`.
23+
- Configuration option `[database] DB_TYPE` is deprecated and will end support in 0.13.0, please start using `[database] TYPE`.
24+
- Configuration option `[database] PASSWD` is deprecated and will end support in 0.13.0, please start using `[database] PASSWORD`.
2325

2426
### Fixed
2527

conf/app.ini

+13-12
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ ANNEX_FILE_MIN_SIZE = 10
145145
; The maximum number of files per upload.
146146
MAX_FILES = 5
147147

148+
[database]
149+
; The database backend, either "postgres", "mysql" "sqlite3" or "mssql".
150+
; You can connect to TiDB with MySQL protocol.
151+
TYPE = postgres
152+
HOST = 127.0.0.1:5432
153+
NAME = gogs
154+
USER = gogs
155+
PASSWORD =
156+
; For "postgres" only, either "disable", "require" or "verify-full".
157+
SSL_MODE = disable
158+
; For "sqlite3" only, make sure to use absolute path.
159+
PATH = data/gogs.db
160+
148161
; Attachment settings for releases
149162
[release.attachment]
150163
; Whether attachments are enabled. Defaults to `true`
@@ -179,18 +192,6 @@ ANGLED_QUOTES = true
179192
; Value for Access-Control-Allow-Origin header, default is not to present
180193
ACCESS_CONTROL_ALLOW_ORIGIN =
181194

182-
[database]
183-
; Either "mysql", "postgres" or "sqlite3", you can connect to TiDB with MySQL protocol
184-
DB_TYPE = mysql
185-
HOST = 127.0.0.1:3306
186-
NAME = gogs
187-
USER = root
188-
PASSWD =
189-
; For "postgres" only, either "disable", "require" or "verify-full"
190-
SSL_MODE = disable
191-
; For "sqlite3" and "tidb", use absolute path when you start as service
192-
PATH = data/gogs.db
193-
194195
[admin]
195196
; Disable regular (non-admin) users to create organizations
196197
DISABLE_REGULAR_ORG_CREATION = false

conf/locale/locale_en-US.ini

+10-10
Original file line numberDiff line numberDiff line change
@@ -1233,22 +1233,22 @@ config.repo.upload.allowed_types = Upload allowed types
12331233
config.repo.upload.file_max_size = Upload file size limit
12341234
config.repo.upload.max_files = Upload files limit
12351235
1236+
config.db_config = Database configuration
1237+
config.db.type = Type
1238+
config.db.host = Host
1239+
config.db.name = Name
1240+
config.db.user = User
1241+
config.db.ssl_mode = SSL mode
1242+
config.db.ssl_mode_helper = (for "postgres" only)
1243+
config.db.path = Path
1244+
config.db.path_helper = (for "sqlite3"only)
1245+
12361246
config.log_file_root_path = Log File Root Path
12371247
config.reverse_auth_user = Reverse Authentication User
12381248
12391249
config.http_config = HTTP Configuration
12401250
config.http_access_control_allow_origin = Access Control Allow Origin
12411251
1242-
config.db_config = Database Configuration
1243-
config.db_type = Type
1244-
config.db_host = Host
1245-
config.db_name = Name
1246-
config.db_user = User
1247-
config.db_ssl_mode = SSL Mode
1248-
config.db_ssl_mode_helper = (for "postgres" only)
1249-
config.db_path = Path
1250-
config.db_path_helper = (for "sqlite3" and "tidb")
1251-
12521252
config.service_config = Service Configuration
12531253
config.register_email_confirm = Require Email Confirmation
12541254
config.disable_register = Disable Registration

internal/assets/conf/conf_gen.go

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/assets/templates/templates_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/admin.go

-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ func runCreateUser(c *cli.Context) error {
147147
return errors.Wrap(err, "init configuration")
148148
}
149149

150-
db.LoadConfigs()
151150
db.SetEngine()
152151

153152
if err := db.CreateUser(&db.User{
@@ -171,7 +170,6 @@ func adminDashboardOperation(operation func() error, successMessage string) func
171170
return errors.Wrap(err, "init configuration")
172171
}
173172

174-
db.LoadConfigs()
175173
db.SetEngine()
176174

177175
if err := operation(); err != nil {

internal/cmd/backup.go

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func runBackup(c *cli.Context) error {
5252
return errors.Wrap(err, "init configuration")
5353
}
5454

55-
db.LoadConfigs()
5655
db.SetEngine()
5756

5857
tmpDir := c.String("tempdir")

internal/cmd/restore.go

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ func runRestore(c *cli.Context) error {
9999
return errors.Wrap(err, "init configuration")
100100
}
101101

102-
db.LoadConfigs()
103102
db.SetEngine()
104103

105104
// Database

internal/cmd/serv.go

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ func setup(c *cli.Context, logPath string, connectDB bool) {
8484
return
8585
}
8686

87-
db.LoadConfigs()
88-
8987
if conf.UseSQLite3 {
9088
_ = os.Chdir(conf.WorkDir())
9189
}

internal/conf/conf.go

+9
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ func Init(customConf string) error {
182182
Repository.Root = ensureAbs(Repository.Root)
183183
Repository.Upload.TempPath = ensureAbs(Repository.Upload.TempPath)
184184

185+
// *******************************
186+
// ----- Database settings -----
187+
// *******************************
188+
189+
if err = File.Section("database").MapTo(&Database); err != nil {
190+
return errors.Wrap(err, "mapping [database] section")
191+
}
192+
Database.Path = ensureAbs(Database.Path)
193+
185194
handleDeprecated()
186195

187196
// TODO

internal/conf/static.go

+25
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ var (
130130
AnnexFileMinSize int64
131131
} `ini:"repository.upload"`
132132
}
133+
134+
// Database settings
135+
Database struct {
136+
Type string
137+
Host string
138+
Name string
139+
User string
140+
Password string
141+
SSLMode string `ini:"SSL_MODE"`
142+
Path string
143+
144+
// Deprecated: Use Type instead, will be removed in 0.13.
145+
DbType string
146+
// Deprecated: Use Password instead, will be removed in 0.13.
147+
Passwd string
148+
}
133149
)
134150

135151
// handleDeprecated transfers deprecated values to the new ones when set.
@@ -147,4 +163,13 @@ func handleDeprecated() {
147163
Server.LandingURL = "/explore"
148164
Server.LangdingPage = ""
149165
}
166+
167+
if Database.DbType != "" {
168+
Database.Type = Database.DbType
169+
Database.DbType = ""
170+
}
171+
if Database.Passwd != "" {
172+
Database.Password = Database.Passwd
173+
Database.Passwd = ""
174+
}
150175
}

internal/db/models.go

+26-44
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ var (
4949
tables []interface{}
5050
HasEngine bool
5151

52-
DbCfg struct {
53-
Type, Host, Name, User, Passwd, Path, SSLMode string
54-
}
55-
5652
EnableSQLite3 bool
5753
)
5854

@@ -74,29 +70,6 @@ func init() {
7470
}
7571
}
7672

77-
func LoadConfigs() {
78-
sec := conf.File.Section("database")
79-
DbCfg.Type = sec.Key("DB_TYPE").String()
80-
switch DbCfg.Type {
81-
case "sqlite3":
82-
conf.UseSQLite3 = true
83-
case "mysql":
84-
conf.UseMySQL = true
85-
case "postgres":
86-
conf.UsePostgreSQL = true
87-
case "mssql":
88-
conf.UseMSSQL = true
89-
}
90-
DbCfg.Host = sec.Key("HOST").String()
91-
DbCfg.Name = sec.Key("NAME").String()
92-
DbCfg.User = sec.Key("USER").String()
93-
if len(DbCfg.Passwd) == 0 {
94-
DbCfg.Passwd = sec.Key("PASSWD").String()
95-
}
96-
DbCfg.SSLMode = sec.Key("SSL_MODE").String()
97-
DbCfg.Path = sec.Key("PATH").MustString("data/gogs.db")
98-
}
99-
10073
// parsePostgreSQLHostPort parses given input in various forms defined in
10174
// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
10275
// and returns proper host and port number.
@@ -127,46 +100,55 @@ func parseMSSQLHostPort(info string) (string, string) {
127100
}
128101

129102
func getEngine() (*xorm.Engine, error) {
130-
connStr := ""
131-
var Param string = "?"
132-
if strings.Contains(DbCfg.Name, Param) {
103+
Param := "?"
104+
if strings.Contains(conf.Database.Name, Param) {
133105
Param = "&"
134106
}
135-
switch DbCfg.Type {
107+
108+
connStr := ""
109+
switch conf.Database.Type {
136110
case "mysql":
137-
if DbCfg.Host[0] == '/' { // looks like a unix socket
111+
conf.UseMySQL = true
112+
if conf.Database.Host[0] == '/' { // looks like a unix socket
138113
connStr = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8mb4&parseTime=true",
139-
DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
114+
conf.Database.User, conf.Database.Password, conf.Database.Host, conf.Database.Name, Param)
140115
} else {
141116
connStr = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8mb4&parseTime=true",
142-
DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
117+
conf.Database.User, conf.Database.Password, conf.Database.Host, conf.Database.Name, Param)
143118
}
144119
var engineParams = map[string]string{"rowFormat": "DYNAMIC"}
145-
return xorm.NewEngineWithParams(DbCfg.Type, connStr, engineParams)
120+
return xorm.NewEngineWithParams(conf.Database.Type, connStr, engineParams)
121+
146122
case "postgres":
147-
host, port := parsePostgreSQLHostPort(DbCfg.Host)
123+
conf.UsePostgreSQL = true
124+
host, port := parsePostgreSQLHostPort(conf.Database.Host)
148125
if host[0] == '/' { // looks like a unix socket
149126
connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
150-
url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), port, DbCfg.Name, Param, DbCfg.SSLMode, host)
127+
url.QueryEscape(conf.Database.User), url.QueryEscape(conf.Database.Password), port, conf.Database.Name, Param, conf.Database.SSLMode, host)
151128
} else {
152129
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
153-
url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode)
130+
url.QueryEscape(conf.Database.User), url.QueryEscape(conf.Database.Password), host, port, conf.Database.Name, Param, conf.Database.SSLMode)
154131
}
132+
155133
case "mssql":
156-
host, port := parseMSSQLHostPort(DbCfg.Host)
157-
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, DbCfg.Name, DbCfg.User, DbCfg.Passwd)
134+
conf.UseMSSQL = true
135+
host, port := parseMSSQLHostPort(conf.Database.Host)
136+
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, conf.Database.Name, conf.Database.User, conf.Database.Passwd)
137+
158138
case "sqlite3":
159139
if !EnableSQLite3 {
160140
return nil, errors.New("this binary version does not build support for SQLite3")
161141
}
162-
if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
142+
if err := os.MkdirAll(path.Dir(conf.Database.Path), os.ModePerm); err != nil {
163143
return nil, fmt.Errorf("create directories: %v", err)
164144
}
165-
connStr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc"
145+
conf.UseSQLite3 = true
146+
connStr = "file:" + conf.Database.Path + "?cache=shared&mode=rwc"
147+
166148
default:
167-
return nil, fmt.Errorf("unknown database type: %s", DbCfg.Type)
149+
return nil, fmt.Errorf("unknown database type: %s", conf.Database.Type)
168150
}
169-
return xorm.NewEngine(DbCfg.Type, connStr)
151+
return xorm.NewEngine(conf.Database.Type, connStr)
170152
}
171153

172154
func NewTestEngine(x *xorm.Engine) (err error) {

internal/route/admin/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ func Config(c *context.Context) {
205205
c.Data["Server"] = conf.Server
206206
c.Data["SSH"] = conf.SSH
207207
c.Data["Repository"] = conf.Repository
208+
c.Data["Database"] = conf.Database
208209

209210
c.Data["LogRootPath"] = conf.LogRootPath
210211
c.Data["ReverseProxyAuthUser"] = conf.ReverseProxyAuthUser
211212

212213
c.Data["HTTP"] = conf.HTTP
213214

214-
c.Data["DbCfg"] = db.DbCfg
215215
c.Data["Service"] = conf.Service
216216
c.Data["Webhook"] = conf.Webhook
217217

internal/route/install.go

+33-29
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func GlobalInit(customConf string) error {
6464
log.Trace("Build time: %s", conf.BuildTime)
6565
log.Trace("Build commit: %s", conf.BuildCommit)
6666

67-
db.LoadConfigs()
6867
conf.NewServices()
6968
mailer.NewContext()
7069

@@ -136,15 +135,15 @@ func Install(c *context.Context) {
136135
f := form.Install{}
137136

138137
// Database settings
139-
f.DbHost = db.DbCfg.Host
140-
f.DbUser = db.DbCfg.User
141-
f.DbName = db.DbCfg.Name
142-
f.DbPath = db.DbCfg.Path
143-
144-
c.Data["CurDbOption"] = "MySQL"
145-
switch db.DbCfg.Type {
146-
case "postgres":
147-
c.Data["CurDbOption"] = "PostgreSQL"
138+
f.DbHost = conf.Database.Host
139+
f.DbUser = conf.Database.User
140+
f.DbName = conf.Database.Name
141+
f.DbPath = conf.Database.Path
142+
143+
c.Data["CurDbOption"] = "PostgreSQL"
144+
switch conf.Database.Type {
145+
case "mysql":
146+
c.Data["CurDbOption"] = "MySQL"
148147
case "mssql":
149148
c.Data["CurDbOption"] = "MSSQL"
150149
case "sqlite3":
@@ -217,16 +216,21 @@ func InstallPost(c *context.Context, f form.Install) {
217216

218217
// Pass basic check, now test configuration.
219218
// Test database setting.
220-
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3", "TiDB": "tidb"}
221-
db.DbCfg.Type = dbTypes[f.DbType]
222-
db.DbCfg.Host = f.DbHost
223-
db.DbCfg.User = f.DbUser
224-
db.DbCfg.Passwd = f.DbPasswd
225-
db.DbCfg.Name = f.DbName
226-
db.DbCfg.SSLMode = f.SSLMode
227-
db.DbCfg.Path = f.DbPath
228-
229-
if db.DbCfg.Type == "sqlite3" && len(db.DbCfg.Path) == 0 {
219+
dbTypes := map[string]string{
220+
"PostgreSQL": "postgres",
221+
"MySQL": "mysql",
222+
"MSSQL": "mssql",
223+
"SQLite3": "sqlite3",
224+
}
225+
conf.Database.Type = dbTypes[f.DbType]
226+
conf.Database.Host = f.DbHost
227+
conf.Database.User = f.DbUser
228+
conf.Database.Password = f.DbPasswd
229+
conf.Database.Name = f.DbName
230+
conf.Database.SSLMode = f.SSLMode
231+
conf.Database.Path = f.DbPath
232+
233+
if conf.Database.Type == "sqlite3" && len(conf.Database.Path) == 0 {
230234
c.FormErr("DbPath")
231235
c.RenderWithErr(c.Tr("install.err_empty_db_path"), INSTALL, &f)
232236
return
@@ -316,20 +320,20 @@ func InstallPost(c *context.Context, f form.Install) {
316320
log.Error("Failed to load custom conf %q: %v", conf.CustomConf, err)
317321
}
318322
}
319-
cfg.Section("database").Key("DB_TYPE").SetValue(db.DbCfg.Type)
320-
cfg.Section("database").Key("HOST").SetValue(db.DbCfg.Host)
321-
cfg.Section("database").Key("NAME").SetValue(db.DbCfg.Name)
322-
cfg.Section("database").Key("USER").SetValue(db.DbCfg.User)
323-
cfg.Section("database").Key("PASSWD").SetValue(db.DbCfg.Passwd)
324-
cfg.Section("database").Key("SSL_MODE").SetValue(db.DbCfg.SSLMode)
325-
cfg.Section("database").Key("PATH").SetValue(db.DbCfg.Path)
323+
cfg.Section("database").Key("TYPE").SetValue(conf.Database.Type)
324+
cfg.Section("database").Key("HOST").SetValue(conf.Database.Host)
325+
cfg.Section("database").Key("NAME").SetValue(conf.Database.Name)
326+
cfg.Section("database").Key("USER").SetValue(conf.Database.User)
327+
cfg.Section("database").Key("PASSWORD").SetValue(conf.Database.Password)
328+
cfg.Section("database").Key("SSL_MODE").SetValue(conf.Database.SSLMode)
329+
cfg.Section("database").Key("PATH").SetValue(conf.Database.Path)
326330

327-
cfg.Section("").Key("APP_NAME").SetValue(f.AppName)
331+
cfg.Section("").Key("BRAND_NAME").SetValue(f.AppName)
328332
cfg.Section("repository").Key("ROOT").SetValue(f.RepoRootPath)
329333
cfg.Section("").Key("RUN_USER").SetValue(f.RunUser)
330334
cfg.Section("server").Key("DOMAIN").SetValue(f.Domain)
331335
cfg.Section("server").Key("HTTP_PORT").SetValue(f.HTTPPort)
332-
cfg.Section("server").Key("ROOT_URL").SetValue(f.AppUrl)
336+
cfg.Section("server").Key("EXTERNAL_URL").SetValue(f.AppUrl)
333337

334338
if f.SSHPort == 0 {
335339
cfg.Section("server").Key("DISABLE_SSH").SetValue("true")

0 commit comments

Comments
 (0)