Skip to content

Commit

Permalink
Handle file streams properly
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Nov 14, 2024
1 parent 2f4ef70 commit b35eaa8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ This is an http service with two routes:
```
export SHARED_SECRET=changeme
go build
nohup ./fabrictor &
nohup ./fabricator &
echo $! > pid
```

Expand Down
8 changes: 4 additions & 4 deletions internal/handlers/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ func TransformCsv(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
defer file.Close()

writer := csv.NewWriter(file)
defer writer.Flush()

firstRow := make([]string, 0, len(headers))
for header := range headers {
firstRow = append(firstRow, header)
Expand All @@ -64,6 +61,8 @@ func TransformCsv(w http.ResponseWriter, r *http.Request) {
return
}
}
writer.Flush()
file.Close()

files := []string{
target,
Expand Down Expand Up @@ -102,7 +101,6 @@ func TransformCsv(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error opening file: "+err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()

fileName := filepath.Base(filePath)
zipFile, err := zipWriter.Create(fileName)
Expand All @@ -116,7 +114,9 @@ func TransformCsv(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error writing to zip: "+err.Error(), http.StatusInternalServerError)
return
}
file.Close()
os.Remove(filePath)
w.(http.Flusher).Flush()
}

}
Expand Down

0 comments on commit b35eaa8

Please sign in to comment.