Skip to content

Commit ff75695

Browse files
committed
Refactor to use http.ServeContent
1 parent b59e2a8 commit ff75695

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

nextrouter/nextrouter.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package nextrouter
22

33
import (
4+
"bytes"
45
"fmt"
56
"io/fs"
67
"net/http"
78
"path/filepath"
89
"strings"
10+
"time"
911

1012
"github.com/go-chi/chi/v5"
1113
)
@@ -39,7 +41,6 @@ func buildRouter(rtr chi.Router, fileSystem fs.FS, name string) {
3941
})
4042
} else {
4143
serveFile(rtr, fileSystem, name)
42-
4344
}
4445
}
4546
}
@@ -52,19 +53,14 @@ func serveFile(router chi.Router, fileSystem fs.FS, fileName string) {
5253
return
5354
}
5455

55-
bytes, err := fs.ReadFile(fileSystem, fileName)
56+
data, err := fs.ReadFile(fileSystem, fileName)
5657
if err != nil {
5758
// TODO(Bryan): Log here
5859
return
5960
}
6061

6162
handler := func(w http.ResponseWriter, r *http.Request) {
62-
_, err = w.Write(bytes)
63-
64-
if err != nil {
65-
// TODO(BRYAN): Log here
66-
http.Error(w, http.StatusText(500), 500)
67-
}
63+
http.ServeContent(w, r, fileName, time.Time{}, bytes.NewReader(data))
6864
}
6965

7066
fileNameWithoutExtension := removeFileExtension(fileName)

nextrouter/nextrouter_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package nextrouter_test
33
import (
44
"context"
55
"io"
6-
"io/fs"
76
"net/http"
87
"net/http/httptest"
98
"testing"

0 commit comments

Comments
 (0)