f-21: code: separate and create route fonction

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-04-16 22:57:20 +02:00
commit 75e02bd4f7
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
6 changed files with 145 additions and 35 deletions

View file

@ -9,16 +9,36 @@ func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode([]interface{}{})
s.listVpcs(w, r)
case http.MethodPost:
s.queue.Submit(func() {
createVpc()
})
w.WriteHeader(http.StatusAccepted)
s.postVpc(w, r)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func createVpc() {}
func (s *Server) listVpcs(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode([]VPC{})
}
func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
var req VPCCreateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
return
}
if req.Name == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"})
return
}
s.queue.Submit(func() {
createVpc(req.Name)
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
}
func createVpc(name string) {}