Skip to content

Commit bd56901

Browse files
committed
use custom writer
1 parent 81e56da commit bd56901

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

cmd/dashboard/controller/controller.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,31 @@ func getUid(c *gin.Context) uint64 {
285285
return user.ID
286286
}
287287

288+
type ginCustomWriter struct {
289+
gin.ResponseWriter
290+
291+
customCode int
292+
}
293+
294+
func newCustomWriter(c *gin.Context, code int) *ginCustomWriter {
295+
return &ginCustomWriter{
296+
ResponseWriter: c.Writer,
297+
customCode: code,
298+
}
299+
}
300+
301+
func (w *ginCustomWriter) WriteHeader(code int) {
302+
w.ResponseWriter.WriteHeader(w.customCode)
303+
}
304+
305+
func fileWithCustomStatusCode(c *gin.Context, filepath string, customCode int) {
306+
http.ServeFile(newCustomWriter(c, customCode), c.Request, filepath)
307+
}
308+
288309
func fallbackToFrontend(frontendDist fs.FS) func(*gin.Context) {
289310
checkLocalFileOrFs := func(c *gin.Context, fs fs.FS, path string, customStatusCode int) bool {
290311
if _, err := os.Stat(path); err == nil {
291-
c.FileWithCustomStatusCode(path, customStatusCode)
312+
fileWithCustomStatusCode(c, path, customStatusCode)
292313
return true
293314
}
294315
f, err := fs.Open(path)
@@ -303,7 +324,7 @@ func fallbackToFrontend(frontendDist fs.FS) func(*gin.Context) {
303324
if fileStat.IsDir() {
304325
return false
305326
}
306-
http.ServeContentCustomStatusCode(c.Writer, c.Request, path, fileStat.ModTime(), f.(io.ReadSeeker), customStatusCode)
327+
http.ServeContent(newCustomWriter(c, customStatusCode), c.Request, path, fileStat.ModTime(), f.(io.ReadSeeker))
307328
return true
308329
}
309330
return func(c *gin.Context) {

0 commit comments

Comments
 (0)