-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Users #5
base: master
Are you sure you want to change the base?
Users #5
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package bigv | ||
|
||
import ( | ||
"errors" | ||
"tange/common" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type UserModel struct { | ||
ID uint `gorm:"primary_key"` | ||
Username string `gorm:"column:username"` | ||
PasswordHash string `gorm:"column:password;not null"` | ||
} | ||
|
||
func AutoMigrate() { | ||
db := common.GetDB() | ||
|
||
db.AutoMigrate(&UserModel{}) | ||
} | ||
|
||
// err := userModel.setPassword("password0") | ||
func (u *UserModel) setPassword(password string) error { | ||
if len(password) == 0 { | ||
return errors.New("password should not be empty!") | ||
} | ||
bytePassword := []byte(password) | ||
// Make sure the second param `bcrypt generator cost` between [4, 32) | ||
passwordHash, _ := bcrypt.GenerateFromPassword(bytePassword, bcrypt.DefaultCost) | ||
u.PasswordHash = string(passwordHash) | ||
return nil | ||
} | ||
|
||
func (u *UserModel) checkPassword(password string) error { | ||
bytePassword := []byte(password) | ||
byteHashedPassword := []byte(u.PasswordHash) | ||
return bcrypt.CompareHashAndPassword(byteHashedPassword, bytePassword) | ||
} | ||
|
||
func FindOneUser(condition interface{}) (UserModel, error) { | ||
db := common.GetDB() | ||
var model UserModel | ||
err := db.Where(condition).First(&model).Error | ||
return model, err | ||
} | ||
|
||
func SaveOne(data interface{}) error { | ||
db := common.GetDB() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle panic |
||
err := db.Save(data).Error | ||
return err | ||
} | ||
|
||
func (model *UserModel) Update(data interface{}) error { | ||
db := common.GetDB() | ||
err := db.Model(model).Update(data).Error | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"tange/bigv" | ||
"tange/common" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
// "github.com/spf13/viper" | ||
) | ||
|
||
//var port string | ||
|
||
// serveCmd represents the serve command | ||
var initdbCmd = &cobra.Command{ | ||
Use: "initdb", | ||
Short: "Initialize database", | ||
Long: `No longer description Needed.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
initdb() | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(initdbCmd) | ||
|
||
} | ||
|
||
func initdb() { | ||
log.Info("Initializing Database") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle panic |
||
db := common.Init() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. measure this event |
||
bigv.AutoMigrate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log this event too, also measure |
||
defer db.Close() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm/dialects/postgres" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Database struct { | ||
*gorm.DB | ||
} | ||
|
||
var DB *gorm.DB | ||
|
||
func Init() *gorm.DB { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle panic |
||
// var dbPort string | ||
// var dbHost string | ||
// var postgresUser string | ||
// var postgresPassword string | ||
dbPort := viper.GetString("db_port") | ||
dbHost := viper.GetString("db_host") | ||
dbName := viper.GetString("db_name") | ||
postgresUser := viper.GetString("postgres_user") | ||
postgresPassword := viper.GetString("postgres_password") | ||
|
||
db, err := gorm.Open("postgres", "host="+dbHost+" port="+dbPort+" user="+postgresUser+" dbname="+dbName+" password="+postgresPassword) | ||
if err != nil { | ||
fmt.Println("db err: ", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log |
||
} | ||
|
||
db.DB().SetMaxIdleConns(10) | ||
//db.LogMode(true) | ||
DB = db | ||
return DB | ||
} | ||
|
||
func GetDB() *gorm.DB { | ||
return DB | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handle panic