|
|
@ -16,30 +16,39 @@ import ( |
|
|
|
"github.com/gogits/gogs/modules/base" |
|
|
|
) |
|
|
|
|
|
|
|
var orm *xorm.Engine |
|
|
|
var ( |
|
|
|
orm *xorm.Engine |
|
|
|
|
|
|
|
dbCfg struct { |
|
|
|
Type, Host, Name, User, Pwd, Path, SslMode string |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
func LoadModelsConfig() { |
|
|
|
dbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE") |
|
|
|
dbCfg.Host = base.Cfg.MustValue("database", "HOST") |
|
|
|
dbCfg.Name = base.Cfg.MustValue("database", "NAME") |
|
|
|
dbCfg.User = base.Cfg.MustValue("database", "USER") |
|
|
|
dbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD") |
|
|
|
dbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db") |
|
|
|
dbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE") |
|
|
|
} |
|
|
|
|
|
|
|
func setEngine() { |
|
|
|
dbType := base.Cfg.MustValue("database", "DB_TYPE") |
|
|
|
dbHost := base.Cfg.MustValue("database", "HOST") |
|
|
|
dbName := base.Cfg.MustValue("database", "NAME") |
|
|
|
dbUser := base.Cfg.MustValue("database", "USER") |
|
|
|
dbPwd := base.Cfg.MustValue("database", "PASSWD") |
|
|
|
dbPath := base.Cfg.MustValue("database", "PATH", "data/gogs.db") |
|
|
|
sslMode := base.Cfg.MustValue("database", "SSL_MODE") |
|
|
|
|
|
|
|
var err error |
|
|
|
switch dbType { |
|
|
|
switch dbCfg.Type { |
|
|
|
case "mysql": |
|
|
|
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8", |
|
|
|
dbUser, dbPwd, dbHost, dbName)) |
|
|
|
dbCfg.User, dbCfg.Pwd, dbCfg.Host, dbCfg.Name)) |
|
|
|
case "postgres": |
|
|
|
orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s", |
|
|
|
dbUser, dbPwd, dbName, sslMode)) |
|
|
|
dbCfg.User, dbCfg.Pwd, dbCfg.Name, dbCfgspan>.SslMode)) |
|
|
|
case "sqlite3": |
|
|
|
os.MkdirAll(path.Dir(dbPath), os.ModePerm) |
|
|
|
orm, err = xorm.NewEngine("sqlite3", dbPath) |
|
|
|
os.MkdirAll(path.Dir(dbCfg.Path), os.ModePerm) |
|
|
|
orm, err = xorm.NewEngine("sqlite3", dbCfg.Path) |
|
|
|
default: |
|
|
|
fmt.Printf("Unknown database type: %s\n", dbType) |
|
|
|
fmt.Printf("Unknown database type: %s\n", dbCfg.Type) |
|
|
|
os.Exit(2) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
@ -61,7 +70,7 @@ func setEngine() { |
|
|
|
orm.ShowSQL = true |
|
|
|
} |
|
|
|
|
|
|
|
func init() { |
|
|
|
func NewEngine() { |
|
|
|
setEngine() |
|
|
|
if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch), |
|
|
|
new(Action), new(Access)); err != nil { |
|
|
|