-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
85 lines (69 loc) · 2.45 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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/scottmckendry/mnemstart/auth"
"github.com/scottmckendry/mnemstart/config"
"github.com/scottmckendry/mnemstart/data"
"github.com/scottmckendry/mnemstart/handlers"
)
func main() {
db, err := data.NewLibSqlDatabase(config.Envs.DatabaseURL)
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
store := data.NewStore(db)
initStorage(db)
sessionStore, err := auth.NewFileStore(auth.SessionOptions{
StorePath: "./sessions", // Add this line - specify where to store session files
CookiesKey: config.Envs.CookiesAuthSecret,
MaxAge: config.Envs.CookiesAuthAgeInSeconds,
HttpOnly: config.Envs.CookiesAuthIsHttpOnly,
Secure: config.Envs.CookiesAuthIsSecure,
})
if err != nil {
log.Fatalf("Error creating session store: %v", err)
}
authService := auth.NewAuthService(sessionStore)
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
handler := handlers.New(store, authService)
r.Group(func(r chi.Router) {
// Require authentication for all routes in this group
r.Use(auth.RequireAuth(authService))
// app routes
r.Get("/", handler.HandleRoot)
r.Get("/settings", handler.HandleSettings)
r.Put("/update-settings", handler.HandleSettingsUpdate)
r.Get("/mappings", handler.HandleMappings)
r.Get("/mappings/{id}", handler.HandleMapping)
r.Get("/mappings/new", handler.HandleMappingNew)
r.Post("/mappings/add", handler.HandleMappingAdd)
r.Get("/mappings/edit/{id}", handler.HandleMappingEdit)
r.Put("/mappings/update/{id}", handler.HandleMappingUpdate)
r.Delete("/mappings/delete/{id}", handler.HandleMappingDelete)
r.Post("/search/suggest", handler.HandleSearchSuggest)
r.Get("/help", handler.HandleHelp)
})
// auth
r.Get("/auth/{provider}", handler.HandleProviderLogin)
r.Get("/auth/{provider}/callback", handler.HandleAuthCallbackFunction)
r.Get("/auth/{provider}/logout", handler.HandleLogout)
r.Get("/login", handler.HandleLogin)
// static content
r.Handle("/public/*", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
log.Printf("Server: Listening on %s:%s", config.Envs.PublicHost, config.Envs.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.Envs.Port), r))
}
func initStorage(db *sql.DB) {
err := db.Ping()
if err != nil {
log.Fatalf("Error pinging database: %v", err)
}
log.Println("DB: Connected!")
}