fix: increase read & write server timeouts
- commit
- b4853100dac6e9615904543f5ff5f6d30eab5d25
- author
- bnema · · 2024-04-21 01:50 UTC
- parent
- dac92ca
- signature
- unsigned
2 files · +19 -9
M internal/cli/handler/startserver.go +3 -0
| @@ -6,6 +6,7 @@ import ( | |||
| 6 | 6 | "os" | |
| 7 | 7 | "os/signal" | |
| 8 | 8 | "syscall" | |
| 9 | + | "time" | |
| 9 | 10 | ||
| 10 | 11 | "github.com/bnema/gordon/internal/httpserve" | |
| 11 | 12 | "github.com/bnema/gordon/internal/server" | |
| @@ -39,6 +40,8 @@ func StartServer(a *server.App, port string) error { | |||
| 39 | 40 | }() | |
| 40 | 41 | ||
| 41 | 42 | e := echo.New() | |
| 43 | + | e.Server.ReadTimeout = 10 * time.Minute | |
| 44 | + | e.Server.WriteTimeout = 10 * time.Minute | |
| 42 | 45 | e.HideBanner = true | |
| 43 | 46 | e.HidePort = true | |
| 44 | 47 | e = httpserve.RegisterRoutes(e, a) | |
| @@ -6,6 +6,7 @@ import ( | |||||
| 6 | "os" | 6 | "os" | ||
| 7 | "os/signal" | 7 | "os/signal" | ||
| 8 | "syscall" | 8 | "syscall" | ||
| 9 | + | "time" | |||
| 9 | 10 | ||||
| 10 | "github.com/bnema/gordon/internal/httpserve" | 11 | "github.com/bnema/gordon/internal/httpserve" | ||
| 11 | "github.com/bnema/gordon/internal/server" | 12 | "github.com/bnema/gordon/internal/server" | ||
| @@ -39,6 +40,8 @@ func StartServer(a *server.App, port string) error { | |||||
| 39 | }() | 40 | }() | ||
| 40 | 41 | ||||
| 41 | e := echo.New() | 42 | e := echo.New() | ||
| 43 | + | e.Server.ReadTimeout = 10 * time.Minute | |||
| 44 | + | e.Server.WriteTimeout = 10 * time.Minute | |||
| 42 | e.HideBanner = true | 45 | e.HideBanner = true | ||
| 43 | e.HidePort = true | 46 | e.HidePort = true | ||
| 44 | e = httpserve.RegisterRoutes(e, a) | 47 | e = httpserve.RegisterRoutes(e, a) | ||
M internal/httpserve/handler/uploadcontainerimage.go +16 -9
| @@ -38,19 +38,26 @@ func UploadImageGETHandler(c echo.Context, a *server.App) error { | |||
| 38 | 38 | func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | |
| 39 | 39 | // Set upload size limit | |
| 40 | 40 | c.Request().Body = http.MaxBytesReader(c.Response(), c.Request().Body, MaxUploadSize) | |
| 41 | - | if err := c.Request().ParseMultipartForm(MaxUploadSize); err != nil { | |
| 42 | - | return c.String(http.StatusRequestEntityTooLarge, "File size exceeded") | |
| 41 | + | ||
| 42 | + | // Parse the multipart form | |
| 43 | + | form, err := c.MultipartForm() | |
| 44 | + | if err != nil { | |
| 45 | + | return sendError(c, err) | |
| 43 | 46 | } | |
| 44 | 47 | ||
| 45 | 48 | // Get the file from the form | |
| 46 | - | file, header, err := c.Request().FormFile("file") | |
| 49 | + | files := form.File["file"] | |
| 50 | + | if len(files) == 0 { | |
| 51 | + | return c.String(http.StatusBadRequest, "No file uploaded") | |
| 52 | + | } | |
| 53 | + | file := files[0] | |
| 54 | + | ||
| 55 | + | // Open the file | |
| 56 | + | src, err := file.Open() | |
| 47 | 57 | if err != nil { | |
| 48 | 58 | return sendError(c, err) | |
| 49 | 59 | } | |
| 50 | - | defer file.Close() | |
| 51 | - | ||
| 52 | - | // Get the filename | |
| 53 | - | filename := header.Filename | |
| 60 | + | defer src.Close() | |
| 54 | 61 | ||
| 55 | 62 | // Create a temporary file to store the uploaded chunks | |
| 56 | 63 | tempFile, err := os.CreateTemp("", "upload-*") | |
| @@ -60,7 +67,7 @@ func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | |||
| 60 | 67 | defer tempFile.Close() | |
| 61 | 68 | ||
| 62 | 69 | // Copy the file chunks to the temporary file | |
| 63 | - | _, err = io.Copy(tempFile, file) | |
| 70 | + | _, err = io.Copy(tempFile, src) | |
| 64 | 71 | if err != nil { | |
| 65 | 72 | return sendError(c, err) | |
| 66 | 73 | } | |
| @@ -72,7 +79,7 @@ func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | |||
| 72 | 79 | } | |
| 73 | 80 | ||
| 74 | 81 | // Save the image to the storage directory | |
| 75 | - | saveInPath, err := store.SaveImageToStorage(&a.Config, filename, tempFile) | |
| 82 | + | saveInPath, err := store.SaveImageToStorage(&a.Config, file.Filename, tempFile) | |
| 76 | 83 | if err != nil { | |
| 77 | 84 | return sendError(c, err) | |
| 78 | 85 | } | |
| @@ -38,19 +38,26 @@ func UploadImageGETHandler(c echo.Context, a *server.App) error { | |||||
| 38 | func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | 38 | func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | ||
| 39 | // Set upload size limit | 39 | // Set upload size limit | ||
| 40 | c.Request().Body = http.MaxBytesReader(c.Response(), c.Request().Body, MaxUploadSize) | 40 | c.Request().Body = http.MaxBytesReader(c.Response(), c.Request().Body, MaxUploadSize) | ||
| 41 | - | if err := c.Request().ParseMultipartForm(MaxUploadSize); err != nil { | 41 | + | |
| 42 | - | return c.String(http.StatusRequestEntityTooLarge, "File size exceeded") | 42 | + | // Parse the multipart form |
| 43 | + | form, err := c.MultipartForm() | |||
| 44 | + | if err != nil { | |||
| 45 | + | return sendError(c, err) | |||
| 43 | } | 46 | } | ||
| 44 | 47 | ||||
| 45 | // Get the file from the form | 48 | // Get the file from the form | ||
| 46 | - | file, header, err := c.Request().FormFile("file") | 49 | + | files := form.File["file"] |
| 50 | + | if len(files) == 0 { | |||
| 51 | + | return c.String(http.StatusBadRequest, "No file uploaded") | |||
| 52 | + | } | |||
| 53 | + | file := files[0] | |||
| 54 | + | ||||
| 55 | + | // Open the file | |||
| 56 | + | src, err := file.Open() | |||
| 47 | if err != nil { | 57 | if err != nil { | ||
| 48 | return sendError(c, err) | 58 | return sendError(c, err) | ||
| 49 | } | 59 | } | ||
| 50 | - | defer file.Close() | 60 | + | defer src.Close() |
| 51 | - | ||||
| 52 | - | // Get the filename | |||
| 53 | - | filename := header.Filename | |||
| 54 | 61 | ||||
| 55 | // Create a temporary file to store the uploaded chunks | 62 | // Create a temporary file to store the uploaded chunks | ||
| 56 | tempFile, err := os.CreateTemp("", "upload-*") | 63 | tempFile, err := os.CreateTemp("", "upload-*") | ||
| @@ -60,7 +67,7 @@ func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | |||||
| 60 | defer tempFile.Close() | 67 | defer tempFile.Close() | ||
| 61 | 68 | ||||
| 62 | // Copy the file chunks to the temporary file | 69 | // Copy the file chunks to the temporary file | ||
| 63 | - | _, err = io.Copy(tempFile, file) | 70 | + | _, err = io.Copy(tempFile, src) |
| 64 | if err != nil { | 71 | if err != nil { | ||
| 65 | return sendError(c, err) | 72 | return sendError(c, err) | ||
| 66 | } | 73 | } | ||
| @@ -72,7 +79,7 @@ func UploadImagePOSTHandler(c echo.Context, a *server.App) error { | |||||
| 72 | } | 79 | } | ||
| 73 | 80 | ||||
| 74 | // Save the image to the storage directory | 81 | // Save the image to the storage directory | ||
| 75 | - | saveInPath, err := store.SaveImageToStorage(&a.Config, filename, tempFile) | 82 | + | saveInPath, err := store.SaveImageToStorage(&a.Config, file.Filename, tempFile) |
| 76 | if err != nil { | 83 | if err != nil { | ||
| 77 | return sendError(c, err) | 84 | return sendError(c, err) | ||
| 78 | } | 85 | } | ||