🚧 Serve Page Resource bytes

This commit is contained in:
Maxim Lebedev 2023-11-19 14:02:09 +06:00
parent 5c75e6f9e9
commit 860d56cfc5
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 21 additions and 3 deletions

24
main.go
View File

@ -5,10 +5,12 @@
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"log"
"net"
@ -113,8 +115,7 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
if s.DefaultLanguage != domain.LanguageUnd {
supported = append(
[]language.Tag{language.Make(s.DefaultLanguage.Code())},
supported...,
[]language.Tag{language.Make(s.DefaultLanguage.Code())}, supported...,
)
}
@ -163,7 +164,24 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
return
}
http.ServeContent(w, r, res.Name(), domain.ResourceModTime(res), res)
// TODO(toby3d) : ugly workaround, must be refactored
resFile, err := contentDir.Open(res.File.Path())
if err != nil {
http.Error(w, "cannot open: "+err.Error(), http.StatusInternalServerError)
return
}
defer resFile.Close()
resBytes, err := io.ReadAll(resFile)
if err != nil {
http.Error(w, "cannot read all: "+err.Error(), http.StatusInternalServerError)
return
}
defer resFile.Close()
http.ServeContent(w, r, res.Name(), domain.ResourceModTime(res), bytes.NewReader(resBytes))
return
}