package routes import ( "api_serveurless_go/requette" "encoding/json" "fmt" "net/http" "strconv" "strings" ) // SetupRoutes configure toutes les routes de l'application func SetupRoutes() { http.HandleFunc("/", homeHandler) http.HandleFunc("/api/projets", projetsHandler) http.HandleFunc("/api/projet/", projetHandler) } // homeHandler gère la route racine func homeHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Welcome to my website!") } // projetsHandler gère la liste des projets func projetsHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(requette.GetAllProjets()) case "POST": fmt.Fprintf(w, "Créer un nouveau projet") default: http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed) } } // projetHandler gère les actions sur un projet spécifique func projetHandler(w http.ResponseWriter, r *http.Request) { id := strings.TrimPrefix(r.URL.Path, "/api/projet/") if id == "" { http.Error(w, "ID manquant", http.StatusBadRequest) return } idInt, err := strconv.Atoi(id) if err != nil { http.Error(w, "ID invalide", http.StatusBadRequest) return } switch r.Method { case "GET": w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(requette.GetProjetByID(idInt)) case "PUT": fmt.Fprintf(w, "Modifier le projet %d", idInt) case "DELETE": fmt.Fprintf(w, "Supprimer le projet %d", idInt) default: http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed) } }