Skip to content

Commit

Permalink
Fix file download and upload CORS; Increase allowed request time; Add…
Browse files Browse the repository at this point in the history
… logs for the upload and download
  • Loading branch information
ValentaTomas committed Oct 20, 2023
1 parent eb34d9a commit 002e9a2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/envd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/drael/GOnetstat v0.0.0-20201004132414-bf4a88b0bdab
github.com/ethereum/go-ethereum v1.12.2
github.com/fsnotify/fsnotify v1.6.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/rs/xid v1.5.0
Expand All @@ -15,6 +16,7 @@ require (

require (
github.com/deckarep/golang-set/v2 v2.3.1 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions packages/envd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ github.com/drael/GOnetstat v0.0.0-20201004132414-bf4a88b0bdab h1:e3ZU5RneT1oa8oI
github.com/drael/GOnetstat v0.0.0-20201004132414-bf4a88b0bdab/go.mod h1:QtldKtugUV5zmFEu34yrkdalP1qD52QaOZgZ+V6N5DI=
github.com/ethereum/go-ethereum v1.12.2 h1:eGHJ4ij7oyVqUQn48LBz3B7pvQ8sV0wGJiIE6gDq/6Y=
github.com/ethereum/go-ethereum v1.12.2/go.mod h1:1cRAEV+rp/xX0zraSCBnu9Py3HQ+geRMj3HdR+k0wfI=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down
8 changes: 8 additions & 0 deletions packages/envd/internal/file/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
const FileHeaderByteSize = 512

func Download(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request) {
logger.Info(
"Starting file download",
)

filePath := r.URL.Query().Get("path")
if filePath == "" {
http.Error(w, "File path is required", http.StatusBadRequest)
Expand All @@ -31,6 +35,8 @@ func Download(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request)
return
}

logger.Info("File opened successfully")

defer func() {
closeErr := file.Close()
if closeErr != nil {
Expand Down Expand Up @@ -72,4 +78,6 @@ func Download(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request)

return
}

logger.Info("File download complete")
}
16 changes: 15 additions & 1 deletion packages/envd/internal/file/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
const MaxUploadSize = 100 * 1024 * 1024 // 100MB

func Upload(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request) {
logger.Info(
"Starting file upload",
)

r.Body = http.MaxBytesReader(w, r.Body, MaxUploadSize)

if err := r.ParseMultipartForm(MaxUploadSize); err != nil {
Expand All @@ -24,6 +28,8 @@ func Upload(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request) {
return
}

logger.Info("Multipart form parsed successfully")

// The argument to FormFile must match the name attribute
// of the file input on the frontend
file, fileHeader, err := r.FormFile("file")
Expand All @@ -34,6 +40,8 @@ func Upload(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request) {
return
}

logger.Info("File retrieved successfully")

defer func() {
closeErr := file.Close()
if closeErr != nil {
Expand Down Expand Up @@ -68,6 +76,12 @@ func Upload(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request) {
return
}

logger.Infow("File created successfully",
"path", newFilePath,
)

logger.Infow("File created successfully")

defer func() {
closeErr := dst.Close()
if closeErr != nil {
Expand All @@ -89,5 +103,5 @@ func Upload(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request) {
return
}

fmt.Fprintf(w, "Upload successful")
logger.Info("Upload complete")
}
7 changes: 4 additions & 3 deletions packages/envd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"go.uber.org/zap"

Expand Down Expand Up @@ -161,10 +162,10 @@ func main() {
router.HandleFunc("/file", fileHandler)

server := &http.Server{
ReadTimeout: 40 * time.Second,
WriteTimeout: 40 * time.Second,
ReadTimeout: 300 * time.Second,
WriteTimeout: 300 * time.Second,
Addr: fmt.Sprintf("0.0.0.0:%d", serverPort),
Handler: router,
Handler: handlers.CORS(handlers.AllowedMethods([]string{"GET", "POST", "PUT"}), handlers.AllowedOrigins([]string{"*"}))(router),
}

logger.Infow("Starting server",
Expand Down

0 comments on commit 002e9a2

Please sign in to comment.