feature (plugin): add plugin hook to extend frontend code

This commit is contained in:
MickaelK
2023-11-21 23:06:45 +11:00
parent 4f663560f2
commit 9323c73fa9
+32
View File
@@ -3,7 +3,10 @@ package common
import (
"github.com/gorilla/mux"
"io"
"io/fs"
"net/http"
"path/filepath"
"strings"
)
type Plugin struct {
@@ -55,6 +58,35 @@ func (this Get) HttpEndpoint() []func(*mux.Router, *App) error {
return http_endpoint
}
/*
* Override some urls with static content. The main use case for this is to enable
* plugins to change the frontend code and overwrite some core components
*/
func (this Register) Static(www fs.FS, chroot string) {
fs.WalkDir(www, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
} else if d.IsDir() {
return nil
}
this.HttpEndpoint(func(r *mux.Router, app *App) error {
r.PathPrefix("/" + strings.TrimPrefix(path, chroot)).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := www.Open(path)
if err != nil {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("plugin.go::static::err " + err.Error()))
return
}
w.Header().Set("Content-Type", GetMimeType(filepath.Ext(path)))
io.Copy(w, f)
f.Close()
})
return nil
})
return nil
})
}
/*
* Starter is the meat that let us connect to a wide variety of server like:
* - plg_starter_http which is the default that server the application under 8334