Files
website/main.go

65 lines
1.9 KiB
Go

package main
import (
"database/sql"
"log"
"net/http"
"synlotto-website/handlers"
admin "synlotto-website/handlers/admin"
"synlotto-website/helpers"
"synlotto-website/middleware"
"synlotto-website/models"
"synlotto-website/storage"
"github.com/gorilla/csrf"
)
func main() {
db := storage.InitDB("synlotto.db")
models.SetDB(db)
csrfMiddleware := csrf.Protect(
[]byte("abcdefghijklmnopqrstuvwx12345678"), // TodO: Make Global
csrf.Secure(true),
csrf.Path("/"),
)
mux := http.NewServeMux()
setupAdminRoutes(mux, db)
setupAccountRoutes(mux, db)
setupResultRoutes(mux, db)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
mux.HandleFunc("/", handlers.Home(db))
log.Println("🌐 Running on http://localhost:8080")
http.ListenAndServe(":8080", helpers.RateLimit(csrfMiddleware(mux)))
}
func setupAdminRoutes(mux *http.ServeMux, db *sql.DB) {
mux.HandleFunc("/admin/dashboard", admin.AdminDashboardHandler(db))
mux.HandleFunc("/admin/triggers", admin.AdminTriggersHandler(db))
// Draw management
mux.HandleFunc("/admin/draws/new", admin.NewDrawHandler(db))
mux.HandleFunc("/admin/draws/modify", admin.ModifyDrawHandler(db))
mux.HandleFunc("/admin/draws/delete", admin.DeleteDrawHandler(db))
// Prize management
mux.HandleFunc("/admin/draws/prizes/add", admin.AddPrizesHandler(db))
mux.HandleFunc("/admin/draws/prizes/modify", admin.ModifyPrizesHandler(db))
}
func setupAccountRoutes(mux *http.ServeMux, db *sql.DB) {
mux.HandleFunc("/login", middleware.Auth(false)(handlers.Login))
mux.HandleFunc("/logout", handlers.Logout)
mux.HandleFunc("/signup", middleware.Auth(false)(handlers.Signup))
mux.HandleFunc("/account/tickets/add_ticket", handlers.AddTicket(db))
mux.HandleFunc("/account/tickets/my_tickets", handlers.GetMyTickets(db))
}
func setupResultRoutes(mux *http.ServeMux, db *sql.DB) {
mux.HandleFunc("/results/thunderball", handlers.ResultsThunderball(db))
}