You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. )
  15. var (
  16. // SupportedDatabases includes all supported databases type
  17. SupportedDatabases = []string{"MySQL", "PostgreSQL", "MSSQL"}
  18. dbTypes = map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3"}
  19. // EnableSQLite3 use SQLite3, set by build flag
  20. EnableSQLite3 bool
  21. // Database holds the database settings
  22. Database = struct {
  23. Type string
  24. Host string
  25. Name string
  26. User string
  27. Passwd string
  28. Schema string
  29. SSLMode string
  30. Path string
  31. LogSQL bool
  32. Charset string
  33. Timeout int // seconds
  34. UseSQLite3 bool
  35. UseMySQL bool
  36. UseMSSQL bool
  37. UsePostgreSQL bool
  38. DBConnectRetries int
  39. DBConnectBackoff time.Duration
  40. MaxIdleConns int
  41. MaxOpenConns int
  42. ConnMaxLifetime time.Duration
  43. IterateBufferSize int
  44. }{
  45. Timeout: 500,
  46. IterateBufferSize: 50,
  47. }
  48. )
  49. // GetDBTypeByName returns the database type as it defined on XORM according the given name
  50. func GetDBTypeByName(name string) string {
  51. return dbTypes[name]
  52. }
  53. // InitDBConfig loads the database settings
  54. func InitDBConfig() {
  55. sec := Cfg.Section("database")
  56. Database.Type = sec.Key("DB_TYPE").String()
  57. defaultCharset := "utf8"
  58. switch Database.Type {
  59. case "sqlite3":
  60. Database.UseSQLite3 = true
  61. case "mysql":
  62. Database.UseMySQL = true
  63. defaultCharset = "utf8mb4"
  64. case "postgres":
  65. Database.UsePostgreSQL = true
  66. case "mssql":
  67. Database.UseMSSQL = true
  68. }
  69. Database.Host = sec.Key("HOST").String()
  70. Database.Name = sec.Key("NAME").String()
  71. Database.User = sec.Key("USER").String()
  72. if len(Database.Passwd) == 0 {
  73. Database.Passwd = sec.Key("PASSWD").String()
  74. }
  75. Database.Schema = sec.Key("SCHEMA").String()
  76. Database.SSLMode = sec.Key("SSL_MODE").MustString("disable")
  77. Database.Charset = sec.Key("CHARSET").In(defaultCharset, []string{"utf8", "utf8mb4"})
  78. Database.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "gitea.db"))
  79. Database.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
  80. Database.MaxIdleConns = sec.Key("MAX_IDLE_CONNS").MustInt(2)
  81. if Database.UseMySQL {
  82. Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFE_TIME").MustDuration(3 * time.Second)
  83. } else {
  84. Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFE_TIME").MustDuration(0)
  85. }
  86. Database.MaxOpenConns = sec.Key("MAX_OPEN_CONNS").MustInt(0)
  87. Database.IterateBufferSize = sec.Key("ITERATE_BUFFER_SIZE").MustInt(50)
  88. Database.LogSQL = sec.Key("LOG_SQL").MustBool(true)
  89. Database.DBConnectRetries = sec.Key("DB_RETRIES").MustInt(10)
  90. Database.DBConnectBackoff = sec.Key("DB_RETRY_BACKOFF").MustDuration(3 * time.Second)
  91. }
  92. // DBConnStr returns database connection string
  93. func DBConnStr() (string, error) {
  94. connStr := ""
  95. var Param = "?"
  96. if strings.Contains(Database.Name, Param) {
  97. Param = "&"
  98. }
  99. switch Database.Type {
  100. case "mysql":
  101. connType := "tcp"
  102. if len(Database.Host) > 0 && Database.Host[0] == '/' { // looks like a unix socket
  103. connType = "unix"
  104. }
  105. tls := Database.SSLMode
  106. if tls == "disable" { // allow (Postgres-inspired) default value to work in MySQL
  107. tls = "false"
  108. }
  109. connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
  110. Database.User, Database.Passwd, connType, Database.Host, Database.Name, Param, Database.Charset, tls)
  111. case "postgres":
  112. connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Param, Database.SSLMode)
  113. case "mssql":
  114. host, port := ParseMSSQLHostPort(Database.Host)
  115. connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd)
  116. case "sqlite3":
  117. if !EnableSQLite3 {
  118. return "", errors.New("this binary version does not build support for SQLite3")
  119. }
  120. if err := os.MkdirAll(path.Dir(Database.Path), os.ModePerm); err != nil {
  121. return "", fmt.Errorf("Failed to create directories: %v", err)
  122. }
  123. connStr = fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d&_txlock=immediate", Database.Path, Database.Timeout)
  124. default:
  125. return "", fmt.Errorf("Unknown database type: %s", Database.Type)
  126. }
  127. return connStr, nil
  128. }
  129. // parsePostgreSQLHostPort parses given input in various forms defined in
  130. // https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
  131. // and returns proper host and port number.
  132. func parsePostgreSQLHostPort(info string) (string, string) {
  133. host, port := "127.0.0.1", "5432"
  134. if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
  135. idx := strings.LastIndex(info, ":")
  136. host = info[:idx]
  137. port = info[idx+1:]
  138. } else if len(info) > 0 {
  139. host = info
  140. }
  141. return host, port
  142. }
  143. func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) {
  144. host, port := parsePostgreSQLHostPort(dbHost)
  145. if host[0] == '/' { // looks like a unix socket
  146. connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
  147. url.PathEscape(dbUser), url.PathEscape(dbPasswd), port, dbName, dbParam, dbsslMode, host)
  148. } else {
  149. connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
  150. url.PathEscape(dbUser), url.PathEscape(dbPasswd), host, port, dbName, dbParam, dbsslMode)
  151. }
  152. return
  153. }
  154. // ParseMSSQLHostPort splits the host into host and port
  155. func ParseMSSQLHostPort(info string) (string, string) {
  156. host, port := "127.0.0.1", "0"
  157. if strings.Contains(info, ":") {
  158. host = strings.Split(info, ":")[0]
  159. port = strings.Split(info, ":")[1]
  160. } else if strings.Contains(info, ",") {
  161. host = strings.Split(info, ",")[0]
  162. port = strings.TrimSpace(strings.Split(info, ",")[1])
  163. } else if len(info) > 0 {
  164. host = info
  165. }
  166. return host, port
  167. }