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 327a359007
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
6 changed files with 145 additions and 35 deletions

View file

@ -9,22 +9,33 @@ import (
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
if name == "" {
http.NotFound(w, r)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
return
}
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"name": name})
s.getVpc(w, r, name)
case http.MethodDelete:
s.queue.Submit(func() {
deleteVpc(name)
})
w.WriteHeader(http.StatusAccepted)
s.deleteVpc(w, r, name)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func deleteVpc(name string) {}
func (s *Server) getVpc(w http.ResponseWriter, r *http.Request, name string) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
}
func (s *Server) deleteVpc(w http.ResponseWriter, r *http.Request, name string) {
s.queue.Submit(func() {
destroyVpc(name)
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
}
func destroyVpc(name string) {}