diff options
Diffstat (limited to 'app/controllers')
| -rw-r--r-- | app/controllers/app.go | 78 | ||||
| -rw-r--r-- | app/controllers/db.go | 38 | ||||
| -rw-r--r-- | app/controllers/utilities.go | 97 |
3 files changed, 0 insertions, 213 deletions
diff --git a/app/controllers/app.go b/app/controllers/app.go deleted file mode 100644 index 87c8bd1..0000000 --- a/app/controllers/app.go +++ /dev/null @@ -1,78 +0,0 @@ -package controllers - -import "github.com/revel/revel" - -type App struct { - *revel.Controller -} - -func (c App) Index() revel.Result { - return c.Render() -} - -func (c App) Account() revel.Result { - return c.Render() -} - -func (c App) PrintLogin() revel.Result { - return c.Render() -} - -func (c App) Login(login, password string) revel.Result { - - c.Validation.Required(login).Message("Please provide a user name or email.") - c.Validation.Required(password).Message("Please enter a password.") - - if c.Validation.HasErrors() { - c.Validation.Keep() - c.FlashParams() - return c.Redirect(App.PrintLogin) - } - - u := User{} - db.Where("name = ?", login).Or("email = ?", login).Find(&u) - - c.Validation.Required(VerifyPassword(u.Password, password)).Message("User / Password combination did not matched.") - if c.Validation.HasErrors() { - c.Validation.Keep() - c.FlashParams() - return c.Redirect(App.PrintLogin) - } - - c.Session["login"] = "true" - c.Session["uid"] = string(u.Id) - - return c.Redirect(App.Account) -} - -func (c App) Register(email, confirmEmail, user, password, confirmPassword string) revel.Result { - c.Validation.Required(email).Message("Please provide a mail adress.") - c.Validation.Required(email == confirmEmail).Message("The mail adresses do not match.") - c.Validation.Required(user).Message("Please provide a user name.") - - if password != "" { - c.Validation.Required(password == confirmPassword).Message("Passwords do not match.") - } - - if c.Validation.HasErrors() { - c.Validation.Keep() - c.FlashParams() - return c.Redirect(App.PrintLogin) - } - - p, _ := HashPassword(password) - - user := User{ - Name: user, - Email: email, - Password: p, - } - - db.NewRecord(u) - db.Create(&u) - db.Save(&u) - - c.Flash.Success("Registration completed.") - - return c.Redirect(App.Account) -} diff --git a/app/controllers/db.go b/app/controllers/db.go deleted file mode 100644 index e96cb63..0000000 --- a/app/controllers/db.go +++ /dev/null @@ -1,38 +0,0 @@ -package controllers - -import ( - "github.com/jinzhu/gorm" - _ "github.com/mattn/go-sqlite3" - "github.com/revel/revel" - "time" -) - -/* Maybe worth saving uptime history? */ - -type Hosts struct { - Id int64 - UserId int64 - Url string - Protocoll string // e.g. http - Private bool - Response int64 - Date time.Time - Success bool - Include string // Website must include this string - Except string // Website must not include this string - Reason string // Include, Exclude, Connection failure - Alert bool // True to send alert on failure - CreatedAt time.Time - DeletedAt time.Time - UpdatedAt time.Time -} - -type User struct { - Id int64 - Name string - Email string - Password string - CreatedAt time.Time - DeletedAt time.Time - UpdatedAt time.Time -} diff --git a/app/controllers/utilities.go b/app/controllers/utilities.go deleted file mode 100644 index 2d07bdf..0000000 --- a/app/controllers/utilities.go +++ /dev/null @@ -1,97 +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, error) { - if password == "" { - return "", nil - } - p := []byte(password) - hash, err := bcrypt.GenerateFromPassword(p, 10) - if err != nil { - return "", err - } - return string(hash), nil -} - -// 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 -} |
