update to volunteer

This commit is contained in:
Mann Patel
2025-08-28 17:09:23 -06:00
parent 9dd24e71e7
commit 1955407d7c
16 changed files with 1075 additions and 306 deletions

View File

@@ -1,14 +1,10 @@
// Add this debugging code to your main.go
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
@@ -20,30 +16,10 @@ import (
_ "github.com/lib/pq" // use PostgreSQL
)
// Custom file server with logging
func loggingFileServer(dir string) http.Handler {
fs := http.FileServer(http.Dir(dir))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Log the request
log.Printf("File request: %s", r.URL.Path)
// Check if file exists
filePath := filepath.Join(dir, r.URL.Path)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
log.Printf("File not found: %s", filePath)
http.NotFound(w, r)
return
}
log.Printf("Serving file: %s", filePath)
fs.ServeHTTP(w, r)
})
}
// Helper function to determine navigation visibility based on role
func getNavFlags(role int) (bool, bool, bool) {
showAdminNav := role == 1 // Admin role
showLeaderNav := role == 2 // Volunteer role
showLeaderNav := role == 2 // Team Leader role
showVolunteerNav := role == 3 // Volunteer role
return showAdminNav, showVolunteerNav, showLeaderNav
}
@@ -70,18 +46,14 @@ func createTemplateData(title, activeSection string, role int, isAuthenticated b
}
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
// Get individual components from environment variables
jwtSecret := os.Getenv("JWT_SECRET")
var jwtKey = []byte(jwtSecret)
return func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session")
if err != nil {
@@ -124,7 +96,6 @@ func volunteerMiddleware(next http.HandlerFunc) http.HandlerFunc {
return authMiddleware(func(w http.ResponseWriter, r *http.Request) {
role, ok := r.Context().Value("user_role").(int)
if !ok || (role != 3 && role != 2) {
fmt.Printf("Access denied: role %d not allowed\n", role) // Debug log
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -132,36 +103,29 @@ func volunteerMiddleware(next http.HandlerFunc) http.HandlerFunc {
})
}
// Updated handler functions using the helper
func schedualHandler(w http.ResponseWriter, r *http.Request) {
role := r.Context().Value("user_role").(int)
// currentUserID := r.Context().Value("user_id").(int)
data := createTemplateData("My Schedule", "schedual", role, true, nil)
utils.Render(w, "Schedual/schedual.html", data)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
utils.Render(w, "dashboard/dashboard.html", map[string]interface{}{
"Title": "Admin Dashboard",
"IsAuthenticated": false,
"ActiveSection": "dashboard",
})
"Title": "Admin Dashboard",
"IsAuthenticated": false,
"ActiveSection": "dashboard",
})
}
func main() {
models.InitDB()
// Static file servers with logging
// Static file servers
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
// Use logging file server for uploads
http.Handle("/uploads/", http.StripPrefix("/uploads/", loggingFileServer("uploads")))
uploadsFs := http.FileServer(http.Dir("uploads"))
http.Handle("/uploads/", http.StripPrefix("/uploads/", uploadsFs))
// Public HTML Routes
http.HandleFunc("/", HomeHandler)
@@ -187,17 +151,16 @@ func main() {
http.HandleFunc("/assign_address", adminMiddleware(handlers.AssignAddressHandler))
http.HandleFunc("/remove_assigned_address", adminMiddleware(handlers.RemoveAssignedAddressHandler))
http.HandleFunc("/posts", adminMiddleware(handlers.PostsHandler))
//--- Volunteer-only routes
http.HandleFunc("/volunteer/dashboard", volunteerMiddleware(handlers.VolunteerPostsHandler))
http.HandleFunc("/volunteer/Addresses", volunteerMiddleware(handlers.VolunteerAppointmentHandler))
http.HandleFunc("/schedual", volunteerMiddleware(schedualHandler))
// Poll routes (volunteer only)
http.HandleFunc("/poll", volunteerMiddleware(handlers.PollHandler))
log.Println("Server started on localhost:8080")
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}
}