diff --git a/conf/app.ini b/conf/app.ini index 834923d1..fe279cb5 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -2,4 +2,11 @@ APP_NAME = Gogs - Go Git Service [server] HTTP_ADDR = -HTTP_PORT = 3000 \ No newline at end of file +HTTP_PORT = 3000 + +[database] +DB_TYPE = mysql +HOST = +NAME = gogs +USER = root +PASSWD = root \ No newline at end of file diff --git a/models/models.go b/models/models.go index 0d49162d..b4e69d94 100644 --- a/models/models.go +++ b/models/models.go @@ -4,7 +4,16 @@ package models -import "github.com/lunny/xorm" +import ( + "fmt" + "os" + + _ "github.com/go-sql-driver/mysql" + "github.com/lunny/xorm" + + "github.com/gogits/gogs/utils" + "github.com/gogits/gogs/utils/log" +) var ( orm *xorm.Engine @@ -30,3 +39,37 @@ type PullRequest struct { type Comment struct { Id int64 } + +func setEngine() { + dbType := utils.Cfg.MustValue("database", "DB_TYPE") + dbHost := utils.Cfg.MustValue("database", "HOST") + dbName := utils.Cfg.MustValue("database", "NAME") + dbUser := utils.Cfg.MustValue("database", "USER") + dbPwd := utils.Cfg.MustValue("database", "PASSWD") + + var err error + switch dbType { + case "mysql": + orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@%v/%v?charset=utf8", + dbUser, dbPwd, dbHost, dbName)) + default: + log.Critical("Unknown database type: %s", dbType) + os.Exit(2) + } + + if err != nil { + log.Critical("models.init -> Conntect database: %s", dbType) + os.Exit(2) + } + + //x.ShowDebug = true + orm.ShowErr = true + //x.ShowSQL = true + + log.Trace("Initialized database -> %s", dbName) +} + +func init() { + setEngine() + orm.Sync(new(User)) +} diff --git a/models/user.go b/models/user.go index ec88edde..bc9b8fc7 100644 --- a/models/user.go +++ b/models/user.go @@ -97,6 +97,7 @@ func RegisterUser(user *User) (err error) { user.Avatar = utils.EncodeMd5(user.Email) user.Created = time.Now() user.Updated = time.Now() + user.EncodePasswd() _, err = orm.Insert(user) return err } @@ -116,7 +117,7 @@ func DeleteUser(user *User) error { } // EncodePasswd encodes password to safe format. -func (user *User) EncodePasswd(pass string) error { +func (user *User) EncodePasswd() error { newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64) user.Passwd = fmt.Sprintf("%x", newPasswd) return err @@ -124,8 +125,8 @@ func (user *User) EncodePasswd(pass string) error { // LoginUserPlain validates user by raw user name and password. func LoginUserPlain(name, passwd string) (*User, error) { - user := User{Name: name} - if err := user.EncodePasswd(passwd); err != nil { + user := User{Name: name, Passwd: passwd} + if err := user.EncodePasswd(); err != nil { return nil, err } diff --git a/utils/log/log.go b/utils/log/log.go index e5c0b21c..0634bde6 100644 --- a/utils/log/log.go +++ b/utils/log/log.go @@ -16,6 +16,10 @@ func init() { logger.SetLogger("console", "") } +func Trace(format string, v ...interface{}) { + logger.Trace(format, v...) +} + func Info(format string, v ...interface{}) { logger.Info(format, v...) } @@ -27,3 +31,7 @@ func Error(format string, v ...interface{}) { func Warn(format string, v ...interface{}) { logger.Warn(format, v...) } + +func Critical(format string, v ...interface{}) { + logger.Critical(format, v...) +}