f-21: refactor: add dispatcher layer for MQTT migration
Introduce internal/dispatcher package with a Command interface and typed commands (CreateVPC, DeleteVPC, CreateSubnet, DeleteSubnet). The API handlers now call dispatcher.Dispatch() instead of enqueuing closures directly, decoupling transport (HTTP today, MQTT tomorrow) from execution. Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
7ee8527582
commit
63a288f69e
9 changed files with 114 additions and 39 deletions
|
|
@ -4,17 +4,17 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/worker"
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
queue *worker.Queue
|
||||
db *badger.DB
|
||||
dispatcher *dispatcher.Dispatcher
|
||||
db *badger.DB
|
||||
}
|
||||
|
||||
func New(queue *worker.Queue, db *badger.DB) *Server {
|
||||
return &Server{queue: queue, db: db}
|
||||
func New(d *dispatcher.Dispatcher, db *badger.DB) *Server {
|
||||
return &Server{dispatcher: d, db: db}
|
||||
}
|
||||
|
||||
func (s *Server) Start(address string) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
)
|
||||
|
||||
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -31,11 +33,8 @@ func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string)
|
|||
}
|
||||
|
||||
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
|
||||
s.queue.Submit(func() {
|
||||
destroySubnet(name)
|
||||
})
|
||||
s.dispatcher.Dispatch(dispatcher.DeleteSubnetCommand{Name: name})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
|
||||
}
|
||||
|
||||
func destroySubnet(name string) {}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package agentapi
|
|||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
)
|
||||
|
||||
func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -34,8 +36,13 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"})
|
||||
return
|
||||
}
|
||||
s.queue.Submit(func() {
|
||||
createSubnet(req)
|
||||
s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{
|
||||
Name: req.Name,
|
||||
VPC: req.VPC,
|
||||
VxlanID: req.VxlanID,
|
||||
LocalIP: req.LocalIP,
|
||||
GatewayIP: req.GatewayIP,
|
||||
CIDR: req.CIDR,
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(Subnet{
|
||||
|
|
@ -49,4 +56,3 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
|||
})
|
||||
}
|
||||
|
||||
func createSubnet(req SubnetCreateRequest) {}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,10 @@ package agentapi
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
)
|
||||
|
||||
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -36,20 +33,8 @@ func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
|||
}
|
||||
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
s.queue.Submit(func() {
|
||||
kv.AddInDB(s.db, "vpc/"+name+"/state", "deleting")
|
||||
if err := vpc.DeleteVPC(s.db, name); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
if state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state"); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
} else if state == "deleted" {
|
||||
kv.DeleteInDB(s.db, "vpc/"+name)
|
||||
}
|
||||
})
|
||||
s.dispatcher.Dispatch(dispatcher.DeleteVPCCommand{Name: name})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
|
||||
}
|
||||
|
||||
func destroyVpc(name string) {}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@ package agentapi
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"git.g3e.fr/syonad/two/internal/dispatcher"
|
||||
)
|
||||
|
||||
func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -38,12 +36,7 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"})
|
||||
return
|
||||
}
|
||||
s.queue.Submit(func() {
|
||||
kv.AddInDB(s.db, "vpc/"+req.Name+"/state", "creating")
|
||||
if err := vpc.CreateVPC(s.db, req.Name); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
s.dispatcher.Dispatch(dispatcher.CreateVPCCommand{Name: req.Name})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue