/
application.go
103 lines (84 loc) · 2.89 KB
/
application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package routes
import (
"database/sql"
"net/http"
"os"
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
"github.com/haisum/recaptcha"
"github.com/jonahgeorge/weatherglass/models"
repo "github.com/jonahgeorge/weatherglass/repositories"
_ "github.com/lib/pq"
"github.com/sendgrid/sendgrid-go"
)
type Application struct {
db *sql.DB
sessions *sessions.CookieStore
emailClient *sendgrid.Client
recaptchaClient recaptcha.R
hostName string
}
func NewApplication() *Application {
db, _ := sql.Open("postgres", os.Getenv("DATABASE_URL"))
sessions := sessions.NewCookieStore([]byte(os.Getenv("SECRET_TOKEN")))
emailClient := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
recaptchaClient := recaptcha.R{Secret: os.Getenv("RECAPTCHA_SECRET_TOKEN")}
hostName := os.Getenv("HOST")
return &Application{
db: db,
sessions: sessions,
emailClient: emailClient,
recaptchaClient: recaptchaClient,
hostName: hostName,
}
}
func (app *Application) Render(w http.ResponseWriter, r *http.Request, name string, data pongo2.Context) error {
t, _ := pongo2.FromFile("templates/" + name + ".html")
session, _ := app.GetSession(r)
if session.Values["userId"] != nil {
user, _ := repo.NewUsersRepository(app.db).FindById(session.Values["userId"].(int))
data["currentUser"] = user
}
data["flashes"] = session.Flashes()
data["host"] = os.Getenv("HOST")
data["recaptcha_site_key"] = os.Getenv("RECAPTCHA_SITE_KEY")
data["weatherglass_site_id"] = os.Getenv("WEATHERGLASS_SITE_ID")
session.Save(r, w)
return t.ExecuteWriter(data, w)
}
func (app *Application) GetSession(r *http.Request) (*sessions.Session, error) {
return app.sessions.Get(r, "weatherglass")
}
type AuthenticatedHandlerFunc func(http.ResponseWriter, *http.Request, *models.User)
func (app *Application) RequireAuthentication(next AuthenticatedHandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := app.GetSession(r)
userId, ok := session.Values["userId"]
if !ok {
session.AddFlash("You must be logged in!")
session.Save(r, w)
http.Redirect(w, r, "/login", 307)
return
}
user, err := repo.NewUsersRepository(app.db).FindById(userId.(int))
if user == nil || err != nil {
session.AddFlash("You must be logged in!")
session.Save(r, w)
http.Redirect(w, r, "/login", 307)
return
}
next(w, r, user)
})
}
func (app *Application) RequireEmailConfirmation(next AuthenticatedHandlerFunc) AuthenticatedHandlerFunc {
return AuthenticatedHandlerFunc(func(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
session, _ := app.GetSession(r)
if !currentUser.IsEmailConfirmed {
session.AddFlash("You must confirm your email address before continuing")
session.Save(r, w)
http.Redirect(w, r, "/email_confirmation/new", 302)
return
}
next(w, r, currentUser)
})
}