package src import ( "encoding/json" "fmt" "log" "net/http" "os" "path/filepath" "time" ) type SignupRequest struct { Email string `json:"email"` } func BetaSignupHandler(w http.ResponseWriter, r *http.Request) { log.Printf("\033[33m%s \033[0m ", "betahandler") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") if r.Method == http.MethodOptions { w.WriteHeader(http.StatusOK) return } if r.Method != http.MethodPost { log.Printf("\033[33m%s %s\033[0m ", "Method not allowed: ", r.Method) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Parse JSON request var req SignupRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "Invalid request", http.StatusBadRequest) return } // Get home directory homeDir, err := os.UserHomeDir() if err != nil { log.Printf("Error getting home directory: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } // Check if email folder exists folderPath := filepath.Join(homeDir, "Admin", "beta_signups", req.Email) if _, err := os.Stat(folderPath); !os.IsNotExist(err) { log.Printf("\033[31memail already exists!\033[0m") http.Error(w, "Email already exists", http.StatusBadRequest) return } // Create directory err = os.MkdirAll(folderPath, 0755) if err != nil { log.Printf("Error creating directory: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } // Format date and time now := time.Now() formattedDate := now.Format("1.2.2006") // M.D format formattedTime := now.Format("3:04pm") // h:mma format csvLine := fmt.Sprintf("\n%s,%s %s", req.Email, formattedDate, formattedTime) // Append to CSV file csvFilePath := filepath.Join(homeDir, "Admin", "beta", "beta_signups.csv") err = os.MkdirAll(filepath.Dir(csvFilePath), 0755) if err != nil { log.Printf("Error creating CSV directory: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } f, err := os.OpenFile(csvFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Printf("Error opening CSV file: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } defer f.Close() if _, err := f.WriteString(csvLine); err != nil { log.Printf("Error appending to CSV: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) }