Skip to content

Instantly share code, notes, and snippets.

@gocs
Created February 26, 2024 09:29
Show Gist options
  • Save gocs/140409b5dd476a901fa43e10a7f9801b to your computer and use it in GitHub Desktop.
Save gocs/140409b5dd476a901fa43e10a7f9801b to your computer and use it in GitHub Desktop.
sample embeded static pages handler
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