Skip to content

Commit 228ae5e

Browse files
committed
feat: support for containers routes to multi server
1 parent db234ac commit 228ae5e

File tree

15 files changed

+81
-35
lines changed

15 files changed

+81
-35
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-12-11T14:46:46.935266+05:30",
6+
"release_date": "2025-12-11T14:57:08.042103+05:30",
77
"end_of_life": "0001-01-01T00:00:00Z",
88
"changes": [
99
"Initial API version"

api/internal/features/container/controller/get_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func (c *ContainerController) GetContainer(f fuego.ContextNoBody) (*shared_types.Response, error) {
1414
containerID := f.PathParam("container_id")
1515

16-
containerInfo, err := c.dockerService.GetContainerById(containerID)
16+
containerInfo, err := c.getDockerService(f.Request().Context()).GetContainerById(containerID)
1717
if err != nil {
1818
c.logger.Log(logger.Error, err.Error(), "")
1919
return nil, fuego.HTTPError{

api/internal/features/container/controller/get_container_logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (c *ContainerController) GetContainerLogs(f fuego.ContextWithBody[types.Con
2323
}
2424
}
2525

26-
logsReader, err := c.dockerService.GetContainerLogs(req.ID, container.LogsOptions{
26+
logsReader, err := c.getDockerService(f.Request().Context()).GetContainerLogs(req.ID, container.LogsOptions{
2727
Follow: req.Follow,
2828
Tail: strconv.Itoa(req.Tail),
2929
Since: req.Since,

api/internal/features/container/controller/init.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"github.com/raghavyuva/nixopus-api/internal/features/notification"
1111
shared_storage "github.com/raghavyuva/nixopus-api/internal/storage"
1212
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
13+
"github.com/raghavyuva/nixopus-api/internal/utils"
1314
)
1415

1516
type ContainerController struct {
16-
store *shared_storage.Store
17-
dockerService *docker.DockerService
18-
ctx context.Context
19-
logger logger.Logger
20-
notification *notification.NotificationManager
17+
store *shared_storage.Store
18+
ctx context.Context
19+
logger logger.Logger
20+
notification *notification.NotificationManager
2121
}
2222

2323
func NewContainerController(
@@ -27,16 +27,22 @@ func NewContainerController(
2727
notificationManager *notification.NotificationManager,
2828
) *ContainerController {
2929
return &ContainerController{
30-
store: store,
31-
dockerService: docker.NewDockerService(),
32-
ctx: ctx,
33-
logger: l,
34-
notification: notificationManager,
30+
store: store,
31+
ctx: ctx,
32+
logger: l,
33+
notification: notificationManager,
3534
}
3635
}
3736

38-
func (c *ContainerController) isProtectedContainer(containerID string, action string) (*shared_types.Response, bool) {
39-
details, err := c.dockerService.GetContainerById(containerID)
37+
// getDockerService creates a DockerService using the active server from the database
38+
// It extracts the organization ID from the request context (set by supertokens middleware)
39+
func (c *ContainerController) getDockerService(ctx context.Context) *docker.DockerService {
40+
organizationID := utils.GetOrganizationIDFromContext(ctx)
41+
return docker.NewDockerServiceWithServer(c.store.DB, ctx, organizationID)
42+
}
43+
44+
func (c *ContainerController) isProtectedContainer(ctx context.Context, containerID string, action string) (*shared_types.Response, bool) {
45+
details, err := c.getDockerService(ctx).GetContainerById(containerID)
4046
if err != nil {
4147
return nil, false
4248
}

api/internal/features/container/controller/list_containers.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"context"
45
"net/http"
56
"sort"
67
"strconv"
@@ -19,7 +20,7 @@ func (c *ContainerController) ListContainers(fuegoCtx fuego.ContextNoBody) (*sha
1920
params := parseContainerListParams(fuegoCtx.Request())
2021

2122
// Get pre-filtered summaries from Docker
22-
containers, err := c.dockerService.ListContainers(container.ListOptions{
23+
containers, err := c.getDockerService(fuegoCtx.Request().Context()).ListContainers(container.ListOptions{
2324
All: true,
2425
Filters: buildDockerFilters(params),
2526
})
@@ -34,7 +35,7 @@ func (c *ContainerController) ListContainers(fuegoCtx fuego.ContextNoBody) (*sha
3435
rows := summarizeContainers(containers)
3536
pageRows, totalCount := applySearchSortPaginate(rows, params)
3637

37-
result := c.appendContainerInfo(pageRows, containers)
38+
result := c.appendContainerInfo(fuegoCtx.Request().Context(), pageRows, containers)
3839

3940
return &shared_types.Response{
4041
Status: "success",
@@ -186,10 +187,11 @@ func applySearchSortPaginate(rows []containertypes.ContainerListRow, p container
186187
return rows[start:end], totalCount
187188
}
188189

189-
func (c *ContainerController) appendContainerInfo(pageRows []containertypes.ContainerListRow, summaries []container.Summary) []containertypes.Container {
190+
func (c *ContainerController) appendContainerInfo(ctx context.Context, pageRows []containertypes.ContainerListRow, summaries []container.Summary) []containertypes.Container {
191+
dockerService := c.getDockerService(ctx)
190192
result := make([]containertypes.Container, 0, len(pageRows))
191193
for _, r := range pageRows {
192-
info, err := c.dockerService.GetContainerById(r.ID)
194+
info, err := dockerService.GetContainerById(r.ID)
193195
if err != nil {
194196
c.logger.Log(logger.Error, "Error inspecting container", r.ID)
195197
continue

api/internal/features/container/controller/list_images.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ func (c *ContainerController) ListImages(f fuego.ContextWithBody[ListImagesReque
2626
}
2727
}
2828

29+
dockerService := c.getDockerService(f.Request().Context())
2930
filterArgs := filters.NewArgs()
3031
if req.ContainerID != "" {
31-
_, err := c.dockerService.GetContainerById(req.ContainerID)
32+
_, err := dockerService.GetContainerById(req.ContainerID)
3233
if err != nil {
3334
return nil, fuego.HTTPError{
3435
Err: err,
@@ -45,7 +46,7 @@ func (c *ContainerController) ListImages(f fuego.ContextWithBody[ListImagesReque
4546
filterArgs.Add("reference", pattern)
4647
}
4748

48-
images := c.dockerService.ListAllImages(image.ListOptions{
49+
images := dockerService.ListAllImages(image.ListOptions{
4950
All: req.All,
5051
Filters: filterArgs,
5152
})

api/internal/features/container/controller/prune_build_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (c *ContainerController) PruneBuildCache(f fuego.ContextWithBody[PruneBuild
2222
Status: http.StatusBadRequest,
2323
}
2424
}
25-
err = c.dockerService.PruneBuildCache(types.BuildCachePruneOptions{
25+
err = c.getDockerService(f.Request().Context()).PruneBuildCache(types.BuildCachePruneOptions{
2626
All: req.All,
2727
})
2828
if err != nil {

api/internal/features/container/controller/prune_images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *ContainerController) PruneImages(f fuego.ContextWithBody[PruneImagesReq
3434
filterArgs.Add("dangling", "true")
3535
}
3636

37-
pruneReport, err := c.dockerService.PruneImages(filterArgs)
37+
pruneReport, err := c.getDockerService(f.Request().Context()).PruneImages(filterArgs)
3838
if err != nil {
3939
c.logger.Log(logger.Error, err.Error(), "")
4040
return nil, fuego.HTTPError{

api/internal/features/container/controller/remove_container.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
func (c *ContainerController) RemoveContainer(f fuego.ContextNoBody) (*shared_types.Response, error) {
1313
containerID := f.PathParam("container_id")
14+
ctx := f.Request().Context()
1415

15-
if resp, skipped := c.isProtectedContainer(containerID, "remove"); skipped {
16+
if resp, skipped := c.isProtectedContainer(ctx, containerID, "remove"); skipped {
1617
return resp, nil
1718
}
1819

19-
err := c.dockerService.RemoveContainer(containerID, container.RemoveOptions{Force: true})
20+
err := c.getDockerService(ctx).RemoveContainer(containerID, container.RemoveOptions{Force: true})
2021
if err != nil {
2122
c.logger.Log(logger.Error, err.Error(), "")
2223
return nil, fuego.HTTPError{

api/internal/features/container/controller/restart_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func (c *ContainerController) RestartContainer(f fuego.ContextNoBody) (*shared_types.Response, error) {
1313
containerID := f.PathParam("container_id")
1414

15-
err := c.dockerService.RestartContainer(containerID, container.StopOptions{})
15+
err := c.getDockerService(f.Request().Context()).RestartContainer(containerID, container.StopOptions{})
1616
if err != nil {
1717
c.logger.Log(logger.Error, err.Error(), "")
1818
return nil, fuego.HTTPError{

0 commit comments

Comments
 (0)