Newer
Older
bathroom-plugin / mattermost / server / bathroom.go
package main

import (
	"fmt"
	"net/http"
	"github.com/mattermost/mattermost-server/plugin"
	"github.com/pkg/errors"
	"sync"
	"reflect"
)

type Config struct {
	NumDoors uint8
	WatchPath string
}

type HelloWorldPlugin struct {
	plugin.MattermostPlugin
	config *Config
	configLock sync.RWMutex
	configChanged chan struct{}
}

func (p *HelloWorldPlugin) Init() *HelloWorldPlugin {
	p.config = &Config{NumDoors: 1, WatchPath: "./"}

	return p;
}

func (p *HelloWorldPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, world!")
}

func (p *HelloWorldPlugin) OnConfigurationChange() error {
	p.configLock.Lock()
	defer p.configLock.Unlock()

	var newConfig *Config = new(Config);
	if err := p.API.LoadPluginConfiguration(newConfig); err != nil {
		return errors.Wrap(err, "failed to load configuration")
	}

	if newConfig != p.config && reflect.ValueOf(*newConfig).NumField() != 0 {
	//	p.config = newConfig
	}

	p.API.LogInfo(fmt.Sprintf("Config: %d %s", p.config.NumDoors, p.config.WatchPath))

	return nil
}

func watchLoop(p *HelloWorldPlugin) {
	for {
		p.configLock.Lock()
		var restart chan struct{} = p.configChanged
		p.configLock.Unlock()
		select {
		case <- restart:
			p.API.LogInfo("CONFIG CHANGED, RESTARTING")
		}
	}
}


func (p *HelloWorldPlugin) OnActivate() error {
	//go watchLoop(p)
	return nil
}

// This example demonstrates a plugin that handles HTTP requests which respond by greeting the
// world.
func main() {
	plugin.ClientMain((&HelloWorldPlugin{}).Init())
}