From 27086d6233182a4bdb557e13d1c88e1ee7615de6 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:07:51 +0100 Subject: [PATCH] Cli --- go.mod | 5 +++- main.go | 80 +++++++++++++++++++++++++++------------------------------ 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index 9933630..205800a 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module caddy-docker-proxy-simulator go 1.22.6 -require github.com/lucaslorentz/caddy-docker-proxy/v2 v2.9.1 +require ( + github.com/lucaslorentz/caddy-docker-proxy/v2 v2.9.1 + gopkg.in/yaml.v2 v2.4.0 +) require ( filippo.io/edwards25519 v1.1.0 // indirect diff --git a/main.go b/main.go index 4a2f89d..74a1d7c 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,17 @@ package main import ( - "encoding/json" + "flag" "fmt" "github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile" - "io" - "net/http" + "gopkg.in/yaml.v2" + "log" + "os" ) func generateConfig(labels map[string]string, upstreams string) []byte { UpstreamsFunc := map[string]any{ - "upstreams": func() string { + "upstreams": func(upses ...any) string { return upstreams }, } @@ -23,52 +24,47 @@ func generateConfig(labels map[string]string, upstreams string) []byte { return container.Marshal() } -func main() { - // CLI Flavor - labels := map[string]string{ - "caddy": "example.com", - "caddy.reverse_proxy": "{{upstreams}}", - } - config := generateConfig(labels, "localhost") - fmt.Print(string(config[:])) +type DockerCompose struct { + Services map[string]Service `yaml:"services"` } -func configHandler(w http.ResponseWriter, r *http.Request) { - // Parse query parameters - upstreams := r.URL.Query().Get("upstreams") - if upstreams == "" { - upstreams = "example.com" - } +type Service struct { + Labels map[string]string `yaml:"labels"` +} - body, err := io.ReadAll(r.Body) +func main() { + // Define command-line flag to accept docker-compose file path + composeFilePath := flag.String("compose-file", "docker-compose.yml", "Path to docker-compose file") + flag.Parse() + + // Open the docker-compose file + file, err := os.Open(*composeFilePath) if err != nil { - http.Error(w, "Unable to read request body", http.StatusBadRequest) - return + log.Fatalf("Failed to open docker-compose file: %v", err) } - defer r.Body.Close() + defer file.Close() - // Decode the JSON body into the labels map - var labels map[string]string - if err := json.Unmarshal(body, &labels); err != nil { - http.Error(w, "Invalid JSON format", http.StatusBadRequest) - return + // Parse the docker-compose file into a struct + var compose DockerCompose + err = yaml.NewDecoder(file).Decode(&compose) + if err != nil { + log.Fatalf("Failed to parse docker-compose file: %v", err) } - // Generate config + // Example: Mapping service labels to our desired format + labels := map[string]string{} + for serviceName, service := range compose.Services { + for labelKey, labelValue := range service.Labels { + // Modify this logic as needed to map labels to desired format + mappedKey := fmt.Sprintf("%s.%s", serviceName, labelKey) + labels[mappedKey] = labelValue + } + } + + // Generate config with extracted labels and upstreams + upstreams := "localhost" // This could be passed as a cmd argument or dynamically set config := generateConfig(labels, upstreams) - // Respond with the generated config - w.Header().Set("Content-Type", "application/text") - - w.Write(config) + // Output the generated config + fmt.Print(string(config)) } - -//func main() { -// API flavor -// http.HandleFunc("/generate-config", configHandler) // Set up the route -// fmt.Println("Starting server on port 8080...") -// err := http.ListenAndServe(":8080", nil) // Start the HTTP server on port 8080 -// if err != nil { -// fmt.Println("Error starting server:", err) -// } -//}