Push command working
- commit
- 9e32d128f459e9681357c30acda78f8a40a04f9e
- author
- bnema · · 2023-10-26 16:53 UTC
- committer
- bnema · · 2023-10-26 16:01 UTC
- parent
- 9b776ef
- signature
- unsigned
5 files · +19 -13
M internal/cli/cmd/ping.go +1 -1
| @@ -36,7 +36,7 @@ func NewPingCommand(a *cli.App) *cobra.Command { | |||
| 36 | 36 | Payload: payload, | |
| 37 | 37 | } | |
| 38 | 38 | ||
| 39 | - | resp, err := handler.SendHTTPRequest(a, &reqPayload, "/ping") | |
| 39 | + | resp, err := handler.SendHTTPRequest(a, &reqPayload, "GET", "/ping") | |
| 40 | 40 | if err != nil { | |
| 41 | 41 | fmt.Println("Error sending HTTP request:", err) | |
| 42 | 42 | return | |
| @@ -36,7 +36,7 @@ func NewPingCommand(a *cli.App) *cobra.Command { | |||||
| 36 | Payload: payload, | 36 | Payload: payload, | ||
| 37 | } | 37 | } | ||
| 38 | 38 | ||||
| 39 | - | resp, err := handler.SendHTTPRequest(a, &reqPayload, "/ping") | 39 | + | resp, err := handler.SendHTTPRequest(a, &reqPayload, "GET", "/ping") |
| 40 | if err != nil { | 40 | if err != nil { | ||
| 41 | fmt.Println("Error sending HTTP request:", err) | 41 | fmt.Println("Error sending HTTP request:", err) | ||
| 42 | return | 42 | return | ||
M internal/cli/cmd/push.go +2 -2
| @@ -52,7 +52,7 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||
| 52 | 52 | } | |
| 53 | 53 | fmt.Println("Image exported successfully") | |
| 54 | 54 | // Create a RequestPayload and populate it | |
| 55 | - | payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App | |
| 55 | + | reqPayload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App | |
| 56 | 56 | Type: "push", | |
| 57 | 57 | Payload: common.PushPayload{ // <- Same | |
| 58 | 58 | Ports: port, | |
| @@ -63,7 +63,7 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||
| 63 | 63 | } | |
| 64 | 64 | fmt.Println("Sending image to backend") | |
| 65 | 65 | // Send the request to the backend | |
| 66 | - | resp, err := handler.SendHTTPRequest(a, &payload, "/push") | |
| 66 | + | resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push") | |
| 67 | 67 | if err != nil { | |
| 68 | 68 | fmt.Println("Error sending HTTP request:", err) | |
| 69 | 69 | return | |
| @@ -52,7 +52,7 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||||
| 52 | } | 52 | } | ||
| 53 | fmt.Println("Image exported successfully") | 53 | fmt.Println("Image exported successfully") | ||
| 54 | // Create a RequestPayload and populate it | 54 | // Create a RequestPayload and populate it | ||
| 55 | - | payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App | 55 | + | reqPayload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App |
| 56 | Type: "push", | 56 | Type: "push", | ||
| 57 | Payload: common.PushPayload{ // <- Same | 57 | Payload: common.PushPayload{ // <- Same | ||
| 58 | Ports: port, | 58 | Ports: port, | ||
| @@ -63,7 +63,7 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||||
| 63 | } | 63 | } | ||
| 64 | fmt.Println("Sending image to backend") | 64 | fmt.Println("Sending image to backend") | ||
| 65 | // Send the request to the backend | 65 | // Send the request to the backend | ||
| 66 | - | resp, err := handler.SendHTTPRequest(a, &payload, "/push") | 66 | + | resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push") |
| 67 | if err != nil { | 67 | if err != nil { | ||
| 68 | fmt.Println("Error sending HTTP request:", err) | 68 | fmt.Println("Error sending HTTP request:", err) | ||
| 69 | return | 69 | return | ||
M internal/cli/handler/http.go +4 -3
| @@ -39,21 +39,22 @@ type Response struct { | |||
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | 41 | // SendHTTPRequest sends the HTTP request | |
| 42 | - | func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, endpoint string) (*Response, error) { | |
| 42 | + | func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, method string, endpoint string) (*Response, error) { | |
| 43 | 43 | apiUrl := a.Config.Http.BackendURL + "/api" | |
| 44 | 44 | token := a.Config.General.Token | |
| 45 | 45 | ||
| 46 | + | fmt.Printf("Final url:", apiUrl+endpoint) | |
| 47 | + | ||
| 46 | 48 | // Prepare the entire RequestPayload, not just the inner Payload | |
| 47 | 49 | jsonPayload, err := json.Marshal(rp) | |
| 48 | 50 | if err != nil { | |
| 49 | 51 | return nil, fmt.Errorf("failed to marshal payload: %w", err) | |
| 50 | 52 | } | |
| 51 | 53 | // Create a new request | |
| 52 | - | req, err := CreateNewRequest("GET", apiUrl+endpoint, jsonPayload) | |
| 54 | + | req, err := CreateNewRequest(method, apiUrl+endpoint, jsonPayload) | |
| 53 | 55 | if err != nil { | |
| 54 | 56 | return nil, fmt.Errorf("failed to create new request: %w", err) | |
| 55 | 57 | } | |
| 56 | - | ||
| 57 | 58 | // Set the request headers | |
| 58 | 59 | SetRequestHeaders(req, token) | |
| 59 | 60 | ||
| @@ -39,21 +39,22 @@ type Response struct { | |||||
| 39 | } | 39 | } | ||
| 40 | 40 | ||||
| 41 | // SendHTTPRequest sends the HTTP request | 41 | // SendHTTPRequest sends the HTTP request | ||
| 42 | - | func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, endpoint string) (*Response, error) { | 42 | + | func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, method string, endpoint string) (*Response, error) { |
| 43 | apiUrl := a.Config.Http.BackendURL + "/api" | 43 | apiUrl := a.Config.Http.BackendURL + "/api" | ||
| 44 | token := a.Config.General.Token | 44 | token := a.Config.General.Token | ||
| 45 | 45 | ||||
| 46 | + | fmt.Printf("Final url:", apiUrl+endpoint) | |||
| 47 | + | ||||
| 46 | // Prepare the entire RequestPayload, not just the inner Payload | 48 | // Prepare the entire RequestPayload, not just the inner Payload | ||
| 47 | jsonPayload, err := json.Marshal(rp) | 49 | jsonPayload, err := json.Marshal(rp) | ||
| 48 | if err != nil { | 50 | if err != nil { | ||
| 49 | return nil, fmt.Errorf("failed to marshal payload: %w", err) | 51 | return nil, fmt.Errorf("failed to marshal payload: %w", err) | ||
| 50 | } | 52 | } | ||
| 51 | // Create a new request | 53 | // Create a new request | ||
| 52 | - | req, err := CreateNewRequest("GET", apiUrl+endpoint, jsonPayload) | 54 | + | req, err := CreateNewRequest(method, apiUrl+endpoint, jsonPayload) |
| 53 | if err != nil { | 55 | if err != nil { | ||
| 54 | return nil, fmt.Errorf("failed to create new request: %w", err) | 56 | return nil, fmt.Errorf("failed to create new request: %w", err) | ||
| 55 | } | 57 | } | ||
| 56 | - | ||||
| 57 | // Set the request headers | 58 | // Set the request headers | ||
| 58 | SetRequestHeaders(req, token) | 59 | SetRequestHeaders(req, token) | ||
| 59 | 60 | ||||
R internal/httpserve/handler/endpoints.go → internal/httpserve/handler/clientendpoins.go +9 -7 renamed, 90% similar
| @@ -61,7 +61,7 @@ func GetInfos(c echo.Context, a *server.App) error { | |||
| 61 | 61 | func PostPush(c echo.Context, a *server.App) error { | |
| 62 | 62 | body, _ := io.ReadAll(c.Request().Body) | |
| 63 | 63 | c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) // Reset the body | |
| 64 | - | ||
| 64 | + | fmt.Print(body) | |
| 65 | 65 | payload := new(common.RequestPayload) | |
| 66 | 66 | if err := c.Bind(payload); err != nil { | |
| 67 | 67 | fmt.Println("Bind Error:", err) | |
| @@ -101,15 +101,17 @@ func PostPush(c echo.Context, a *server.App) error { | |||
| 101 | 101 | return c.JSON(http.StatusBadRequest, "Image name cannot be empty") | |
| 102 | 102 | } | |
| 103 | 103 | ||
| 104 | - | // 1 - Import the image tar | |
| 105 | - | imageId, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar)) | |
| 104 | + | // 1 - Save the image tar in the storage | |
| 105 | + | imagePath, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar)) | |
| 106 | 106 | if err != nil { | |
| 107 | 107 | return c.JSON(http.StatusBadRequest, err.Error()) | |
| 108 | 108 | } | |
| 109 | - | // 2 - Create the container with traefik labels | |
| 110 | - | fmt.Println("Image ID:", imageId) | |
| 111 | - | // 3 - Start the container | |
| 112 | - | // 4 - Return : | |
| 109 | + | fmt.Printf("Saved in %s", imagePath) | |
| 110 | + | // 2 - Import the tar in docker | |
| 111 | + | ||
| 112 | + | // 3 - Create the container with traefik labels | |
| 113 | + | ||
| 114 | + | // 4 - Start the container | |
| 113 | 115 | ||
| 114 | 116 | return c.JSON(http.StatusOK, "OK") | |
| 115 | 117 | } | |
| @@ -61,7 +61,7 @@ func GetInfos(c echo.Context, a *server.App) error { | |||||
| 61 | func PostPush(c echo.Context, a *server.App) error { | 61 | func PostPush(c echo.Context, a *server.App) error { | ||
| 62 | body, _ := io.ReadAll(c.Request().Body) | 62 | body, _ := io.ReadAll(c.Request().Body) | ||
| 63 | c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) // Reset the body | 63 | c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) // Reset the body | ||
| 64 | - | 64 | + | fmt.Print(body) | |
| 65 | payload := new(common.RequestPayload) | 65 | payload := new(common.RequestPayload) | ||
| 66 | if err := c.Bind(payload); err != nil { | 66 | if err := c.Bind(payload); err != nil { | ||
| 67 | fmt.Println("Bind Error:", err) | 67 | fmt.Println("Bind Error:", err) | ||
| @@ -101,15 +101,17 @@ func PostPush(c echo.Context, a *server.App) error { | |||||
| 101 | return c.JSON(http.StatusBadRequest, "Image name cannot be empty") | 101 | return c.JSON(http.StatusBadRequest, "Image name cannot be empty") | ||
| 102 | } | 102 | } | ||
| 103 | 103 | ||||
| 104 | - | // 1 - Import the image tar | 104 | + | // 1 - Save the image tar in the storage |
| 105 | - | imageId, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar)) | 105 | + | imagePath, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar)) |
| 106 | if err != nil { | 106 | if err != nil { | ||
| 107 | return c.JSON(http.StatusBadRequest, err.Error()) | 107 | return c.JSON(http.StatusBadRequest, err.Error()) | ||
| 108 | } | 108 | } | ||
| 109 | - | // 2 - Create the container with traefik labels | 109 | + | fmt.Printf("Saved in %s", imagePath) |
| 110 | - | fmt.Println("Image ID:", imageId) | 110 | + | // 2 - Import the tar in docker |
| 111 | - | // 3 - Start the container | 111 | + | |
| 112 | - | // 4 - Return : | 112 | + | // 3 - Create the container with traefik labels |
| 113 | + | ||||
| 114 | + | // 4 - Start the container | |||
| 113 | 115 | ||||
| 114 | return c.JSON(http.StatusOK, "OK") | 116 | return c.JSON(http.StatusOK, "OK") | ||
| 115 | } | 117 | } | ||
M internal/httpserve/router.go +3 -0
| @@ -51,6 +51,9 @@ func bindAPIEndpoints(e *echo.Echo, a *server.App) { | |||
| 51 | 51 | apiGroup.GET("/ping", func(c echo.Context) error { | |
| 52 | 52 | return handler.GetInfos(c, a) | |
| 53 | 53 | }) | |
| 54 | + | apiGroup.POST("/push", func(c echo.Context) error { | |
| 55 | + | return handler.PostPush(c, a) | |
| 56 | + | }) | |
| 54 | 57 | } | |
| 55 | 58 | ||
| 56 | 59 | // bindStaticRoute bind static path | |
| @@ -51,6 +51,9 @@ func bindAPIEndpoints(e *echo.Echo, a *server.App) { | |||||
| 51 | apiGroup.GET("/ping", func(c echo.Context) error { | 51 | apiGroup.GET("/ping", func(c echo.Context) error { | ||
| 52 | return handler.GetInfos(c, a) | 52 | return handler.GetInfos(c, a) | ||
| 53 | }) | 53 | }) | ||
| 54 | + | apiGroup.POST("/push", func(c echo.Context) error { | |||
| 55 | + | return handler.PostPush(c, a) | |||
| 56 | + | }) | |||
| 54 | } | 57 | } | ||
| 55 | 58 | ||||
| 56 | // bindStaticRoute bind static path | 59 | // bindStaticRoute bind static path | ||