Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active May 10, 2023 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/556b7ba92708ba62312cda8e147b3b58 to your computer and use it in GitHub Desktop.
Save peterhellberg/556b7ba92708ba62312cda8e147b3b58 to your computer and use it in GitHub Desktop.
Embed and serve `static/*` on the root of your web server, using `embed`, `io/fs` and `net/http`
package main
import (
"embed"
"io/fs"
"net/http"
)
//go:embed static/*
var staticEmbed embed.FS
func main() {
staticFS := fs.FS(staticEmbed)
static, err := fs.Sub(staticFS, "static")
if err != nil {
panic(err)
}
hfs := http.FileServer(http.FS(static))
http.Handle("/", hfs)
http.ListenAndServe(":8080", nil)
}
@peterhellberg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment