-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
163 lines (146 loc) · 3.88 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/gorilla/csrf"
"github.com/joho/godotenv"
"github.com/zero-one-group/fullstack-go/controllers"
"github.com/zero-one-group/fullstack-go/migrations"
"github.com/zero-one-group/fullstack-go/models"
"github.com/zero-one-group/fullstack-go/templates"
"github.com/zero-one-group/fullstack-go/views"
)
type config struct {
PSQL models.PostgresConfig
SMTP models.SMTPConfig
CSRF struct {
Key string
Secure bool
}
Server struct {
Address string
}
}
func loadEnvConfig() (config, error) {
var cfg config
err := godotenv.Load()
if err != nil {
return cfg, err
}
// TODO: Read the PSQL values from an ENV variable
cfg.PSQL = models.DefaultPostgresConfig()
// TODO: SMTP
cfg.SMTP.Host = os.Getenv("SMTP_HOST")
portStr := os.Getenv("SMTP_PORT")
cfg.SMTP.Port, err = strconv.Atoi(portStr)
if err != nil {
return cfg, err
}
cfg.SMTP.Username = os.Getenv("SMTP_USERNAME")
cfg.SMTP.Password = os.Getenv("SMTP_PASSWORD")
// TODO: Read the CSRF values from an ENV variable
cfg.CSRF.Key = "gFvi45R4fy5xNBlnEeZtQbfAVCYEIAUX"
cfg.CSRF.Secure = false
// TODO: Read the server values from an ENV variable
cfg.Server.Address = ":3000"
return cfg, nil
}
func main() {
cfg, err := loadEnvConfig()
if err != nil {
panic(err)
}
// Setup the database
db, err := models.Open(cfg.PSQL)
if err != nil {
panic(err)
}
defer db.Close()
err = models.MigrateFS(db, migrations.FS, ".")
if err != nil {
panic(err)
}
// Set up services
userService := &models.UserService{
DB: db,
}
sessionService := &models.SessionService{
DB: db,
}
pwResetService := &models.PasswordResetService{
DB: db,
}
emailService := models.NewEmailService(cfg.SMTP)
// Set up middleware
umw := controllers.UserMiddleware{
SessionService: sessionService,
}
csrfMw := csrf.Protect(
[]byte(cfg.CSRF.Key),
csrf.Secure(cfg.CSRF.Secure),
)
// Set up controllers
usersC := controllers.Users{
UserService: userService,
SessionService: sessionService,
PasswordResetService: pwResetService,
EmailService: emailService,
}
usersC.Templates.New = views.Must(views.ParseFS(
templates.FS, "signup.html", "tailwind.html"))
usersC.Templates.SignIn = views.Must(views.ParseFS(
templates.FS, "signin.html", "tailwind.html"))
usersC.Templates.ForgotPassword = views.Must(views.ParseFS(
templates.FS,
"forgot-pw.html", "tailwind.html",
))
usersC.Templates.CheckYourEmail = views.Must(views.ParseFS(
templates.FS,
"check-your-email.html", "tailwind.html",
))
usersC.Templates.ResetPassword = views.Must(views.ParseFS(
templates.FS,
"reset-pw.html", "tailwind.html",
))
// Set up router and routes
r := chi.NewRouter()
r.Use(csrfMw)
r.Use(umw.SetUser)
r.Get("/", controllers.StaticHandler(views.Must(views.ParseFS(
templates.FS,
"home.html", "tailwind.html",
))))
r.Get("/contact", controllers.StaticHandler(views.Must(views.ParseFS(
templates.FS,
"contact.html", "tailwind.html",
))))
r.Get("/faq", controllers.FAQ(views.Must(views.ParseFS(
templates.FS,
"faq.html", "tailwind.html",
))))
r.Get("/signup", usersC.New)
r.Post("/signup", usersC.Create)
r.Get("/signin", usersC.SignIn)
r.Post("/signin", usersC.ProcessSignIn)
r.With(umw.RequireUser).Post("/signout", usersC.ProcessSignOut)
r.Get("/forgot-pw", usersC.ForgotPassword)
r.Post("/forgot-pw", usersC.ProcessForgotPassword)
r.Get("/reset-pw", usersC.ResetPassword)
r.Post("/reset-pw", usersC.ProcessResetPassword)
r.Route("/users/me", func(r chi.Router) {
r.Use(umw.RequireUser)
r.Get("/", usersC.CurrentUser)
})
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Page not found", http.StatusNotFound)
})
// Start the server
fmt.Printf("Starting the server on %s...\n", cfg.Server.Address)
err = http.ListenAndServe(cfg.Server.Address, r)
if err != nil {
panic(err)
}
}