f-21: code: ajout d'un system de worker
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
dc701886eb
commit
2fbde24e89
8 changed files with 87 additions and 14 deletions
|
|
@ -3,14 +3,24 @@ package agentapi
|
|||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/worker"
|
||||
)
|
||||
|
||||
func Start(address string) {
|
||||
type Server struct {
|
||||
queue *worker.Queue
|
||||
}
|
||||
|
||||
func New(queue *worker.Queue) *Server {
|
||||
return &Server{queue: queue}
|
||||
}
|
||||
|
||||
func (s *Server) Start(address string) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/vpcs", VpcsHandler)
|
||||
mux.HandleFunc("/vpcs/", VpcByNameHandler)
|
||||
mux.HandleFunc("/subnets", SubnetsHandler)
|
||||
mux.HandleFunc("/subnets/", SubnetByNameHandler)
|
||||
mux.HandleFunc("/vpcs", s.VpcsHandler)
|
||||
mux.HandleFunc("/vpcs/", s.VpcByNameHandler)
|
||||
mux.HandleFunc("/subnets", s.SubnetsHandler)
|
||||
mux.HandleFunc("/subnets/", s.SubnetByNameHandler)
|
||||
log.Printf("API server listening on %s", address)
|
||||
log.Fatal(http.ListenAndServe(address, mux))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/subnets/")
|
||||
if name == "" {
|
||||
http.NotFound(w, r)
|
||||
|
|
@ -18,8 +18,13 @@ func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||
case http.MethodDelete:
|
||||
s.queue.Submit(func() {
|
||||
deleteSubnet(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteSubnet(name string) {}
|
||||
|
|
|
|||
|
|
@ -5,15 +5,20 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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{}{})
|
||||
case http.MethodPost:
|
||||
s.queue.Submit(func() {
|
||||
createSubnet()
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func createSubnet() {}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
|
||||
if name == "" {
|
||||
http.NotFound(w, r)
|
||||
|
|
@ -18,8 +18,13 @@ func VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||
case http.MethodDelete:
|
||||
s.queue.Submit(func() {
|
||||
deleteVpc(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteVpc(name string) {}
|
||||
|
|
|
|||
|
|
@ -5,15 +5,20 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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{}{})
|
||||
case http.MethodPost:
|
||||
s.queue.Submit(func() {
|
||||
createVpc()
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func createVpc() {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue