Commit fb9fcb94 authored by zauberstuhl's avatar zauberstuhl

Move controllers from deprecated log interface to AppLog

parent 84f1ae1b
......@@ -41,7 +41,7 @@ func (name DisplayImageByName) Apply(req *revel.Request, resp *revel.Response) {
err := captcha.WriteImage(&content, id, 250, 100)
if err != nil {
revel.ERROR.Println(err)
revel.AppLog.Error("Cannot write captcha image", "error", err)
status = http.StatusInternalServerError
}
resp.WriteHeader(status, "image/png")
......
......@@ -18,47 +18,35 @@ package controllers
//
import (
"net/http"
"github.com/revel/revel"
"gopkg.in/ganggo/ganggo.v0/app/models"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Hcard struct {
*revel.Controller
}
func (c Hcard) User() revel.Result {
var (
guid string
person models.Person
)
c.Params.Bind(&guid, "guid")
func (c Hcard) User(guid string) revel.Result {
var person models.Person
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return c.Render()
c.Log.Error("Cannot open database", "error", err)
return c.RenderError(err)
}
defer db.Close()
err = db.Where("guid = ?", guid).First(&person).Error
if err != nil {
c.Response.Status = http.StatusNotFound
revel.WARN.Println(err)
return c.Render()
c.Log.Error("Person not found", "error", err)
return c.NotFound(err.Error())
}
var profile models.Profile
err = db.Where("person_id = ?", person.ID).First(&profile).Error
if err != nil {
c.Response.Status = http.StatusNotFound
revel.WARN.Println(err)
return c.Render()
c.Log.Error("Profile not found", "error", err)
return c.NotFound(err.Error())
}
revel.Config.SetSection("ganggo")
......
......@@ -52,8 +52,8 @@ func requiresLogin(c *revel.Controller) revel.Result {
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return c.Render()
c.Log.Error("Cannot open database", "error", err)
return c.RenderError(err)
}
defer db.Close()
......
......@@ -103,12 +103,12 @@ func generateSchema(version string) SchemaJson {
revel.Config.SetSection("DEFAULT")
appName, found := revel.Config.String("app.name")
if !found {
revel.ERROR.Println("app.name configuration value not found!")
revel.AppLog.Error("app.name configuration value not found!")
return SchemaJson{}
}
appVersion, found := revel.Config.String("app.version")
if !found {
revel.ERROR.Println("app.version configuration value not found!")
revel.AppLog.Error("app.version configuration value not found!")
return SchemaJson{}
}
......
......@@ -39,14 +39,14 @@ func (p Profile) IndexPagination(guid string, page int) revel.Result {
err := person.FindByGuid(guid)
if err != nil {
revel.ERROR.Println(err)
return p.Redirect(Stream.Index)
p.Log.Error("Cannot find person", "error", err)
return p.RenderError(err)
}
err = posts.FindAllByPersonID(person.ID, offset)
if err != nil {
revel.ERROR.Println(err)
return p.Redirect(Stream.Index)
p.Log.Error("Cannot find posts", "error", err)
return p.RenderError(err)
}
p.ViewArgs["posts"] = posts
......
......@@ -21,11 +21,6 @@ import (
"net/http"
"github.com/revel/revel"
federation "gopkg.in/ganggo/federation.v0"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"gopkg.in/ganggo/ganggo.v0/app/models"
"gopkg.in/ganggo/ganggo.v0/app/jobs"
"io/ioutil"
......@@ -36,10 +31,10 @@ type Receiver struct {
}
func (r Receiver) Public() revel.Result {
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return r.Render()
r.Log.Error("Cannot open database", "error", err)
return r.RenderError(err)
}
defer db.Close()
......@@ -50,14 +45,14 @@ func (r Receiver) Public() revel.Result {
return r.Render()
}
revel.TRACE.Println("public", string(content))
r.Log.Debug("received publicly", "message", string(content))
// in case it succeeds reply with status 202
r.Response.Status = http.StatusAccepted
message, err := federation.ParseDecryptedRequest(content)
if err != nil {
revel.ERROR.Println(err)
r.Log.Error("Cannot parse decrypted request", "error", err)
// NOTE Send accept code even tho the entity is not
// known otherwise the sender pod will throw an error
//r.Response.Status = http.StatusNotAcceptable
......@@ -80,10 +75,10 @@ func (r Receiver) Private() revel.Result {
user models.User
)
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return r.Render()
r.Log.Error("Cannot open database", "error", err)
return r.RenderError(err)
}
defer db.Close()
......@@ -91,25 +86,25 @@ func (r Receiver) Private() revel.Result {
r.Params.Bind(&guid, "guid")
r.Response.Status = http.StatusAccepted
revel.TRACE.Println("aes request", wrapper)
r.Log.Debug("AES request", "message", wrapper)
err = db.Where("guid like ?", guid).First(&person).Error
if err != nil {
revel.ERROR.Println(err)
r.Log.Error("Cannot find person", "error", err)
// diaspora will not process on StatusNotAcceptable
return r.Render()
}
err = db.First(&user, person.UserID).Error
if err != nil {
revel.ERROR.Println(err)
r.Log.Error("Cannot find user", "error", err)
r.Response.Status = http.StatusNotAcceptable
return r.Render()
}
message, err := federation.ParseEncryptedRequest(wrapper, []byte(user.SerializedPrivateKey))
if err != nil {
revel.ERROR.Println(err)
r.Log.Error("Cannot parse encrypted request", "error", err)
// NOTE Send accept code even tho the entity is not
// known otherwise the sender pod will throw an error
//r.Response.Status = http.StatusNotAcceptable
......
......@@ -30,14 +30,14 @@ type Search struct {
func (s Search) Create(text string) revel.Result {
_, _, err := helpers.ParseAuthor(text)
if err != nil {
return s.Redirect(Stream.Index)
return s.NotFound(err.Error())
}
fetchAuthor := jobs.FetchAuthor{Author: text}
fetchAuthor.Run()
if fetchAuthor.Err != nil {
revel.WARN.Println(fetchAuthor.Err)
return s.Redirect(Stream.Index)
s.Log.Error("Cannot fetch author", "error", fetchAuthor.Err)
return s.RenderError(fetchAuthor.Err)
}
guid := fetchAuthor.Person.Guid
......
......@@ -18,7 +18,6 @@ package controllers
//
import (
"net/http"
"github.com/revel/revel"
"gopkg.in/ganggo/ganggo.v0/app/models"
)
......@@ -37,14 +36,14 @@ func (s Stream) IndexPagination(page int) revel.Result {
user, err := models.GetCurrentUser(s.Session["TOKEN"])
if err != nil {
revel.ERROR.Println(err)
s.Log.Error("Cannot fetch current user", "error", err)
return s.RenderError(err)
}
err = posts.FindAll(user.ID, offset)
if err != nil {
s.Response.Status = http.StatusInternalServerError
revel.WARN.Println(err)
s.Log.Error("Cannot find posts", "error", err)
return s.RenderError(err)
}
s.ViewArgs["currentUser"] = user
......
......@@ -18,7 +18,7 @@ package controllers
//
import (
"net/http"
"errors"
"golang.org/x/crypto/bcrypt"
"github.com/revel/revel"
"github.com/dchest/captcha"
......@@ -30,11 +30,6 @@ import (
"gopkg.in/ganggo/ganggo.v0/app/models"
"gopkg.in/ganggo/ganggo.v0/app/helpers"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type User struct {
......@@ -50,10 +45,11 @@ func (u User) Create() revel.Result {
var username, password, verify string
var captchaID, captchaValue string
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return u.Render()
u.Log.Error("Cannot open database", "error", err)
return u.RenderError(err)
}
defer db.Close()
......@@ -65,31 +61,42 @@ func (u User) Create() revel.Result {
u.Params.Bind(&captchaValue, "captchaValue")
if !captcha.VerifyString(captchaID, captchaValue) {
u.Flash.Error("Captcha was not correct!")
u.Flash.Error(revel.MessageFunc(
u.Request.Locale,
"flash.errors.captcha",
))
return u.Redirect(User.Index)
}
if !db.Where("username = ?", username).First(&user).RecordNotFound() {
u.Flash.Error("Username already exists!")
u.Flash.Error(revel.MessageFunc(
u.Request.Locale,
"flash.errors.username",
))
return u.Redirect(User.Index)
}
if password == "" || password != verify {
u.Flash.Error("Password was empty or didn't match!")
u.Flash.Error(revel.MessageFunc(
u.Request.Locale,
"flash.errors.password_empty",
))
return u.Redirect(User.Index)
}
if len(password) < 4 {
u.Flash.Error("Password length should be greater then four!")
u.Flash.Error(revel.MessageFunc(
u.Request.Locale,
"flash.errors.password_length",
))
return u.Redirect(User.Index)
}
// generate priv,pub key
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
revel.ERROR.Println(err)
u.Response.Status = http.StatusInternalServerError
return u.Redirect(User.Index)
u.Log.Error("Cannot generate RSA key", "error", err)
return u.RenderError(err)
}
// private key
key := x509.MarshalPKCS1PrivateKey(privKey)
......@@ -101,9 +108,8 @@ func (u User) Create() revel.Result {
// public key
pub, err := x509.MarshalPKIXPublicKey(&privKey.PublicKey)
if err != nil {
revel.ERROR.Println(err)
u.Response.Status = http.StatusInternalServerError
return u.Redirect(User.Index)
u.Log.Error("Cannot extract public key", "error", err)
return u.RenderError(err)
}
pubBlock := pem.Block{
Type: "PUBLIC KEY",
......@@ -113,24 +119,22 @@ func (u User) Create() revel.Result {
guid, err := helpers.Uuid()
if err != nil {
revel.ERROR.Println(err)
u.Response.Status = http.StatusInternalServerError
return u.Redirect(User.Index)
u.Log.Error("Cannot generate UUID", "error", err)
return u.RenderError(err)
}
passwordEncoded, err := bcrypt.GenerateFromPassword([]byte(password), -1)
if err != nil {
revel.ERROR.Println(err)
u.Response.Status = http.StatusInternalServerError
return u.Redirect(User.Index)
u.Log.Error("Cannot encode password", "error", err)
return u.RenderError(err)
}
revel.Config.SetSection("ganggo")
host, found := revel.Config.String("address")
if !found {
revel.ERROR.Println("No server address configured")
u.Response.Status = http.StatusInternalServerError
return u.Redirect(User.Index)
err = errors.New("No server address configured")
u.Log.Error("", "error", err)
return u.RenderError(err)
}
// build user struct
......@@ -151,30 +155,31 @@ func (u User) Create() revel.Result {
}
err = db.Create(&user).Error
if err != nil {
u.Flash.Error("Something went wrong :(")
revel.ERROR.Println(err)
return u.Redirect(User.Index)
u.Log.Error("Cannot create user", "error", err)
return u.RenderError(err)
} else {
u.Flash.Success("The user was successfully created! Please login")
u.Flash.Success(revel.MessageFunc(
u.Request.Locale,
"flash.success.registration",
))
}
return u.Redirect(App.Index)
return u.Redirect(User.Login)
}
func (u User) Logout() revel.Result {
var session models.Session
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return u.Render()
u.Log.Error("Cannot open database", "error", err)
return u.RenderError(err)
}
defer db.Close()
err = db.Where("token = ?", u.Session["TOKEN"]).First(&session).Error
if err != nil {
u.Response.Status = http.StatusInternalServerError
revel.ERROR.Println(err)
return u.Render()
u.Log.Error("Cannot find session", "error", err)
return u.RenderError(err)
}
db.Delete(&session)
delete(u.Session, "TOKEN")
......@@ -197,44 +202,50 @@ func (u User) Login() revel.Result {
u.Params.Bind(&username, "username")
u.Params.Bind(&password, "password")
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return u.Render()
u.Log.Error("Cannot open database", "error", err)
return u.RenderError(err)
}
defer db.Close()
err = db.Where("username = ?", username).First(&user).Error
if err != nil {
// TODO flash message not found
revel.TRACE.Println(err)
return u.Redirect(App.Index)
u.Flash.Error(revel.MessageFunc(
u.Request.Locale,
"flash.errors.username_not_found",
username,
))
return u.Redirect(User.Login)
}
if !checkHash(password, user.EncryptedPassword) {
revel.TRACE.Println("Login failed for user " + username)
return u.Redirect(App.Index)
u.Flash.Error(revel.MessageFunc(
u.Request.Locale,
"flash.errors.login_failed",
))
return u.Redirect(User.Login)
}
revel.TRACE.Println("Login successful for user " + username)
token, err := helpers.Uuid()
if err != nil {
u.Response.Status = http.StatusInternalServerError
revel.TRACE.Println(err)
return u.Redirect(App.Index)
u.Log.Error("Cannot generate UUID", "error", err)
return u.RenderError(err)
}
session.UserID = user.ID
session.Token = token
u.Session["TOKEN"] = session.Token
err = db.Create(&session).Error
if err != nil {
u.Response.Status = http.StatusInternalServerError
revel.TRACE.Println(err)
delete(u.Session, "TOKEN")
return u.Redirect(App.Index)
u.Log.Error("Cannot create session", "error", err)
return u.RenderError(err)
}
// TODO flash message
u.Session["TOKEN"] = session.Token
u.Flash.Success(revel.MessageFunc(
u.Request.Locale,
"flash.success.login",
))
return u.Redirect(Stream.Index)
}
......
......@@ -22,11 +22,6 @@ import (
"github.com/revel/revel"
"gopkg.in/ganggo/ganggo.v0/app/models"
federation "gopkg.in/ganggo/federation.v0"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Webfinger struct {
......@@ -45,27 +40,27 @@ func (c Webfinger) Webfinger() revel.Result {
proto, ok := revel.Config.String("proto")
if !ok {
c.Response.Status = http.StatusNotFound
revel.TRACE.Println("no proto config found")
c.Log.Error("No proto config found")
return c.RenderJSON(json)
}
address, ok := revel.Config.String("address")
if !ok {
c.Response.Status = http.StatusNotFound
revel.TRACE.Println("no address config found")
c.Log.Error("No address config found")
return c.RenderJSON(json)
}
username, err := federation.ParseWebfingerHandle(resource)
if err != nil {
c.Response.Status = http.StatusNotFound
revel.TRACE.Println(err)
c.Log.Error("Cannot parse webfinger handle", "error", err)
return c.RenderJSON(json)
}
db, err := gorm.Open(models.DB.Driver, models.DB.Url)
db, err := models.OpenDatabase()
if err != nil {
revel.WARN.Println(err)
return c.RenderJSON(json)
c.Log.Error("Cannot open database", "error", err)
return c.RenderError(err)
}
defer db.Close()
......@@ -73,7 +68,7 @@ func (c Webfinger) Webfinger() revel.Result {
err = db.Where("author = ?", username + "@" + address).First(&person).Error
if err != nil {
c.Response.Status = http.StatusNotFound
revel.TRACE.Println(err)
c.Log.Error("Cannot find person", "error", err)
return c.RenderJSON(json)
}
......@@ -125,13 +120,13 @@ func (c Webfinger) HostMeta() revel.Result {
proto, ok := revel.Config.String("proto")
if !ok {
c.Response.Status = http.StatusNotFound
revel.TRACE.Println("no proto config found")
c.Log.Error("No proto config found")
return c.RenderXML(m)
}
address, ok := revel.Config.String("address")
if !ok {
c.Response.Status = http.StatusNotFound
revel.TRACE.Println("no address config found")
c.Log.Error("No address config found")
return c.RenderXML(m)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment