Start integrating Hugo in the new plugin
Former-commit-id: dcc6bd82b3d3a89637a1032aad1a25d0b1f80046 [formerly 8784cd37bf58e81cdbe1bcec00e6f16b58efc915] [formerly 9e25850e063ae0825de337d5d5f29cee601b9040 [formerly 8b1d36dfb9ebfa001ddeef98034bb5a73d135c94]] Former-commit-id: 04a38bea2d141093570d9289d0d0a056a136fe8a [formerly 5995538504889e698aa6cd35b7da40c38b5d5ddf] Former-commit-id: 8c81a0b060167e1a2983a99bc87b380838ac07dc
This commit is contained in:
@@ -49,7 +49,7 @@ type FileManager struct {
|
||||
Commands map[string][]string
|
||||
|
||||
// The plugins that have been plugged in.
|
||||
Plugins []*Plugin
|
||||
Plugins map[string]Plugin
|
||||
}
|
||||
|
||||
// Command is a command function.
|
||||
@@ -111,9 +111,13 @@ type Regexp struct {
|
||||
}
|
||||
|
||||
// Plugin is a File Manager plugin.
|
||||
type Plugin struct {
|
||||
type Plugin interface {
|
||||
// The JavaScript that will be injected into the main page.
|
||||
JavaScript string
|
||||
JavaScript() string
|
||||
// If the Plugin returns (0, nil), the executation of File Manager will procced as usual.
|
||||
// Otherwise it will stop.
|
||||
BeforeAPI(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error)
|
||||
AfterAPI(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error)
|
||||
}
|
||||
|
||||
// DefaultUser is used on New, when no 'base' user is provided.
|
||||
@@ -134,13 +138,13 @@ var DefaultUser = User{
|
||||
// exists, it will load the users from there. Otherwise, a new user
|
||||
// will be created using the 'base' variable. The 'base' User should
|
||||
// not have the Password field hashed.
|
||||
// TODO: should it ask for a baseURL on New????
|
||||
func New(database string, base User) (*FileManager, error) {
|
||||
// Creates a new File Manager instance with the Users
|
||||
// map and Assets box.
|
||||
m := &FileManager{
|
||||
Users: map[string]*User{},
|
||||
assets: rice.MustFindBox("./assets/dist"),
|
||||
Users: map[string]*User{},
|
||||
assets: rice.MustFindBox("./assets/dist"),
|
||||
Plugins: map[string]Plugin{},
|
||||
}
|
||||
|
||||
// Tries to open a database on the location provided. This
|
||||
@@ -240,13 +244,33 @@ func (m *FileManager) SetBaseURL(url string) {
|
||||
m.BaseURL = strings.TrimSuffix(url, "/")
|
||||
}
|
||||
|
||||
// RegisterPlugin registers a plugin to a File Manager instance and
|
||||
// loads its options from the database.
|
||||
func (m *FileManager) RegisterPlugin(name string, plugin Plugin) error {
|
||||
if _, ok := m.Plugins[name]; ok {
|
||||
return errors.New("Plugin already registred")
|
||||
}
|
||||
|
||||
err := m.db.Get("plugins", name, &plugin)
|
||||
if err != nil && err == storm.ErrNotFound {
|
||||
err = m.db.Set("plugins", name, plugin)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Plugins[name] = plugin
|
||||
return nil
|
||||
}
|
||||
|
||||
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
||||
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// TODO: Handle errors here and make it compatible with http.Handler
|
||||
code, err := serveHTTP(&requestContext{
|
||||
fm: m,
|
||||
us: nil,
|
||||
fi: nil,
|
||||
code, err := serveHTTP(&RequestContext{
|
||||
FM: m,
|
||||
User: nil,
|
||||
FI: nil,
|
||||
}, w, r)
|
||||
|
||||
if code != 0 && err != nil {
|
||||
|
||||
Reference in New Issue
Block a user