This commit is contained in:
norohind 2025-01-15 14:07:51 +01:00
parent fe1bfe7cfd
commit 27086d6233
Signed by: norohind
SSH Key Fingerprint: SHA256:SnI4bWnejM2/YEQ5hpH58TUohiQpnjoKN6tXUQlobE0
2 changed files with 42 additions and 43 deletions

5
go.mod
View File

@ -2,7 +2,10 @@ module caddy-docker-proxy-simulator
go 1.22.6 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 ( require (
filippo.io/edwards25519 v1.1.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect

80
main.go
View File

@ -1,16 +1,17 @@
package main package main
import ( import (
"encoding/json" "flag"
"fmt" "fmt"
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile" "github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
"io" "gopkg.in/yaml.v2"
"net/http" "log"
"os"
) )
func generateConfig(labels map[string]string, upstreams string) []byte { func generateConfig(labels map[string]string, upstreams string) []byte {
UpstreamsFunc := map[string]any{ UpstreamsFunc := map[string]any{
"upstreams": func() string { "upstreams": func(upses ...any) string {
return upstreams return upstreams
}, },
} }
@ -23,52 +24,47 @@ func generateConfig(labels map[string]string, upstreams string) []byte {
return container.Marshal() return container.Marshal()
} }
func main() { type DockerCompose struct {
// CLI Flavor Services map[string]Service `yaml:"services"`
labels := map[string]string{
"caddy": "example.com",
"caddy.reverse_proxy": "{{upstreams}}",
}
config := generateConfig(labels, "localhost")
fmt.Print(string(config[:]))
} }
func configHandler(w http.ResponseWriter, r *http.Request) { type Service struct {
// Parse query parameters Labels map[string]string `yaml:"labels"`
upstreams := r.URL.Query().Get("upstreams") }
if upstreams == "" {
upstreams = "example.com"
}
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 { if err != nil {
http.Error(w, "Unable to read request body", http.StatusBadRequest) log.Fatalf("Failed to open docker-compose file: %v", err)
return
} }
defer r.Body.Close() defer file.Close()
// Decode the JSON body into the labels map // Parse the docker-compose file into a struct
var labels map[string]string var compose DockerCompose
if err := json.Unmarshal(body, &labels); err != nil { err = yaml.NewDecoder(file).Decode(&compose)
http.Error(w, "Invalid JSON format", http.StatusBadRequest) if err != nil {
return 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) config := generateConfig(labels, upstreams)
// Respond with the generated config // Output the generated config
w.Header().Set("Content-Type", "application/text") fmt.Print(string(config))
w.Write(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)
// }
//}