2016-05-30 16:08:09 +02:00
package iriscontrol
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/config"
2016-06-06 00:37:32 +02:00
"github.com/kataras/iris/middleware/basicauth"
2016-05-30 16:08:09 +02:00
)
// Name the name(string) of this plugin which is Iris Control
const Name = "Iris Control"
type irisControlPlugin struct {
options config . IrisControl
// the pluginContainer is the container which keeps this plugin from the main user's iris instance
2016-06-14 07:45:40 +02:00
pluginContainer iris . PluginContainer
2016-05-30 16:08:09 +02:00
// the station object of the main user's iris instance
2016-06-14 07:45:40 +02:00
station * iris . Framework
2016-05-30 16:08:09 +02:00
//a copy of the server which the main user's iris is listening for
2016-06-14 07:45:40 +02:00
stationServer * iris . Server
2016-05-30 16:08:09 +02:00
// the server is this plugin's server object, it is managed by this plugin only
2016-06-14 07:45:40 +02:00
server * iris . Framework
2016-05-30 16:08:09 +02:00
//
//infos
2016-06-14 07:45:40 +02:00
routes [ ] iris . Route
2016-05-30 16:08:09 +02:00
plugins [ ] PluginInfo
2016-06-06 12:25:09 +02:00
// last time the server was on
lastOperationDate time . Time
2016-05-30 16:08:09 +02:00
//
2016-06-06 00:37:32 +02:00
authFunc iris . HandlerFunc
2016-05-30 16:08:09 +02:00
}
// New returns the plugin which is ready-to-use inside iris.Plugin method
// receives config.IrisControl
2016-06-14 07:45:40 +02:00
func New ( cfg ... config . IrisControl ) iris . Plugin {
2016-05-30 16:08:09 +02:00
c := config . DefaultIrisControl ( )
if len ( cfg ) > 0 {
c = cfg [ 0 ]
}
2016-06-06 00:37:32 +02:00
if c . Users == nil || len ( c . Users ) == 0 {
2016-05-30 16:08:09 +02:00
panic ( Name + " Error: you should pass authenticated users map to the options, refer to the docs!" )
}
2016-06-06 00:37:32 +02:00
auth := basicauth . Default ( c . Users )
2016-06-14 07:45:40 +02:00
return & irisControlPlugin { options : c , authFunc : auth , routes : make ( [ ] iris . Route , 0 ) }
2016-05-30 16:08:09 +02:00
}
// Web set the options for the plugin and return the plugin which is ready-to-use inside iris.Plugin method
// first parameter is port
// second parameter is map of users (username:password)
2016-06-14 07:45:40 +02:00
func Web ( port int , users map [ string ] string ) iris . Plugin {
return New ( config . IrisControl { Port : port , Users : users } )
2016-05-30 16:08:09 +02:00
}
// implement the base IPlugin
2016-06-14 07:45:40 +02:00
func ( i * irisControlPlugin ) Activate ( container iris . PluginContainer ) error {
2016-05-30 16:08:09 +02:00
i . pluginContainer = container
return nil
}
func ( i irisControlPlugin ) GetName ( ) string {
return Name
}
func ( i irisControlPlugin ) GetDescription ( ) string {
return Name + " is just a web interface which gives you control of your Iris.\n"
}
//
// implement the rest of the plugin
// PostListen sets the station object after the main server starts
// starts the actual work of the plugin
2016-06-14 07:45:40 +02:00
func ( i * irisControlPlugin ) PostListen ( s * iris . Framework ) {
2016-05-30 16:08:09 +02:00
//if the first time, because other times start/stop of the server so listen and no listen will be only from the control panel
if i . station == nil {
i . station = s
2016-06-14 07:45:40 +02:00
i . stationServer = i . station . HTTPServer
2016-06-06 12:25:09 +02:00
i . lastOperationDate = time . Now ( )
2016-06-14 07:45:40 +02:00
i . routes = s . Lookups ( )
2016-05-30 16:08:09 +02:00
i . startControlPanel ( )
}
}
2016-06-14 07:45:40 +02:00
func ( i * irisControlPlugin ) PreClose ( s * iris . Framework ) {
2016-05-30 16:08:09 +02:00
// Do nothing. This is a wrapper of the main server if we destroy when users stop the main server then we cannot continue the control panel i.Destroy()
}
//
// Destroy removes entirely the plugin, the options and all of these properties, you cannot re-use this plugin after this method.
func ( i * irisControlPlugin ) Destroy ( ) {
i . pluginContainer . Remove ( Name )
i . options = config . IrisControl { }
i . routes = nil
i . station = nil
2016-06-06 12:25:09 +02:00
i . lastOperationDate = config . CookieExpireNever
2016-05-30 16:08:09 +02:00
i . server . Close ( )
i . pluginContainer = nil
2016-06-06 00:37:32 +02:00
i . authFunc = nil
2016-05-30 16:08:09 +02:00
i . pluginContainer . Printf ( "[%s] %s is turned off" , time . Now ( ) . UTC ( ) . String ( ) , Name )
}