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,16 +9,44 @@ func (s *Server) SubnetsHandler(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.listSubnets(w, r)
case http.MethodPost:
s.queue.Submit(func() {
createSubnet()
})
w.WriteHeader(http.StatusAccepted)
s.postSubnet(w, r)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func createSubnet() {}
func (s *Server) listSubnets(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode([]Subnet{})
}
func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
var req SubnetCreateRequest
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 == "" || req.VPC == "" || req.LocalIP == "" || req.GatewayIP == "" || req.CIDR == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"})
return
}
s.queue.Submit(func() {
createSubnet(req)
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(Subnet{
Name: req.Name,
State: "creating",
VPC: req.VPC,
VxlanID: req.VxlanID,
LocalIP: req.LocalIP,
GatewayIP: req.GatewayIP,
CIDR: req.CIDR,
})
}
func createSubnet(req SubnetCreateRequest) {}