|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +const defaultYAML = `# Apex Proxy Configuration |
| 12 | +# High-performance reverse proxy configuration file |
| 13 | +
|
| 14 | +server: |
| 15 | + http_port: 80 |
| 16 | + https_port: 443 |
| 17 | + auto_tls: true |
| 18 | +
|
| 19 | +logging: |
| 20 | + csv_enabled: true |
| 21 | + csv_path: "/var/log/apex.csv" |
| 22 | + redact_headers: ["Authorization", "Cookie"] |
| 23 | +
|
| 24 | +middlewares: |
| 25 | + rate_limit: |
| 26 | + enabled: true |
| 27 | + requests_per_minute: 1000 |
| 28 | + compression: |
| 29 | + enabled: true |
| 30 | + types: ["text/html", "application/json"] |
| 31 | +
|
| 32 | +routing: |
| 33 | + - host: api.example.com |
| 34 | + path: / |
| 35 | + strategy: round-robin |
| 36 | + priority: 100 |
| 37 | + targets: |
| 38 | + - url: "http://127.0.0.1:3000" |
| 39 | + weight: 3 |
| 40 | + - url: "http://127.0.0.1:3001" |
| 41 | + weight: 1 |
| 42 | +
|
| 43 | + - host: example.com |
| 44 | + path: / |
| 45 | + strategy: single |
| 46 | + priority: 10 |
| 47 | + targets: |
| 48 | + - url: "http://127.0.0.1:5173" |
| 49 | +` |
| 50 | + |
| 51 | +const systemdTemplate = `[Unit] |
| 52 | +Description=Apex Proxy Server |
| 53 | +After=network.target |
| 54 | +
|
| 55 | +[Service] |
| 56 | +Type=simple |
| 57 | +ExecStart=/usr/local/bin/apex start --config /etc/apex/apex.yaml |
| 58 | +Restart=always |
| 59 | +RestartSec=5 |
| 60 | +LimitNOFILE=65536 |
| 61 | +
|
| 62 | +[Install] |
| 63 | +WantedBy=multi-user.target |
| 64 | +` |
| 65 | + |
| 66 | +var initCmd = &cobra.Command{ |
| 67 | + Use: "init", |
| 68 | + Short: "Installs apex globally and configures the systemd service", |
| 69 | + Run: func(cmd *cobra.Command, args []string) { |
| 70 | + if os.Geteuid() != 0 { |
| 71 | + fmt.Println("Error: apex init must be run as root. Try 'sudo apex init'") |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + |
| 75 | + execPath, err := os.Executable() |
| 76 | + if err != nil { |
| 77 | + fmt.Printf("Error resolving executable path: %v\n", err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + |
| 81 | + binData, err := os.ReadFile(execPath) |
| 82 | + if err != nil { |
| 83 | + fmt.Printf("Error reading binary: %v\n", err) |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + |
| 87 | + err = os.WriteFile("/usr/local/bin/apex", binData, 0755) |
| 88 | + if err != nil { |
| 89 | + fmt.Printf("Error writing binary to /usr/local/bin: %v\n", err) |
| 90 | + os.Exit(1) |
| 91 | + } |
| 92 | + |
| 93 | + err = os.MkdirAll("/etc/apex", 0755) |
| 94 | + if err != nil { |
| 95 | + fmt.Printf("Error creating /etc/apex directory: %v\n", err) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | + |
| 99 | + if _, err := os.Stat("/etc/apex/apex.yaml"); os.IsNotExist(err) { |
| 100 | + err = os.WriteFile("/etc/apex/apex.yaml", []byte(defaultYAML), 0644) |
| 101 | + if err != nil { |
| 102 | + fmt.Printf("Error writing config file: %v\n", err) |
| 103 | + os.Exit(1) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + err = os.WriteFile("/etc/systemd/system/apexproxy.service", []byte(systemdTemplate), 0644) |
| 108 | + if err != nil { |
| 109 | + fmt.Printf("Error writing systemd service: %v\n", err) |
| 110 | + os.Exit(1) |
| 111 | + } |
| 112 | + |
| 113 | + exec.Command("systemctl", "daemon-reload").Run() |
| 114 | + exec.Command("systemctl", "enable", "apexproxy").Run() |
| 115 | + exec.Command("systemctl", "restart", "apexproxy").Run() |
| 116 | + |
| 117 | + fmt.Println("Installation complete.") |
| 118 | + fmt.Println("Binary path: /usr/local/bin/apex") |
| 119 | + fmt.Println("Config path: /etc/apex/apex.yaml") |
| 120 | + fmt.Println("Service: apexproxy (running)") |
| 121 | + }, |
| 122 | +} |
| 123 | + |
| 124 | +func init() { |
| 125 | + rootCmd.AddCommand(initCmd) |
| 126 | +} |
0 commit comments