From f334c93c0364d14a2b55641b155ad58f715a4b39 Mon Sep 17 00:00:00 2001 From: horus_arch Date: Thu, 19 Mar 2015 13:39:37 +0100 Subject: Rewriting from scratch. --- app/controllers/app.go | 63 -------------------- app/controllers/db.go | 138 ------------------------------------------- app/controllers/email.db | Bin 10240 -> 0 bytes app/controllers/init.go | 10 ---- app/controllers/utilities.go | 98 ------------------------------ 5 files changed, 309 deletions(-) delete mode 100644 app/controllers/app.go delete mode 100644 app/controllers/db.go delete mode 100644 app/controllers/email.db delete mode 100644 app/controllers/init.go delete mode 100644 app/controllers/utilities.go (limited to 'app/controllers') diff --git a/app/controllers/app.go b/app/controllers/app.go deleted file mode 100644 index 2b8945c..0000000 --- a/app/controllers/app.go +++ /dev/null @@ -1,63 +0,0 @@ -package controllers - -import ( - _ "github.com/jinzhu/gorm" - _ "github.com/mattn/go-sqlite3" - "github.com/revel/revel" -) - -type App struct { - //*revel.Controller - GormController -} - -func (c App) Index() revel.Result { - return c.Render() -} - -func (c App) DoRegister(name, email, confirmEmail, password, confirmPassword string) revel.Result { - c.Validation.Required(name).Message("Please provide a user name.") - c.Validation.Required(email).Message("Please provide a mail adress.") - c.Validation.Required(email == confirmEmail).Message("Mail adresses do not match.") - c.Validation.Required(password).Message("Please provide a password.") - c.Validation.Required(password == confirmPassword).Message("Passwords do not match.") - - if c.Validation.HasErrors() { - c.Validation.Keep() - c.FlashParams() - return c.Redirect(App.Register) - } - - hash := HashPassword(password) - revel.INFO.Printf("%s \n", hash) - user := User{Name: name, Email: email, Password: hash} - - //c.Validation.Required(c.Db.NewRecord(user)).Message("User name already used.") - /* - if !c.Db.NewRecord(user) { - revel.ERROR.Println("User name already in use.") - } - */ - - revel.INFO.Printf("%s", c.Gdb) - c.Gdb.Debug().NewRecord(user) - c.Gdb.Debug().Save(&user) - //c.Db.Debug().NewRecord(user) - //c.Db.Debug().Save(&user) - //c.Db.Create(&user) - - /* - if c.Validation.HasErrors() { - c.Validation.Keep() - c.FlashParams() - return c.Redirect(App.Register) - } - */ - - //return c.Render() - return c.Redirect(App.Register) -} - -func (c App) Register() revel.Result { - return c.Render() -} diff --git a/app/controllers/db.go b/app/controllers/db.go deleted file mode 100644 index 5b7dd97..0000000 --- a/app/controllers/db.go +++ /dev/null @@ -1,138 +0,0 @@ -package controllers - -import ( - "github.com/jinzhu/gorm" - // _ "github.com/mattn/go-sqlite3" - "database/sql" - "github.com/revel/revel" - "time" -) - -// Website user -type User struct { - Id int64 - Name string - Email string - Password string - // Domains []Domain - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time -} - -/* -type Domain struct { - Id int64 - UserId int64 - Domain string - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time -} -*/ - -// Domains for which we do E-Mail hosting -type VirtualDomain struct { - Id int64 - User User - UserId int64 - Name string -} - -// User for postfix -type VirtualUser struct { - Id int64 - UserId int64 - VirtualDomain VirtualDomain - VirtualDomainId int64 - Password string - Email string -} - -// E-Mail aliase for postfix -type VirtualAliase struct { - Id int64 - VirtualDomain VirtualDomain - VirtualDomainId int64 - Source string - Destination string -} - -type GormController struct { - *revel.Controller - Gdb *gorm.DB -} - -var Gdb gorm.DB - -func InitDB() { - // db.Init() - Gdb, err := gorm.Open(revel.Config.StringDefault("db.driver", "sqlite3"), revel.Config.StringDefault("db.spec", "email.db")) - if err != nil { - revel.ERROR.Printf("%s", err) - } - Gdb.LogMode(true) - Gdb.SetLogger(gorm.Logger{revel.TRACE}) - - user := User{} - Gdb.AutoMigrate(&user) - Gdb.Model(&user).AddUniqueIndex("idx_user_name", "name") - Gdb.Model(&user).AddUniqueIndex("idx_user_email", "email") - - /* - domain := Domain{} - Gdb.AutoMigrate(&domain) - Gdb.Model(&domain).AddUniqueIndex("idx_domain_name", "domain") - */ - - vu := VirtualUser{} - Gdb.AutoMigrate(&vu) - Gdb.Model(&vu).AddUniqueIndex("idx_virtual_user_email", "email") - Gdb.Debug().Model(&vu).Related(&user) - Gdb.Debug().Model(&user).Related(&vu) - - vd := VirtualDomain{} - Gdb.AutoMigrate(&vd) - - va := VirtualAliase{} - Gdb.AutoMigrate(&va) -} - -// transactions - -// This method fills the c.Gdb before each transaction -func (c *GormController) Begin() revel.Result { - txn := Gdb.Begin() - if txn.Error != nil { - //panic(txn.Error) - revel.ERROR.Printf("%s \n", txn.Error) - } - c.Gdb = txn - return nil -} - -// This method clears the c.Gdb after each transaction -func (c *GormController) Commit() revel.Result { - if c.Gdb == nil { - return nil - } - c.Gdb.Commit() - if err := c.Gdb.Error; err != nil && err != sql.ErrTxDone { - panic(err) - } - c.Gdb = nil - return nil -} - -// This method clears the c.Gdb after each transaction, too -func (c *GormController) Rollback() revel.Result { - if c.Gdb == nil { - return nil - } - c.Gdb.Rollback() - if err := c.Gdb.Error; err != nil && err != sql.ErrTxDone { - panic(err) - } - c.Gdb = nil - return nil -} diff --git a/app/controllers/email.db b/app/controllers/email.db deleted file mode 100644 index 4a4cefc..0000000 Binary files a/app/controllers/email.db and /dev/null differ diff --git a/app/controllers/init.go b/app/controllers/init.go deleted file mode 100644 index a5c9096..0000000 --- a/app/controllers/init.go +++ /dev/null @@ -1,10 +0,0 @@ -package controllers - -import "github.com/revel/revel" - -func init() { - revel.OnAppStart(InitDB) // invoke InitDB function before - revel.InterceptMethod((*GormController).Begin, revel.BEFORE) - revel.InterceptMethod((*GormController).Commit, revel.AFTER) - revel.InterceptMethod((*GormController).Rollback, revel.FINALLY) -} diff --git a/app/controllers/utilities.go b/app/controllers/utilities.go deleted file mode 100644 index 693a459..0000000 --- a/app/controllers/utilities.go +++ /dev/null @@ -1,98 +0,0 @@ -package controllers - -import ( - "crypto/md5" - "fmt" - // "github.com/garyburd/redigo/redis" - "github.com/revel/revel" - "golang.org/x/crypto/bcrypt" - "io" - "io/ioutil" - "math/rand" - "net/http" - // "time" -) - -// Returns the content of a webpage as string -func HttpGet(url string) (http.Header, string, error) { - response, err := http.Get(url) - if err != nil { - return nil, "Get request failed.", err - } - - defer response.Body.Close() - contents, err := ioutil.ReadAll(response.Body) - if err != nil { - return nil, "Reading body failed.", err - } - - return response.Header, string(contents), nil -} - -// Hashs and returns a string (md5) -func Hash(content string) string { - h := md5.New() - io.WriteString(h, content) - hash := fmt.Sprintf("%x", h.Sum(nil)) - - return hash -} - -// Creates a random string -func RandomKey() string { - letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - key := make([]rune, 40) - for i := range key { - key[i] = letters[rand.Intn(len(letters))] - } - - return string(key) -} - -//var pool = newPool() - -/* -// Creates a pool with connections to Redis -func newPool() *redis.Pool { - return &redis.Pool{ - MaxIdle: 3, - IdleTimeout: 240 * time.Second, - Dial: func() (redis.Conn, error) { - //c, err := redis.Dial("tcp", ":6379") - c, err := redis.Dial("tcp", revel.Config.StringDefault("redis.server", "127.0.0.1")+":"+revel.Config.StringDefault("redis.port", "6379")) - if err != nil { - return nil, err - } - return c, err - }, - TestOnBorrow: func(c redis.Conn, t time.Time) error { - _, err := c.Do("PING") - return err - }, - } -} -*/ - -// Hashs password with bcrypt and returns the string -func HashPassword(password string) string { - if password == "" { - return "" - } - p := []byte(password) - hash, err := bcrypt.GenerateFromPassword(p, 10) - if err != nil { - revel.ERROR.Printf("%s \n", err) - return "" - } - return string(hash) -} - -// Verify password and hash -func VerifyPassword(password, hash string) bool { - err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) - if err != nil { - revel.ERROR.Printf("%s \n", err) - return false - } - return true -} -- cgit v1.2.3