Last active
May 10, 2023 07:35
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://go.dev/play/p/CZ4HUBSoAzY