forked from jimafisk/plentiform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.go
68 lines (55 loc) · 1.67 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
package main
import (
"database/sql"
"net/http"
"os"
"log"
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
"github.com/haisum/recaptcha"
repo "github.com/plentiform/plentiform/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, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
if err := db.Ping(); err != nil {
log.Fatal(err)
}
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["recaptcha_site_key"] = os.Getenv("RECAPTCHA_SITE_KEY")
session.Save(r, w)
return t.ExecuteWriter(data, w)
}
func (app *Application) GetSession(r *http.Request) (*sessions.Session, error) {
return app.sessions.Get(r, "plentiform")
}