|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/raghavyuva/caddygo" |
| 8 | + "github.com/raghavyuva/nixopus-api/internal/config" |
| 9 | + "github.com/raghavyuva/nixopus-api/internal/features/deploy/tasks" |
| 10 | + "github.com/raghavyuva/nixopus-api/internal/features/ssh" |
| 11 | + "github.com/raghavyuva/nixopus-api/internal/types" |
| 12 | +) |
| 13 | + |
| 14 | +type proxyModule struct{} |
| 15 | + |
| 16 | +func (proxyModule) Type() string { return "proxy" } |
| 17 | + |
| 18 | +func AddDomainToProxy(domain string, port string) error { |
| 19 | + client := tasks.GetCaddyClient() |
| 20 | + p, err := strconv.Atoi(port) |
| 21 | + if err != nil { |
| 22 | + return fmt.Errorf("invalid port: %w", err) |
| 23 | + } |
| 24 | + upstreamHost := config.AppConfig.SSH.Host |
| 25 | + if err := client.AddDomainWithAutoTLS(domain, upstreamHost, p, caddygo.DomainOptions{}); err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + client.Reload() |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func UpdateDomainInProxy(domain string, port string) error { |
| 33 | + return AddDomainToProxy(domain, port) |
| 34 | +} |
| 35 | + |
| 36 | +func RemoveDomainFromProxy(domain string) error { |
| 37 | + client := tasks.GetCaddyClient() |
| 38 | + if err := client.DeleteDomain(domain); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + client.Reload() |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func (proxyModule) Execute(sshClient *ssh.SSH, step types.SpecStep, vars map[string]interface{}) (string, func(), error) { |
| 46 | + action, _ := step.Properties["action"].(string) |
| 47 | + domain, _ := step.Properties["domain"].(string) |
| 48 | + port, _ := step.Properties["port"].(string) |
| 49 | + |
| 50 | + switch action { |
| 51 | + case "add": |
| 52 | + if domain == "" || port == "" { |
| 53 | + return "", nil, fmt.Errorf("domain and port are required") |
| 54 | + } |
| 55 | + if err := AddDomainToProxy(domain, port); err != nil { |
| 56 | + return "", nil, err |
| 57 | + } |
| 58 | + return fmt.Sprintf("proxy added for %s -> %s:%s", domain, config.AppConfig.SSH.Host, port), nil, nil |
| 59 | + case "update": |
| 60 | + if domain == "" || port == "" { |
| 61 | + return "", nil, fmt.Errorf("domain and port are required") |
| 62 | + } |
| 63 | + if err := UpdateDomainInProxy(domain, port); err != nil { |
| 64 | + return "", nil, err |
| 65 | + } |
| 66 | + return fmt.Sprintf("proxy updated for %s -> %s:%s", domain, config.AppConfig.SSH.Host, port), nil, nil |
| 67 | + case "remove": |
| 68 | + if domain == "" { |
| 69 | + return "", nil, fmt.Errorf("domain is required") |
| 70 | + } |
| 71 | + if err := RemoveDomainFromProxy(domain); err != nil { |
| 72 | + return "", nil, err |
| 73 | + } |
| 74 | + return fmt.Sprintf("proxy removed for %s", domain), nil, nil |
| 75 | + default: |
| 76 | + return "", nil, fmt.Errorf("unsupported proxy action: %s", action) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func init() { |
| 81 | + RegisterModule(proxyModule{}) |
| 82 | +} |
0 commit comments