From 94311e187f07b4ee2344db03279f39f200b4d39c Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 18 Feb 2014 17:48:02 -0500 Subject: [PATCH] Finish register user --- conf/app.ini | 9 ++++++++- models/models.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- models/user.go | 7 ++++--- utils/log/log.go | 8 ++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/conf/app.ini b/conf/app.ini index 834923d146..fe279cb52b 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 0d49162dc8..b4e69d94f8 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 ec88edde19..bc9b8fc746 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 e5c0b21c76..0634bde655 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...) +}