From 78a987d04be44a81fa328c6cd3522f0b5a873ade Mon Sep 17 00:00:00 2001 From: mahesh bhatiya Date: Tue, 10 Jun 2025 06:53:08 +0530 Subject: [PATCH] added caddy server configuration... --- cmd/root.go | 31 ++++++++++++++++++++++++++----- config/caddy/caddy.go | 32 ++++++++++++++++++++++++++++++++ config/config.go | 3 +++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 config/caddy/caddy.go diff --git a/cmd/root.go b/cmd/root.go index 1d2c98b..f3f55f3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,19 +37,20 @@ var createDomainCmd = &cobra.Command{ if internal.IsNilOrEmpty(port) { port = "80" } - - // Extract username from domain username := strings.Split(domain, ".")[0] - - // Check config existence first + ext := ".conf" var configPath string switch serverType { case "nginx": configPath = filepath.Join("/etc/nginx/sites-available", domain+ext) - default: // assume apache + case "caddy": + configPath = filepath.Join("/etc/caddy/sites-available", domain+ext) + case "apache": configPath = filepath.Join("/etc/apache2/sites-available", domain+ext) + default: + fmt.Println("Error: Unsupported server type. Supported types are: apache, nginx, caddy") } if _, err := os.Stat(configPath); err == nil { @@ -157,6 +158,24 @@ var createDomainCmd = &cobra.Command{ fmt.Printf(" Failed to reload nginx: %v\n", err) os.Exit(1) } + + case "caddy": + sitePath := filepath.Join("/etc/caddy/sites-available", filename) + linkPath := filepath.Join("/etc/caddy/sites-enabled", filename) + fmt.Println(" Enabling Caddy site...") + + if _, err := os.Stat(linkPath); os.IsNotExist(err) { + if err := internal.RunCommand("sudo", "ln", "-s", sitePath, linkPath); err != nil { + fmt.Printf(" Failed to enable caddy site: %v\n", err) + os.Exit(1) + } + } + } + + fmt.Println("Reloading Caddy server...") + if err := internal.RunCommand("sudo", "systemctl", "reload", "caddy"); err != nil { + fmt.Printf(" Failed to reload caddy: %v\n", err) + os.Exit(1) } fmt.Printf("🎉 %s configuration created and enabled for %s on port %s\n", serverType, domain, port) @@ -191,6 +210,8 @@ func writeConfigFile(domain, content, extension string) error { if extension == ".conf" { if strings.HasPrefix(content, "server") { outputDir = "/etc/nginx/sites-available" + } else if strings.Contains(content, "php_fastcgi") { + outputDir = "/etc/caddy/sites-available" } else { outputDir = "/etc/apache2/sites-available" } diff --git a/config/caddy/caddy.go b/config/caddy/caddy.go new file mode 100644 index 0000000..dc3eb00 --- /dev/null +++ b/config/caddy/caddy.go @@ -0,0 +1,32 @@ +package caddy + +import "fmt" + +// CaddyConfig implements WebServerConfig for Caddy +type CaddyConfig struct{} + +func (c *CaddyConfig) Generate(domain, port, username string) (string, error) { + config := ` +%s:%s { + root * /home/%s/public_html + file_server + encode gzip + + php_fastcgi unix//run/php/php8.1-fpm.sock + + log { + output file /var/log/caddy/%s-access.log + format single_field common_log + } + + handle_errors { + respond "Something went wrong" 500 + } +} +` + return fmt.Sprintf(config, domain, port, username, domain), nil +} + +func (c *CaddyConfig) GetFileExtension() string { + return ".conf" +} diff --git a/config/config.go b/config/config.go index 40fd6ce..8e9ae9d 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "fmt" "stackroost/config/apache" "stackroost/config/nginx" + "stackroost/config/caddy" ) // WebServerConfig defines the interface for generating web server configurations @@ -19,6 +20,8 @@ func NewWebServerConfig(serverType string) (WebServerConfig, error) { return &apache.ApacheConfig{}, nil case "nginx": return &nginx.NginxConfig{}, nil + case "caddy": + return &caddy.CaddyConfig{}, nil default: return nil, fmt.Errorf("unsupported web server type: %s", serverType) }