added account
This commit is contained in:
68
main.go
68
main.go
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"mealprep/auth"
|
||||
"mealprep/database"
|
||||
"mealprep/handlers"
|
||||
"net/http"
|
||||
@@ -26,11 +27,32 @@ func main() {
|
||||
fs := http.FileServer(http.Dir("static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
|
||||
// Routes
|
||||
http.HandleFunc("/", indexHandler)
|
||||
// Authentication routes (public)
|
||||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
auth.RedirectIfAuthenticated(handlers.LoginPageHandler)(w, r)
|
||||
} else if r.Method == "POST" {
|
||||
handlers.LoginHandler(w, r)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
auth.RedirectIfAuthenticated(handlers.RegisterPageHandler)(w, r)
|
||||
} else if r.Method == "POST" {
|
||||
handlers.RegisterHandler(w, r)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
http.HandleFunc("/logout", handlers.LogoutHandler)
|
||||
|
||||
// Ingredients
|
||||
http.HandleFunc("/ingredients", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Protected routes
|
||||
http.HandleFunc("/", auth.RequireAuth(indexHandler))
|
||||
|
||||
// Ingredients (protected)
|
||||
http.HandleFunc("/ingredients", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
handlers.IngredientsHandler(w, r)
|
||||
@@ -39,17 +61,17 @@ func main() {
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
http.HandleFunc("/ingredients/", func(w http.ResponseWriter, r *http.Request) {
|
||||
}))
|
||||
http.HandleFunc("/ingredients/", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "DELETE" {
|
||||
handlers.DeleteIngredientHandler(w, r)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
// Meals
|
||||
http.HandleFunc("/meals", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Meals (protected)
|
||||
http.HandleFunc("/meals", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
handlers.MealsHandler(w, r)
|
||||
@@ -58,8 +80,8 @@ func main() {
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
http.HandleFunc("/meals/", func(w http.ResponseWriter, r *http.Request) {
|
||||
}))
|
||||
http.HandleFunc("/meals/", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
if strings.Contains(path, "/ingredients") {
|
||||
// Meal ingredients routes
|
||||
@@ -80,10 +102,10 @@ func main() {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
// Week Plan
|
||||
http.HandleFunc("/week-plan", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Week Plan (protected)
|
||||
http.HandleFunc("/week-plan", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
handlers.WeekPlanHandler(w, r)
|
||||
@@ -92,24 +114,23 @@ func main() {
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
http.HandleFunc("/week-plan/", func(w http.ResponseWriter, r *http.Request) {
|
||||
}))
|
||||
http.HandleFunc("/week-plan/", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "DELETE" {
|
||||
handlers.DeleteWeekPlanEntryHandler(w, r)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
// Grocery List
|
||||
http.HandleFunc("/grocery-list", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Grocery List (protected)
|
||||
http.HandleFunc("/grocery-list", auth.RequireAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
handlers.GroceryListHandler(w, r)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
|
||||
}))
|
||||
|
||||
// Start server
|
||||
port := "8080"
|
||||
@@ -155,7 +176,10 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
<body>
|
||||
<header>
|
||||
<div class="container">
|
||||
<h1>🍽️ Meal Prep Planner</h1>
|
||||
<div class="header-content">
|
||||
<h1>🍽️ Meal Prep Planner</h1>
|
||||
<a href="/logout" class="logout-btn">Logout</a>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user