Move into api routers instead of main

This commit is contained in:
2022-02-16 16:27:48 +01:00
parent f35f277b16
commit c3946df1ff
16 changed files with 561 additions and 358 deletions

View File

@@ -0,0 +1,36 @@
package routers
import (
"github.com/docker/docker/api/types"
"github.com/gin-gonic/gin"
"net/http"
"serverctl/pkg/api/middleware"
"serverctl/pkg/infrastructure/dependencies"
)
func containersRouter(router *gin.Engine, d *dependencies.Dependencies) {
containers := router.Group("/containers", middleware.BasicAuthMiddleware(d.Logger, d.UsersService))
containers.GET("/", func(c *gin.Context) {
type container struct {
Name string `json:"name"`
}
var msg struct {
Containers []container `json:"containers"`
}
get, err := d.Cache.Get("docker-containers")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "could not get containers from container runtime"})
return
}
msg.Containers = []container{}
for _, cont := range get.([]types.Container) {
msg.Containers = append(msg.Containers, container{
Name: cont.Names[0],
})
}
c.JSON(http.StatusOK, msg)
})
}