Files
portfolio/routes/projet.go
T
= 4335fd5cae Add initial project structure with database connection and routing setup
- Create .env.example and .gitignore to manage environment variables
- Implement database connection logic in bd.go
- Define project model and data retrieval functions in projet.go
- Set up HTTP routes for project management in routes/projet.go
- Add main entry point in main.go to start the server
2025-09-23 09:35:10 +02:00

63 lines
1.6 KiB
Go

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)
}
}