package main import ( "encoding/json" "io/ioutil" "log" "net/http" "os" "strconv" "github.com/gorilla/mux" "go-sjles-pta-vote/server/models" "go-sjles-pta-vote/server/services" ) func voteHandler(w http.ResponseWriter, r *http.Request) { var vote models.Vote if err := json.NewDecoder(r.Body).Decode(&vote); err != nil { http.Error(w, "Invalid request payload", http.StatusBadRequest) return } err := services.SetVote(&vote) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) } func voteIDHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) idStr := vars["id"] id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { http.Error(w, "Invalid poll ID", http.StatusBadRequest) return } vote := models.Vote{ PollId: id, Email: "example@example.com", // Replace with actual email retrieval logic Vote: true, // Replace with actual vote retrieval logic } err = services.SetVote(&vote) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) } func statsHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { filePath := "./server/templates/stats.html" log.Printf("Serving stats.html from %s", filePath) http.ServeFile(w, r, filePath) } else if r.Method == "POST" { vars := mux.Vars(r) id := vars["id"] poll, err := services.GetPollByQuestion(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } json.NewEncoder(w).Encode(poll) } else { w.WriteHeader(http.StatusMethodNotAllowed) } } func statsIDHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] poll, err := services.GetPollByQuestion(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } json.NewEncoder(w).Encode(poll) } func adminHandler(w http.ResponseWriter, r *http.Request) { // Add admin functionality here w.WriteHeader(http.StatusOK) } func adminIDHandler(w http.ResponseWriter, r *http.Request) { //vars := mux.Vars(r) //id := vars["id"] // Add admin functionality here w.WriteHeader(http.StatusOK) } func adminMembersHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { filePath := "./server/templates/members.html" log.Printf("Serving members.html from %s", filePath) http.ServeFile(w, r, filePath) } else if r.Method == "POST" { var year int var err error r.ParseForm() if y := r.FormValue("year"); y != "" { year, err = strconv.Atoi(y) if err != nil { http.Error(w, "Invalid year", http.StatusBadRequest) return } } file, _, err := r.FormFile("members.csv") if err != nil { http.Error(w, "Failed to upload file", http.StatusBadRequest) return } defer file.Close() fileBytes, err := ioutil.ReadAll(file) if err != nil { http.Error(w, "Failed to read file", http.StatusInternalServerError) return } err = services.ParseMembersFromBytes(year, fileBytes) if err != nil { w.WriteHeader(http.StatusBadRequest) json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) return } w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]bool{"success": true}) } else { w.WriteHeader(http.StatusMethodNotAllowed) } } func main() { log.SetOutput(os.Stdout) log.SetFlags(log.LstdFlags | log.Lshortfile) log.Printf("Starting server on :8080") http.HandleFunc("/vote", voteHandler) http.HandleFunc("/vote/{id}", voteIDHandler) http.HandleFunc("/stats", statsHandler) http.HandleFunc("/stats/{id}", statsIDHandler) http.HandleFunc("/admin", adminHandler) http.HandleFunc("/admin/{id}", adminIDHandler) http.HandleFunc("/admin/members", adminMembersHandler) http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { filePath := "./server/icons/favicon.ico" http.ServeFile(w, r, filePath) }) log.Fatal(http.ListenAndServe(":8080", nil)) }