Skip to content

Commit

Permalink
dev: serve kernel as whole file, not stream it
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Mar 9, 2024
1 parent 465de5b commit a81485b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions dev/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,33 @@ func main() {
gwutil.FileTransformer(watchfs.New(os.DirFS(dir)), httpfs.FileServer).ServeHTTP(w, r)
})))
mux.Handle(fmt.Sprintf("%s/wanix-kernel.gz", basePath), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/gzip")

file, err := os.Open(filepath.Join(dir, "local/bin/kernel"))
if err != nil {
fmt.Println(err)
http.Error(w, "File not found.", http.StatusNotFound)
return
}
defer file.Close()

gzipWriter := gzip.NewWriter(w)
var b bytes.Buffer
gzipWriter := gzip.NewWriter(&b)
defer gzipWriter.Close()

if _, err := io.Copy(gzipWriter, file); err != nil {
log.Println("copy:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := gzipWriter.Flush(); err != nil {
if err := gzipWriter.Close(); err != nil {
fmt.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

reader := bytes.NewReader(b.Bytes())
w.Header().Set("Content-Type", "application/gzip")
w.Header().Set("Content-Disposition", `attachment; filename="wanix-kernel.gz"`)
http.ServeContent(w, r, "wanix-kernel.gz", time.Now(), reader)
}))
mux.Handle(fmt.Sprintf("%s/wanix-bootloader.js", basePath), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/javascript")
Expand Down

0 comments on commit a81485b

Please sign in to comment.