Skip to content

Commit 2492538

Browse files
committed
feat(proxy) : Preserves the existing config and appends without touching the previous
1 parent a9384dd commit 2492538

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

api/api/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"version": "v1",
55
"status": "active",
6-
"release_date": "2025-04-18T16:26:18.53547+05:30",
6+
"release_date": "2025-04-18T19:33:49.49148+05:30",
77
"end_of_life": "0001-01-01T00:00:00Z",
88
"changes": [
99
"Initial API version"

api/internal/features/deploy/proxy/caddy.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ func (c *Caddy) Serve() error {
3535
return fmt.Errorf("caddy is not running: %w", err)
3636
}
3737

38-
currentConfig, err := c.GetConfig()
39-
if err != nil {
40-
return fmt.Errorf("failed to get current config: %w", err)
41-
}
42-
4338
var handle interface{}
4439
if c.FileServerType == FileServer {
4540
handle = FileServerHandle{
@@ -77,28 +72,26 @@ func (c *Caddy) Serve() error {
7772
Handle: []interface{}{handle},
7873
}
7974

80-
if currentConfig.Apps.HTTP.Servers == nil {
81-
currentConfig.Apps.HTTP.Servers = make(map[string]Server)
75+
jsonData, err := json.Marshal([]Route{routeConfig})
76+
if err != nil {
77+
return fmt.Errorf("failed to marshal route config: %w", err)
8278
}
83-
server := currentConfig.Apps.HTTP.Servers["nixopus"]
8479

85-
routeExists := false
86-
for i, route := range server.Routes {
87-
if len(route.Match) > 0 && len(route.Match[0].Host) > 0 && route.Match[0].Host[0] == c.Domain {
88-
server.Routes[i] = routeConfig
89-
routeExists = true
90-
break
91-
}
80+
req, err := http.NewRequest("POST", c.Endpoint+"/config/apps/http/servers/nixopus/routes/...", bytes.NewBuffer(jsonData))
81+
if err != nil {
82+
return fmt.Errorf("failed to create request: %w", err)
9283
}
84+
req.Header.Set("Content-Type", "application/json")
9385

94-
if !routeExists {
95-
server.Routes = append(server.Routes, routeConfig)
86+
resp, err := c.client.Do(req)
87+
if err != nil {
88+
return fmt.Errorf("failed to send request: %w", err)
9689
}
90+
defer resp.Body.Close()
9791

98-
currentConfig.Apps.HTTP.Servers["nixopus"] = server
99-
100-
if err := c.loadConfig(currentConfig); err != nil {
101-
return fmt.Errorf("failed to load config: %w", err)
92+
if resp.StatusCode != http.StatusOK {
93+
body, _ := io.ReadAll(resp.Body)
94+
return fmt.Errorf("failed to append route: %s - %s", resp.Status, string(body))
10295
}
10396

10497
c.Logger.Log(logger.Info, "Caddy server started successfully", "")

0 commit comments

Comments
 (0)