Skip to content

Commit 88ce1bc

Browse files
committed
feat: add proxy support for extensions
1 parent 24e5ba4 commit 88ce1bc

File tree

3 files changed

+408
-65
lines changed

3 files changed

+408
-65
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

api/internal/features/extension/parser/validate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (p *Parser) validateStep(step ExecutionStep) error {
8181
"user": p.validateUserStep,
8282
"docker": p.validateDockerStep,
8383
"docker_compose": p.validateDockerComposeStep,
84+
"proxy": p.validateProxyStep,
8485
}
8586
v, ok := validators[step.Type]
8687
if !ok {
@@ -149,6 +150,21 @@ func (p *Parser) validateDockerComposeStep(step ExecutionStep) error {
149150
}
150151
}
151152

153+
func (p *Parser) validateProxyStep(step ExecutionStep) error {
154+
if err := p.requireProps(step, map[string]bool{"action": true}); err != nil {
155+
return err
156+
}
157+
action, _ := step.Properties["action"].(string)
158+
switch action {
159+
case "add", "update":
160+
return p.requireProps(step, map[string]bool{"domain": true, "port": true})
161+
case "remove":
162+
return p.requireProps(step, map[string]bool{"domain": true})
163+
default:
164+
return fmt.Errorf("unsupported proxy action: %s", action)
165+
}
166+
}
167+
152168
func (p *Parser) requireProps(step ExecutionStep, required map[string]bool) error {
153169
for key := range required {
154170
if _, ok := step.Properties[key]; !ok {

0 commit comments

Comments
 (0)