Exemplo de Implementação:
package web
import (
"embed"
"net/http"
"path"
)
var frontendFS embed.FS
func NewFileServer() http.Handler {
fs := http.FS(frontendFS)
staticHandler := http.FileServer(fs)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if path.Clean(r.URL.Path).StartsWith("/api/") {
return
}
if _, err := fs.Open(path.Clean(r.URL.Path)); err != nil {
http.ServeFile(w, r, path.Join("dist", "index.html"))
return
}
staticHandler.ServeHTTP(w, r)
})
}
// main.go (exemplo de uso)
func main() {
mux := http.NewServeMux()
mux.Handle("/", web.NewFileServer())
// ... outros handlers
http.ListenAndServe(":8080", mux)
}