1package handler
2
3import (
4 "fmt"
5 "net/http"
6
7 "github.com/bnema/gordon/internal/server"
8 "github.com/bnema/gordon/internal/templating/cmdparams"
9 "github.com/bnema/gordon/internal/templating/render"
10 "github.com/bnema/gordon/pkg/docker"
11 "github.com/bnema/gordon/pkg/sanitize"
12 "github.com/labstack/echo/v4"
13)
14
15// FromShortIDToImageID converts a short image ID to a full image ID
16func FromShortIDToImageID(ShortID string) (string, error) {
17 imageID, exists := safelyInteractWithIDMap(Fetch, ShortID)
18 if exists {
19 return imageID, nil
20 }
21 return "", fmt.Errorf("image ID not found")
22}
23
24// CreateContainerRoute is the route for creating a new container
25func CreateContainerGET(c echo.Context, a *server.App) error {
26 // Retreive the ShortID of the image from the URL
27 ShortID := c.Param("ID")
28
29 // Convert the ShortID to a full image ID
30 imageID, err := FromShortIDToImageID(ShortID)
31 if err != nil {
32 return sendError(c, err)
33 }
34
35 // Get the image info
36 imageInfo, err := docker.GetImageInfo(imageID)
37 if err != nil {
38 return sendError(c, err)
39 }
40
41 // Extract image name
42 var imageName string
43 if len(imageInfo.RepoTags) > 0 {
44 imageName = imageInfo.RepoTags[0]
45 }
46
47 data := map[string]interface{}{
48 "Title": "Create a new container",
49 "ShortID": ShortID,
50 "ImageID": imageID,
51 "ImageName": imageName,
52 }
53
54 rendererData, err := render.GetHTMLRenderer("html/fragments", "createcontainer.gohtml", a.TemplateFS, a)
55
56 if err != nil {
57 return sendError(c, err)
58 }
59
60 renderedHTML, err := rendererData.Render(data, a)
61
62 if err != nil {
63 return sendError(c, err)
64 }
65
66 return c.HTML(200, renderedHTML)
67
68}
69
70// CreateContainerPOST handles the create container form submission
71func CreateContainerPOST(c echo.Context, a *server.App) error {
72 // Retreive the ShortID of the image from the URL
73 ShortID := c.Param("ID")
74
75 // Convert the ShortID to a full image ID
76 imageID, err := FromShortIDToImageID(ShortID)
77 if err != nil {
78 return sendError(c, err)
79 }
80
81 // Get the image info
82 _, err = docker.GetImageInfo(imageID)
83 if err != nil {
84 return sendError(c, err)
85 }
86
87 // Retrieve all form parameters
88 formParams, err := c.FormParams()
89 if err != nil {
90 return sendError(c, err)
91 }
92
93 // Initialize a map to store sanitized values
94 sanitizedInputs := make(map[string]string)
95
96 // Iterate over all form parameters and sanitize them
97 for k, v := range formParams {
98 if len(v) > 0 {
99 sanitized, err := sanitize.SanitizeHTML(v[0])
100 if err != nil {
101 return sendError(c, err)
102 }
103 sanitizedInputs[k] = sanitized
104 }
105 }
106
107 cmdParams, err := cmdparams.FromInputsToCmdParams(sanitizedInputs, a)
108 if err != nil {
109 return sendError(c, err)
110 }
111
112 // Create the container
113 _, err = docker.CreateContainer(cmdParams)
114 if err != nil {
115 return sendError(c, err)
116 }
117
118 return c.HTML(http.StatusOK, ActionSuccess(a))
119}