mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2024-04-21 12:32:08 +00:00
feature (plugin): add plugin hook to extend frontend code
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user