-
Notifications
You must be signed in to change notification settings - Fork 2
/
plentiform.go
83 lines (69 loc) · 3.39 KB
/
plentiform.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
package main
import (
"log"
"net/http"
"os"
_ "github.com/joho/godotenv/autoload"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/plentiform/plentiform/models"
repo "github.com/plentiform/plentiform/repositories"
)
func main() {
app := NewApplication()
r := mux.NewRouter()
r.HandleFunc("/", app.IndexHandler).Methods("GET")
r.HandleFunc("/login", app.SessionsNewHandler).Methods("GET")
r.HandleFunc("/login", app.SessionsCreateHandler).Methods("POST")
r.HandleFunc("/logout", app.SessionsDestroyHandler).Methods("GET")
r.HandleFunc("/signup", app.UsersNewHandler).Methods("GET")
r.HandleFunc("/signup", app.UsersCreateHandler).Methods("POST")
r.HandleFunc("/f/{uuid}", app.SubmissionsCreateHandler).Methods("POST")
r.HandleFunc("/email_confirmation/new", app.EmailConfirmationsNewHandler).Methods("GET")
r.HandleFunc("/email_confirmation", app.EmailConfirmationsCreateHandler).Methods("POST")
r.HandleFunc("/email_confirmation", app.EmailConfirmationsShowHandler).Methods("GET")
r.HandleFunc("/forms/{formId:[0-9]+}/submissions/{submissionId:[0-9]+}", app.RequireAuthentication(app.RequireEmailConfirmation(app.SubmissionsDestroyHandler))).Methods("DELETE")
r.HandleFunc("/forms/{id:[0-9]+}/edit", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsEditHandler))).Methods("GET")
r.HandleFunc("/forms/{id:[0-9]+}", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsShowHandler))).Methods("GET")
r.HandleFunc("/forms/{id:[0-9]+}", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsDestroyHandler))).Methods("DELETE")
r.HandleFunc("/forms/{id:[0-9]+}", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsUpdateHandler))).Methods("POST")
r.HandleFunc("/forms/new", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsNewHandler))).Methods("GET")
r.HandleFunc("/forms", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsIndexHandler))).Methods("GET")
r.HandleFunc("/forms", app.RequireAuthentication(app.RequireEmailConfirmation(app.FormsCreateHandler))).Methods("POST")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))
port, ok := os.LookupEnv("PORT")
if !ok {
port = "3000"
}
log.Println("Listening on " + port)
log.Fatal(http.ListenAndServe(":"+port,
handlers.CompressHandler(
handlers.HTTPMethodOverrideHandler(
handlers.LoggingHandler(os.Stdout, r)))))
}
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)
user, err := repo.NewUsersRepository(app.db).FindById(session.Values["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)
})
}