func (m HaProxy) CreateConfigFromTemplates() error {
	configsContent, err := m.getConfigs()
	if err != nil {
		return err
	}
	configPath := fmt.Sprintf("%s/haproxy.cfg", m.configsPath)
	return writeFile(configPath, []byte(configsContent), 0664)
}
CreateConfigFromTemplates creates haproxy.cfg configuration file based on templates
package proxy
import (
	"bytes"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
	"text/template"
	"time"
)
type HaProxy struct {
	templatesPath string
	configsPath   string
	configData    configData
}
var Instance proxy
var reloadPause time.Duration = 1000
var certMu = &sync.Mutex{}
var reloadMu = &sync.Mutex{}
type configData struct {
	CertsString          string
	ContentFrontend      string
	ConnectionMode       string
	ContentFrontendSNI   string
	ContentFrontendTcp   string
	ContentListen        string
	DefaultBinds         string
	DefaultReqMode       string
	ExtraDefaults        string
	ExtraFrontend        string
	ExtraGlobal          string
	Resolvers            []string
	Stats                string
	TimeoutConnect       string
	TimeoutClient        string
	TimeoutServer        string
	TimeoutQueue         string
	TimeoutTunnel        string
	TimeoutHttpRequest   string
	TimeoutHttpKeepAlive string
	SslBindOptions       string
	SslBindCiphers       string
	UserList             string
}
func NewHaProxy(templatesPath, configsPath string) proxy {
	dataInstance.Services = map[string]Service{}
	return HaProxy{
		templatesPath: templatesPath,
		configsPath:   configsPath,
	}
}
func (m HaProxy) GetCertPaths() []string {
	preferredCertsGlobs := strings.Split(os.Getenv("PREFERRED_CERTIFICATE"), ",")
	pathsList := [][]string{[]string{}}
	defaultIndex := len(preferredCertsGlobs)
	for range preferredCertsGlobs {
		pathsList = append(pathsList, []string{})
	}
	files, _ := readDir("/certs")
	for _, file := range files {
		if !file.IsDir() {
			path := fmt.Sprintf("/certs/%s", file.Name())
			foundGlob := false
			for idx, pattern := range preferredCertsGlobs {
				if Glob(pattern, file.Name()) {
					pathsList[idx] = append(pathsList[idx], path)
					foundGlob = true
					break
				}
			}
			if !foundGlob {
				pathsList[defaultIndex] = append(pathsList[defaultIndex], path)
			}
		}
	}
	files, _ = readDir("/run/secrets")
	for _, file := range files {
		if !file.IsDir() {
			lName := strings.ToLower(file.Name())
			if strings.HasPrefix(lName, "cert-") || strings.HasPrefix(lName, "cert_") {
				path := fmt.Sprintf("/run/secrets/%s", file.Name())
				foundGlob := false
				for idx, pattern := range preferredCertsGlobs {
					if Glob(pattern, file.Name()) {
						pathsList[idx] = append(pathsList[idx], path)
						foundGlob = true
						break
					}
				}
				if !foundGlob {
					pathsList[defaultIndex] = append(pathsList[defaultIndex], path)
				}
			}
		}
	}
	outputPaths := []string{}
	for _, paths := range pathsList {
		for _, path := range paths {
			outputPaths = append(outputPaths, path)
		}
	}
	return outputPaths
}
func (m HaProxy) GetCerts() map[string]string {
	certs := map[string]string{}
	paths := m.GetCertPaths()
	for _, path := range paths {
		content, _ := ReadFile(path)
		certs[path] = string(content)
	}
	return certs
}
func (m HaProxy) RunCmd(extraArgs []string) error {
	args := []string{
		"-f",
		"/cfg/haproxy.cfg",
		"-D",
		"-p",
		"/var/run/haproxy.pid",
	}
	args = append(args, extraArgs...)
	if err := cmdRunHa(args); err != nil {
		configData := ""
		if strings.EqualFold(os.Getenv("DISPLAY_CONFIG_ON_ERROR"), "true") {
			data, _ := readConfigsFile("/cfg/haproxy.cfg")
			configData = "\n" + string(data)
		}
		return fmt.Errorf(
			"Command %s\n%s%s",
			strings.Join(args, " "),
			err.Error(),
			string(configData),
		)
	}
	return nil
}