2017-06-30 21:10:25 +05:30
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-14 23:36:22 +05:30
|
|
|
"net"
|
2017-06-30 21:10:25 +05:30
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/mono83/slf/wd"
|
2017-08-14 23:36:22 +05:30
|
|
|
|
2017-08-18 03:20:23 +05:30
|
|
|
"elyby/minecraft-skinsystem/interfaces"
|
2017-08-14 23:36:22 +05:30
|
|
|
"elyby/minecraft-skinsystem/ui"
|
2017-06-30 21:10:25 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ListenSpec string
|
|
|
|
|
2017-08-18 03:20:23 +05:30
|
|
|
SkinsRepo interfaces.SkinsRepository
|
|
|
|
CapesRepo interfaces.CapesRepository
|
|
|
|
Logger wd.Watchdog
|
|
|
|
UI ui.Config
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func Run(cfg *Config) error {
|
|
|
|
cfg.Logger.Info(fmt.Sprintf("Starting, HTTP on: %s\n", cfg.ListenSpec))
|
|
|
|
|
|
|
|
uiService, err := ui.NewUiService(cfg.Logger, cfg.SkinsRepo, cfg.CapesRepo)
|
|
|
|
if err != nil {
|
|
|
|
cfg.Logger.Error(fmt.Sprintf("Error creating ui services: %v\n", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", cfg.ListenSpec)
|
|
|
|
if err != nil {
|
|
|
|
cfg.Logger.Error(fmt.Sprintf("Error creating listener: %v\n", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Start(cfg.UI, uiService, listener)
|
|
|
|
|
|
|
|
waitForSignal(cfg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForSignal(cfg *Config) {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
s := <-ch
|
|
|
|
cfg.Logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s))
|
|
|
|
}
|