2016-05-30 16:08:09 +02:00
|
|
|
package iriscontrol
|
|
|
|
|
|
|
|
import (
|
2016-06-17 00:51:42 +02:00
|
|
|
"strconv"
|
2016-05-30 16:08:09 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
type (
|
|
|
|
// IrisControl is the interface which the iriscontrol should implements
|
|
|
|
// it's empty for now because no need any public API
|
|
|
|
IrisControl interface{}
|
|
|
|
iriscontrol struct {
|
|
|
|
port int
|
|
|
|
users map[string]string
|
|
|
|
|
|
|
|
// child is the plugin's standalone station
|
|
|
|
child *iris.Framework
|
|
|
|
// the station which this plugins is registed to
|
|
|
|
parent *iris.Framework
|
|
|
|
parentLastOp time.Time
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
2016-06-06 00:37:32 +02:00
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
pluginInfo struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
)
|
2016-05-30 16:08:09 +02:00
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
var _ IrisControl = &iriscontrol{}
|
2016-05-30 16:08:09 +02:00
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
func (i *iriscontrol) listen(f *iris.Framework) {
|
|
|
|
i.parent = f
|
|
|
|
i.parentLastOp = time.Now()
|
|
|
|
i.initializeChild()
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
func (i *iriscontrol) initializeChild() {
|
|
|
|
i.child = iris.New()
|
|
|
|
i.child.Config.DisableBanner = true
|
|
|
|
i.child.Config.Render.Template.Directory = assetsPath + "templates"
|
|
|
|
|
|
|
|
// set the assets
|
|
|
|
i.child.Static("/public", assetsPath+"static", 1)
|
|
|
|
|
|
|
|
// set the authentication middleware
|
|
|
|
i.child.Use(basicauth.New(config.BasicAuth{
|
|
|
|
Users: i.users,
|
|
|
|
ContextKey: "user",
|
|
|
|
Realm: config.DefaultBasicAuthRealm,
|
|
|
|
Expires: time.Duration(1) * time.Hour,
|
|
|
|
}))
|
|
|
|
|
|
|
|
i.child.Get("/", func(ctx *iris.Context) {
|
|
|
|
ctx.MustRender("index.html", iris.Map{
|
|
|
|
"ServerIsRunning": i.parentIsRunning(),
|
|
|
|
"Routes": i.parentLookups(),
|
|
|
|
"Plugins": i.infoPlugins(),
|
|
|
|
"LastOperationDateStr": i.infoLastOp(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
i.child.Post("/start_server", func(ctx *iris.Context) {
|
|
|
|
|
|
|
|
if !i.parentIsRunning() {
|
|
|
|
// starts the server with its old configuration
|
|
|
|
go func() {
|
|
|
|
if err := i.parent.HTTPServer.Open(); err != nil {
|
|
|
|
i.parent.Logger.Warningf(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
i.parentLastOp = time.Now()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
i.child.Post("/stop_server", func(ctx *iris.Context) {
|
|
|
|
|
|
|
|
if i.parentIsRunning() {
|
|
|
|
i.parentLastOp = time.Now()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := i.parent.CloseWithErr(); err != nil {
|
|
|
|
i.parent.Logger.Warningf(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-06-17 01:01:42 +02:00
|
|
|
go i.child.Listen(i.parent.HTTPServer.VirtualHostname() + ":" + strconv.Itoa(i.port))
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
func (i *iriscontrol) parentIsRunning() bool {
|
|
|
|
return i.parent != nil && i.parent.HTTPServer.IsListening()
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
func (i *iriscontrol) parentLookups() []iris.Route {
|
|
|
|
if i.parent == nil {
|
|
|
|
return nil
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
2016-06-17 00:51:42 +02:00
|
|
|
return i.parent.Lookups()
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
func (i *iriscontrol) infoPlugins() (info []pluginInfo) {
|
|
|
|
plugins := i.parent.Plugins
|
|
|
|
for _, p := range plugins.GetAll() {
|
|
|
|
name := plugins.GetName(p)
|
|
|
|
description := plugins.GetDescription(p)
|
|
|
|
if name == "" {
|
|
|
|
name = "Unknown plugin name"
|
|
|
|
}
|
|
|
|
if description == "" {
|
|
|
|
description = "description is not available"
|
|
|
|
}
|
|
|
|
|
|
|
|
info = append(info, pluginInfo{Name: name, Description: description})
|
|
|
|
}
|
|
|
|
return
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:42 +02:00
|
|
|
func (i *iriscontrol) infoLastOp() string {
|
|
|
|
return i.parentLastOp.Format(config.TimeFormat)
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|