Created
February 26, 2024 09:29
-
-
Save gocs/140409b5dd476a901fa43e10a7f9801b to your computer and use it in GitHub Desktop.
sample embeded static pages handler
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 ui | |
import ( | |
"embed" | |
"fmt" | |
"io/fs" | |
"net/http" | |
) | |
//go:embed all:public | |
var assets embed.FS | |
// StaticHandler returns a http.Handler that serves the embedded files. | |
func StaticHandler() (http.HandlerFunc, error) { | |
assets, err := fs.Sub(assets, "public") | |
if err != nil { | |
return nil, fmt.Errorf("failed to load assets: %w", err) | |
} | |
fs := http.FileServer(http.FS(assets)) | |
return http.StripPrefix("/", fs).ServeHTTP, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment