Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions bigv/models.go
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{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle panic

}

// 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()
Copy link
Owner

Choose a reason for hiding this comment

The 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
}
34 changes: 34 additions & 0 deletions cmd/initdb.go
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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle panic

db := common.Init()
Copy link
Owner

@andjedani andjedani Dec 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

measure this event

bigv.AutoMigrate()
Copy link
Owner

@andjedani andjedani Dec 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log this event too, also measure

defer db.Close()
}
4 changes: 4 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package cmd
import (
"fmt"
"net/http"
"tange/common"

"github.com/jinzhu/gorm"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
// "github.com/spf13/viper"
)

var port string
var db *gorm.DB

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Expand Down Expand Up @@ -42,6 +45,7 @@ func handler(RespW http.ResponseWriter, ReqR *http.Request) {
}

func serve(port string) {
db = common.Init()
log.Info("I'll serve, RelX DUDE")
portString := ":" + port
log.Info("On port " + portString)
Expand Down
42 changes: 42 additions & 0 deletions common/dasabase.go
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 {

Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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
}