-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (113 loc) · 3.66 KB
/
main.go
File metadata and controls
132 lines (113 loc) · 3.66 KB
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
package main
import (
"log"
"net/http"
"os"
"strconv"
"github.com/ItsJustVaal/WebDevGo/controllers"
"github.com/ItsJustVaal/WebDevGo/migrations"
"github.com/ItsJustVaal/WebDevGo/models"
"github.com/ItsJustVaal/WebDevGo/templates"
"github.com/ItsJustVaal/WebDevGo/views"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/gorilla/csrf"
"github.com/joho/godotenv"
)
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
}
cfg.PSQL = models.DefaultConfig()
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")
cfg.CSRF.Key = "apwfWAAw0f8AWfafwareaweaAfwWAg9W2Lkf"
cfg.CSRF.Secure = false
cfg.Server.Address = ":3000"
return cfg, nil
}
func main() {
// DB init
cfg, err := loadEnvConfig()
if err != nil {
panic(err)
}
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)
}
// Services init
userService := &models.UserService{
DB: db,
}
sessionService := &models.SessionService{
DB: db,
}
passwordResetService := &models.PasswordResetService{
DB: db,
}
emailService := models.NewEmailService(cfg.SMTP)
usersC := controllers.Users{
UserService: userService,
SessionService: sessionService,
PasswordResetService: passwordResetService,
EmailService: emailService,
}
usersC.Templates.New = views.Must(views.ParseFS(templates.FS, "signup.gohtml", "tailwind.gohtml"))
usersC.Templates.SignIn = views.Must(views.ParseFS(templates.FS, "signin.gohtml", "tailwind.gohtml"))
usersC.Templates.ForgotPassword = views.Must(views.ParseFS(templates.FS, "forgot-pw.gohtml", "tailwind.gohtml"))
usersC.Templates.CheckYourEmail = views.Must(views.ParseFS(templates.FS, "check-email.gohtml", "tailwind.gohtml"))
usersC.Templates.ResetPassword = views.Must(views.ParseFS(templates.FS, "reset-pw.gohtml", "tailwind.gohtml"))
// Middlewear
umw := controllers.UserMiddlewear{
SessionService: sessionService,
}
csrfMW := csrf.Protect([]byte(cfg.CSRF.Key), csrf.Secure(cfg.CSRF.Secure))
mainRouter := chi.NewRouter()
mainRouter.Use(csrfMW, umw.SetUser, middleware.Logger)
mainRouter.Get("/", controllers.StaticHandler(views.Must(views.ParseFS(templates.FS, "home.gohtml", "tailwind.gohtml"))))
mainRouter.Get("/contact", controllers.StaticHandler(views.Must(views.ParseFS(templates.FS, "contact.gohtml", "tailwind.gohtml"))))
mainRouter.Get("/faq", controllers.FAQ(views.Must(views.ParseFS(templates.FS, "faq.gohtml", "tailwind.gohtml"))))
// User Routes
mainRouter.Get("/signup", usersC.New)
mainRouter.Get("/signin", usersC.SignIn)
mainRouter.Post("/users", usersC.Create)
mainRouter.Post("/signin", usersC.ProcessSignIn)
mainRouter.Post("/signout", usersC.ProcessSignOut)
mainRouter.Get("/forgot-pw", usersC.ForgotPassword)
mainRouter.Post("/forgot-pw", usersC.ProcessForgotPassword)
mainRouter.Get("/reset-pw", usersC.ResetPassword)
mainRouter.Post("/reset-pw", usersC.ProcessResetPassword)
mainRouter.Route("/users/me", func(r chi.Router) {
r.Use(umw.RequireUser)
r.Get("/", usersC.CurrentUser)
})
mainRouter.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Page not found", http.StatusNotFound)
})
log.Fatal(http.ListenAndServe(cfg.Server.Address, mainRouter))
}