35 lines
622 B
Go
35 lines
622 B
Go
package web
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed assets
|
|
var assetsDir embed.FS
|
|
|
|
func Assets() http.FileSystem {
|
|
f, _ := fs.Sub(assetsDir, "assets")
|
|
return http.FS(f)
|
|
}
|
|
|
|
func ServeAsset(path, mime, attachement string) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if mime != "" {
|
|
w.Header().Add("Content-Type", mime)
|
|
}
|
|
|
|
if attachement != "" {
|
|
w.Header().Add("Content-Disposition", "attachment; filename=\""+attachement+"\"")
|
|
}
|
|
|
|
data, err := assetsDir.ReadFile("assets/" + path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
w.Write(data)
|
|
}
|
|
}
|