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
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

80
main.go
View File

@ -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)
// }
//}