2016-05-30 16:08:09 +02:00
package iris
import (
2016-06-14 07:45:40 +02:00
"sync"
2016-09-07 06:36:23 +02:00
"log"
2016-06-14 07:45:40 +02:00
2016-09-07 06:36:23 +02:00
"github.com/kataras/go-errors"
2016-09-01 05:34:55 +02:00
"github.com/kataras/go-fs"
2016-05-30 16:08:09 +02:00
)
2016-06-14 07:45:40 +02:00
var (
// errPluginAlreadyExists returns an error with message: 'Cannot activate the same plugin again, plugin '+plugin name[+plugin description]' is already exists'
errPluginAlreadyExists = errors . New ( "Cannot use the same plugin again, '%s[%s]' is already exists" )
// errPluginActivate returns an error with message: 'While trying to activate plugin '+plugin name'. Trace: +specific error'
errPluginActivate = errors . New ( "While trying to activate plugin '%s'. Trace: %s" )
// errPluginRemoveNoPlugins returns an error with message: 'No plugins are registed yet, you cannot remove a plugin from an empty list!'
errPluginRemoveNoPlugins = errors . New ( "No plugins are registed yet, you cannot remove a plugin from an empty list!" )
// errPluginRemoveEmptyName returns an error with message: 'Plugin with an empty name cannot be removed'
errPluginRemoveEmptyName = errors . New ( "Plugin with an empty name cannot be removed" )
// errPluginRemoveNotFound returns an error with message: 'Cannot remove a plugin which doesn't exists'
errPluginRemoveNotFound = errors . New ( "Cannot remove a plugin which doesn't exists" )
)
2016-05-30 16:08:09 +02:00
type (
2016-06-14 07:45:40 +02:00
// Plugin just an empty base for plugins
// A Plugin can be added with: .Add(PreListenFunc(func(*Framework))) and so on... or
// .Add(myPlugin{},myPlugin2{}) which myPlugin is a struct with any of the methods below or
// .PostListen(func(*Framework)) and so on...
Plugin interface {
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
// pluginGetName implements the GetName() string method
pluginGetName interface {
2016-05-30 16:08:09 +02:00
// GetName has to returns the name of the plugin, a name is unique
// name has to be not dependent from other methods of the plugin,
// because it is being called even before the Activate
GetName ( ) string
}
2016-06-14 07:45:40 +02:00
// pluginGetDescription implements the GetDescription() string method
pluginGetDescription interface {
2016-05-30 16:08:09 +02:00
// GetDescription has to returns the description of what the plugins is used for
GetDescription ( ) string
}
2016-06-14 07:45:40 +02:00
// pluginActivate implements the Activate(pluginContainer) error method
pluginActivate interface {
2016-05-30 16:08:09 +02:00
// Activate called BEFORE the plugin being added to the plugins list,
// if Activate returns none nil error then the plugin is not being added to the list
// it is being called only one time
//
// PluginContainer parameter used to add other plugins if that's necessary by the plugin
2016-06-14 07:45:40 +02:00
Activate ( PluginContainer ) error
2016-05-30 16:08:09 +02:00
}
2016-07-07 23:59:00 +02:00
// pluginPreLookup implements the PreRoute(Route) method
pluginPreLookup interface {
// PreLookup called before register a route
PreLookup ( Route )
}
// PreLookupFunc implements the simple function listener for the PreLookup(Route)
PreLookupFunc func ( Route )
2016-06-14 07:45:40 +02:00
// pluginPreListen implements the PreListen(*Framework) method
pluginPreListen interface {
2016-05-30 16:08:09 +02:00
// PreListen it's being called only one time, BEFORE the Server is started (if .Listen called)
// is used to do work at the time all other things are ready to go
// parameter is the station
2016-06-14 07:45:40 +02:00
PreListen ( * Framework )
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
// PreListenFunc implements the simple function listener for the PreListen(*Framework)
PreListenFunc func ( * Framework )
// pluginPostListen implements the PostListen(*Framework) method
pluginPostListen interface {
2016-05-30 16:08:09 +02:00
// PostListen it's being called only one time, AFTER the Server is started (if .Listen called)
// parameter is the station
2016-06-14 07:45:40 +02:00
PostListen ( * Framework )
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
// PostListenFunc implements the simple function listener for the PostListen(*Framework)
PostListenFunc func ( * Framework )
// pluginPreClose implements the PreClose(*Framework) method
pluginPreClose interface {
2016-05-30 16:08:09 +02:00
// PreClose it's being called only one time, BEFORE the Iris .Close method
// any plugin cleanup/clear memory happens here
//
// The plugin is deactivated after this state
2016-06-14 07:45:40 +02:00
PreClose ( * Framework )
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
// PreCloseFunc implements the simple function listener for the PreClose(*Framework)
PreCloseFunc func ( * Framework )
2016-05-30 16:08:09 +02:00
2016-06-14 07:45:40 +02:00
// pluginPreDownload It's for the future, not being used, I need to create
2016-05-30 16:08:09 +02:00
// and return an ActivatedPlugin type which will have it's methods, and pass it on .Activate
// but now we return the whole pluginContainer, which I can't determinate which plugin tries to
// download something, so we will leave it here for the future.
2016-06-14 07:45:40 +02:00
pluginPreDownload interface {
2016-05-30 16:08:09 +02:00
// PreDownload it's being called every time a plugin tries to download something
//
// first parameter is the plugin
// second parameter is the download url
// must return a boolean, if false then the plugin is not permmited to download this file
2016-06-14 07:45:40 +02:00
PreDownload ( plugin Plugin , downloadURL string ) // bool
}
// PreDownloadFunc implements the simple function listener for the PreDownload(plugin,string)
PreDownloadFunc func ( Plugin , string )
// PluginContainer is the interface which the pluginContainer should implements
PluginContainer interface {
Add ( ... Plugin ) error
Remove ( string ) error
GetName ( Plugin ) string
GetDescription ( Plugin ) string
GetByName ( string ) Plugin
Printf ( string , ... interface { } )
2016-07-07 23:59:00 +02:00
PreLookup ( PreLookupFunc )
DoPreLookup ( Route )
2016-06-14 07:45:40 +02:00
PreListen ( PreListenFunc )
DoPreListen ( * Framework )
DoPreListenParallel ( * Framework )
PostListen ( PostListenFunc )
DoPostListen ( * Framework )
PreClose ( PreCloseFunc )
DoPreClose ( * Framework )
PreDownload ( PreDownloadFunc )
DoPreDownload ( Plugin , string )
//
GetAll ( ) [ ] Plugin
2016-05-30 16:08:09 +02:00
// GetDownloader is the only one module that is used and fire listeners at the same time in this file
2016-06-14 07:45:40 +02:00
GetDownloader ( ) PluginDownloadManager
2016-09-05 02:56:28 +02:00
} //Note: custom event callbacks, never used internaly by Iris, but if you need them use this: github.com/kataras/go-events
2016-06-14 07:45:40 +02:00
// PluginDownloadManager is the interface which the DownloadManager should implements
PluginDownloadManager interface {
DirectoryExists ( string ) bool
DownloadZip ( string , string ) ( string , error )
Unzip ( string , string ) ( string , error )
Remove ( string ) error
2016-05-30 16:08:09 +02:00
// install is just the flow of: downloadZip -> unzip -> removeFile(zippedFile)
// accepts 2 parameters
//
// first parameter is the remote url file zip
// second parameter is the target directory
// returns a string(installedDirectory) and an error
//
// (string) installedDirectory is the directory which the zip file had, this is the real installation path, you don't need to know what it's because these things maybe change to the future let's keep it to return the correct path.
// the installedDirectory is not empty when the installation is succed, the targetDirectory is not already exists and no error happens
// the installedDirectory is empty when the installation is already done by previous time or an error happens
Install ( remoteFileZip string , targetDirectory string ) ( string , error )
}
2016-06-14 07:45:40 +02:00
// pluginDownloadManager is just a struch which exports the util's downloadZip, directoryExists, unzip methods, used by the plugins via the pluginContainer
pluginDownloadManager struct {
2016-05-30 16:08:09 +02:00
}
)
2016-06-14 07:45:40 +02:00
// convert the functions to plugin
2016-05-30 16:08:09 +02:00
2016-07-07 23:59:00 +02:00
// PreLookup called before register a route
func ( fn PreLookupFunc ) PreLookup ( r Route ) {
fn ( r )
}
2016-06-03 04:11:50 +02:00
// PreListen it's being called only one time, BEFORE the Server is started (if .Listen called)
// is used to do work at the time all other things are ready to go
// parameter is the station
2016-06-14 07:45:40 +02:00
func ( fn PreListenFunc ) PreListen ( station * Framework ) {
2016-05-30 16:08:09 +02:00
fn ( station )
}
2016-06-03 04:11:50 +02:00
// PostListen it's being called only one time, AFTER the Server is started (if .Listen called)
// parameter is the station
2016-06-14 07:45:40 +02:00
func ( fn PostListenFunc ) PostListen ( station * Framework ) {
2016-05-30 16:08:09 +02:00
fn ( station )
}
2016-06-03 04:11:50 +02:00
// PreClose it's being called only one time, BEFORE the Iris .Close method
// any plugin cleanup/clear memory happens here
//
// The plugin is deactivated after this state
2016-06-14 07:45:40 +02:00
func ( fn PreCloseFunc ) PreClose ( station * Framework ) {
2016-05-30 16:08:09 +02:00
fn ( station )
}
2016-06-03 04:11:50 +02:00
// PreDownload it's being called every time a plugin tries to download something
//
// first parameter is the plugin
// second parameter is the download url
// must return a boolean, if false then the plugin is not permmited to download this file
2016-06-14 07:45:40 +02:00
func ( fn PreDownloadFunc ) PreDownload ( pl Plugin , downloadURL string ) {
2016-05-30 16:08:09 +02:00
fn ( pl , downloadURL )
}
//
2016-06-14 07:45:40 +02:00
var _ PluginDownloadManager = & pluginDownloadManager { }
var _ PluginContainer = & pluginContainer { }
2016-05-30 16:08:09 +02:00
// DirectoryExists returns true if a given local directory exists
2016-06-14 07:45:40 +02:00
func ( d * pluginDownloadManager ) DirectoryExists ( dir string ) bool {
2016-09-01 05:34:55 +02:00
return fs . DirectoryExists ( dir )
2016-05-30 16:08:09 +02:00
}
// DownloadZip downlodas a zip to the given local path location
2016-06-14 07:45:40 +02:00
func ( d * pluginDownloadManager ) DownloadZip ( zipURL string , targetDir string ) ( string , error ) {
2016-09-02 05:05:44 +02:00
return fs . DownloadZip ( zipURL , targetDir , true )
2016-05-30 16:08:09 +02:00
}
// Unzip unzips a zip to the given local path location
2016-06-14 07:45:40 +02:00
func ( d * pluginDownloadManager ) Unzip ( archive string , target string ) ( string , error ) {
2016-09-02 05:05:44 +02:00
return fs . DownloadZip ( archive , target , true )
2016-05-30 16:08:09 +02:00
}
// Remove deletes/removes/rm a file
2016-06-14 07:45:40 +02:00
func ( d * pluginDownloadManager ) Remove ( filePath string ) error {
2016-09-01 05:34:55 +02:00
return fs . RemoveFile ( filePath )
2016-05-30 16:08:09 +02:00
}
// Install is just the flow of the: DownloadZip->Unzip->Remove the zip
2016-06-14 07:45:40 +02:00
func ( d * pluginDownloadManager ) Install ( remoteFileZip string , targetDirectory string ) ( string , error ) {
2016-09-02 05:05:44 +02:00
return fs . Install ( remoteFileZip , targetDirectory , true )
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
// pluginContainer is the base container of all Iris, registed plugins
type pluginContainer struct {
activatedPlugins [ ] Plugin
customEvents map [ string ] [ ] func ( )
downloader * pluginDownloadManager
2016-09-07 06:36:23 +02:00
logger * log . Logger
2016-07-05 13:37:10 +02:00
mu sync . Mutex
2016-05-30 16:08:09 +02:00
}
2016-09-05 02:56:28 +02:00
// newPluginContainer receives a logger and returns a new PluginContainer
2016-09-07 06:36:23 +02:00
func newPluginContainer ( l * log . Logger ) PluginContainer {
2016-09-05 02:56:28 +02:00
return & pluginContainer { logger : l }
}
2016-05-30 16:08:09 +02:00
// Add activates the plugins and if succeed then adds it to the activated plugins list
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) Add ( plugins ... Plugin ) error {
for _ , plugin := range plugins {
2016-07-05 13:37:10 +02:00
2016-06-14 07:45:40 +02:00
if p . activatedPlugins == nil {
p . activatedPlugins = make ( [ ] Plugin , 0 )
}
2016-05-30 16:08:09 +02:00
2016-06-14 07:45:40 +02:00
// Check if it's a plugin first, has Activate GetName
2016-05-30 16:08:09 +02:00
2016-06-14 07:45:40 +02:00
// Check if the plugin already exists
pName := p . GetName ( plugin )
if pName != "" && p . GetByName ( pName ) != nil {
return errPluginAlreadyExists . Format ( pName , p . GetDescription ( plugin ) )
}
// Activate the plugin, if no error then add it to the plugins
if pluginObj , ok := plugin . ( pluginActivate ) ; ok {
2016-08-17 19:16:23 +02:00
tempPluginContainer := * p // contains the mutex but we' re safe here.
2016-07-05 13:37:10 +02:00
err := pluginObj . Activate ( & tempPluginContainer )
2016-06-14 07:45:40 +02:00
if err != nil {
return errPluginActivate . Format ( pName , err . Error ( ) )
}
2016-07-05 13:37:10 +02:00
tempActivatedPluginsLen := len ( tempPluginContainer . activatedPlugins )
if tempActivatedPluginsLen != len ( p . activatedPlugins ) + tempActivatedPluginsLen + 1 { // see test: plugin_test.go TestPluginActivate && TestPluginActivationError
p . activatedPlugins = tempPluginContainer . activatedPlugins
}
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
// All ok, add it to the plugins list
p . activatedPlugins = append ( p . activatedPlugins , plugin )
}
2016-05-30 16:08:09 +02:00
return nil
}
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) Reset ( ) {
}
2016-05-30 16:08:09 +02:00
// Remove removes a plugin by it's name, if pluginName is empty "" or no plugin found with this name, then nothing is removed and a specific error is returned.
// This doesn't calls the PreClose method
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) Remove ( pluginName string ) error {
2016-05-30 16:08:09 +02:00
if p . activatedPlugins == nil {
2016-09-01 05:01:53 +02:00
return errPluginRemoveNoPlugins
2016-05-30 16:08:09 +02:00
}
if pluginName == "" {
//return error: cannot delete an unamed plugin
2016-09-01 05:01:53 +02:00
return errPluginRemoveEmptyName
2016-05-30 16:08:09 +02:00
}
indexToRemove := - 1
for i := range p . activatedPlugins {
if p . GetName ( p . activatedPlugins [ i ] ) == pluginName { // Note: if GetName is not implemented then the name is "" which is != with the plugiName, we checked this before.
indexToRemove = i
}
}
if indexToRemove == - 1 { //if index stills -1 then no plugin was found with this name, just return an error. it is not a critical error.
2016-09-01 05:01:53 +02:00
return errPluginRemoveNotFound
2016-05-30 16:08:09 +02:00
}
p . activatedPlugins = append ( p . activatedPlugins [ : indexToRemove ] , p . activatedPlugins [ indexToRemove + 1 : ] ... )
return nil
}
// GetName returns the name of a plugin, if no GetName() implemented it returns an empty string ""
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) GetName ( plugin Plugin ) string {
if pluginObj , ok := plugin . ( pluginGetName ) ; ok {
2016-05-30 16:08:09 +02:00
return pluginObj . GetName ( )
}
return ""
}
// GetDescription returns the name of a plugin, if no GetDescription() implemented it returns an empty string ""
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) GetDescription ( plugin Plugin ) string {
if pluginObj , ok := plugin . ( pluginGetDescription ) ; ok {
2016-05-30 16:08:09 +02:00
return pluginObj . GetDescription ( )
}
return ""
}
// GetByName returns a plugin instance by it's name
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) GetByName ( pluginName string ) Plugin {
2016-05-30 16:08:09 +02:00
if p . activatedPlugins == nil {
return nil
}
for i := range p . activatedPlugins {
2016-06-14 07:45:40 +02:00
if pluginObj , ok := p . activatedPlugins [ i ] . ( pluginGetName ) ; ok {
2016-05-30 16:08:09 +02:00
if pluginObj . GetName ( ) == pluginName {
return pluginObj
}
}
}
return nil
}
// GetAll returns all activated plugins
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) GetAll ( ) [ ] Plugin {
2016-05-30 16:08:09 +02:00
return p . activatedPlugins
}
// GetDownloader returns the download manager
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) GetDownloader ( ) PluginDownloadManager {
2016-05-30 16:08:09 +02:00
// create it if and only if it used somewhere
if p . downloader == nil {
2016-06-14 07:45:40 +02:00
p . downloader = & pluginDownloadManager { }
2016-05-30 16:08:09 +02:00
}
return p . downloader
}
// Printf sends plain text to any registed logger (future), some plugins maybe want use this method
// maybe at the future I change it, instead of sync even-driven to async channels...
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) Printf ( format string , a ... interface { } ) {
2016-06-06 12:25:09 +02:00
if p . logger != nil {
p . logger . Printf ( format , a ... ) //for now just this.
}
2016-05-30 16:08:09 +02:00
}
2016-07-07 23:59:00 +02:00
// PreLookup adds a PreLookup plugin-function to the plugin flow container
func ( p * pluginContainer ) PreLookup ( fn PreLookupFunc ) {
p . Add ( fn )
}
// DoPreLookup raise all plugins which has the PreLookup method
func ( p * pluginContainer ) DoPreLookup ( r Route ) {
for i := range p . activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj , ok := p . activatedPlugins [ i ] . ( pluginPreLookup ) ; ok {
pluginObj . PreLookup ( r )
}
}
}
2016-06-14 07:45:40 +02:00
// PreListen adds a PreListen plugin-function to the plugin flow container
func ( p * pluginContainer ) PreListen ( fn PreListenFunc ) {
2016-05-30 16:08:09 +02:00
p . Add ( fn )
}
2016-07-07 23:59:00 +02:00
// DoPreListen raise all plugins which has the PreListen method
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) DoPreListen ( station * Framework ) {
2016-05-30 16:08:09 +02:00
for i := range p . activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
2016-06-14 07:45:40 +02:00
if pluginObj , ok := p . activatedPlugins [ i ] . ( pluginPreListen ) ; ok {
pluginObj . PreListen ( station )
2016-05-30 16:08:09 +02:00
}
}
}
2016-06-14 07:45:40 +02:00
// DoPreListenParallel raise all PreListen plugins 'at the same time'
func ( p * pluginContainer ) DoPreListenParallel ( station * Framework ) {
var wg sync . WaitGroup
2016-05-30 16:08:09 +02:00
2016-06-14 07:45:40 +02:00
for _ , plugin := range p . activatedPlugins {
wg . Add ( 1 )
2016-05-30 16:08:09 +02:00
// check if this method exists on our plugin obj, these are optionaly and call it
2016-06-14 07:45:40 +02:00
go func ( plugin Plugin ) {
if pluginObj , ok := plugin . ( pluginPreListen ) ; ok {
pluginObj . PreListen ( station )
}
2016-05-30 16:08:09 +02:00
2016-06-14 07:45:40 +02:00
wg . Done ( )
2016-05-30 16:08:09 +02:00
2016-06-14 07:45:40 +02:00
} ( plugin )
2016-05-30 16:08:09 +02:00
}
2016-06-14 07:45:40 +02:00
wg . Wait ( )
2016-05-30 16:08:09 +02:00
}
// PostListen adds a PostListen plugin-function to the plugin flow container
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) PostListen ( fn PostListenFunc ) {
2016-05-30 16:08:09 +02:00
p . Add ( fn )
}
// DoPostListen raise all plugins which has the DoPostListen method
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) DoPostListen ( station * Framework ) {
2016-05-30 16:08:09 +02:00
for i := range p . activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
2016-06-14 07:45:40 +02:00
if pluginObj , ok := p . activatedPlugins [ i ] . ( pluginPostListen ) ; ok {
2016-05-30 16:08:09 +02:00
pluginObj . PostListen ( station )
}
}
}
// PreClose adds a PreClose plugin-function to the plugin flow container
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) PreClose ( fn PreCloseFunc ) {
2016-05-30 16:08:09 +02:00
p . Add ( fn )
}
// DoPreClose raise all plugins which has the DoPreClose method
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) DoPreClose ( station * Framework ) {
2016-05-30 16:08:09 +02:00
for i := range p . activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
2016-06-14 07:45:40 +02:00
if pluginObj , ok := p . activatedPlugins [ i ] . ( pluginPreClose ) ; ok {
2016-05-30 16:08:09 +02:00
pluginObj . PreClose ( station )
}
}
}
// PreDownload adds a PreDownload plugin-function to the plugin flow container
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) PreDownload ( fn PreDownloadFunc ) {
2016-05-30 16:08:09 +02:00
p . Add ( fn )
}
// DoPreDownload raise all plugins which has the DoPreDownload method
2016-06-14 07:45:40 +02:00
func ( p * pluginContainer ) DoPreDownload ( pluginTryToDownload Plugin , downloadURL string ) {
2016-05-30 16:08:09 +02:00
for i := range p . activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
2016-06-14 07:45:40 +02:00
if pluginObj , ok := p . activatedPlugins [ i ] . ( pluginPreDownload ) ; ok {
2016-05-30 16:08:09 +02:00
pluginObj . PreDownload ( pluginTryToDownload , downloadURL )
}
}
}