feat: Initialize project with MySQL database and API for managing projects and technologies
- Created SQL script to set up database tables for projects and technologies. - Added Docker Compose configuration for application and MySQL service. - Implemented API endpoints for CRUD operations on technologies. - Defined data types for projects and technologies. - Established routing for API endpoints.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"api_serveurless_go/requette"
|
||||
"api_serveurless_go/types"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// technologiesHandler gère la liste des technologies
|
||||
func technologiesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", os.Getenv("FRONTEND_URL")) // <-- Remplace avec le port de ton front-end
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(requette.GetAllTechnologies())
|
||||
case "POST":
|
||||
|
||||
var technologie types.TechnologieCreate
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode(&technologie)
|
||||
if err != nil {
|
||||
http.Error(w, "Erreur de décodage JSON: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if requette.CreateTechnologie(technologie) == nil {
|
||||
http.Error(w, "Erreur lors de la création de la technologie", http.StatusInternalServerError)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
case "OPTIONS":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
|
||||
default:
|
||||
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user