Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ var createDomainCmd = &cobra.Command{

// Check config existence first
ext := ".conf"
configPath := filepath.Join("/etc/apache2/sites-available", domain+ext)
var configPath string

switch serverType {
case "nginx":
configPath = filepath.Join("/etc/nginx/sites-available", domain+ext)
default: // assume apache
configPath = filepath.Join("/etc/apache2/sites-available", domain+ext)
}

if _, err := os.Stat(configPath); err == nil {
fmt.Printf(" Configuration for '%s' already exists at %s\n", domain, configPath)
fmt.Println(" Aborting to prevent overwriting existing configuration.")
Expand Down Expand Up @@ -119,16 +127,36 @@ var createDomainCmd = &cobra.Command{

filename := fmt.Sprintf("%s%s", domain, configGen.GetFileExtension())

fmt.Println(" Enabling site with a2ensite...")
if err := internal.RunCommand("sudo", "a2ensite", filename); err != nil {
fmt.Printf(" Failed to enable site: %v\n", err)
os.Exit(1)
}
switch serverType {
case "apache":
fmt.Println(" Enabling site with a2ensite...")
if err := internal.RunCommand("sudo", "a2ensite", filename); err != nil {
fmt.Printf(" Failed to enable site: %v\n", err)
os.Exit(1)
}

fmt.Println("Reloading Apache server...")
if err := internal.RunCommand("sudo", "systemctl", "reload", "apache2"); err != nil {
fmt.Printf(" Failed to reload apache: %v\n", err)
os.Exit(1)
fmt.Println("Reloading Apache server...")
if err := internal.RunCommand("sudo", "systemctl", "reload", "apache2"); err != nil {
fmt.Printf(" Failed to reload apache: %v\n", err)
os.Exit(1)
}

case "nginx":
sitePath := filepath.Join("/etc/nginx/sites-available", filename)
linkPath := filepath.Join("/etc/nginx/sites-enabled", filename)
fmt.Println(" Enabling Nginx 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 nginx site: %v\n", err)
os.Exit(1)
}
}

fmt.Println("Reloading Nginx server...")
if err := internal.RunCommand("sudo", "systemctl", "reload", "nginx"); err != nil {
fmt.Printf(" Failed to reload nginx: %v\n", err)
os.Exit(1)
}
}

fmt.Printf("🎉 %s configuration created and enabled for %s on port %s\n", serverType, domain, port)
Expand Down Expand Up @@ -159,7 +187,15 @@ func printWelcome() {
}

func writeConfigFile(domain, content, extension string) error {
outputDir := "/etc/apache2/sites-available"
var outputDir string
if extension == ".conf" {
if strings.HasPrefix(content, "server") {
outputDir = "/etc/nginx/sites-available"
} else {
outputDir = "/etc/apache2/sites-available"
}
}

if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"stackroost/config/apache"
"stackroost/config/nginx"
)

// WebServerConfig defines the interface for generating web server configurations
Expand All @@ -16,7 +17,9 @@ func NewWebServerConfig(serverType string) (WebServerConfig, error) {
switch serverType {
case "apache":
return &apache.ApacheConfig{}, nil
case "nginx":
return &nginx.NginxConfig{}, nil
default:
return nil, fmt.Errorf("unsupported web server type: %s", serverType)
}
}
}
39 changes: 39 additions & 0 deletions config/nginx/nginx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package nginx

import "fmt"

// NginxConfig implements WebServerConfig for Nginx
type NginxConfig struct{}

func (n *NginxConfig) Generate(domain, port, username string) (string, error) {
config := `
server {
listen %s;
server_name %s www.%s;

root /home/%s/public_html;
index index.html index.htm index.php;

access_log /var/log/nginx/%s-access.log;
error_log /var/log/nginx/%s-error.log;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Update as per your PHP version
}

location ~ /\.ht {
deny all;
}
}
`
return fmt.Sprintf(config, port, domain, domain, username, domain, domain), nil
}

func (n *NginxConfig) GetFileExtension() string {
return ".conf"
}