Start refactoring
This commit is contained in:
@@ -5,9 +5,10 @@ package main
|
||||
import (
|
||||
_ "embed"
|
||||
"github.com/getlantern/systray"
|
||||
"opensavecloudserver/constant"
|
||||
"opensavecloudserver/server"
|
||||
"os"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"tawesoft.co.uk/go/dialog"
|
||||
)
|
||||
|
||||
//go:generate go-winres make
|
||||
@@ -16,32 +17,50 @@ import (
|
||||
var icon []byte
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
InitCommon()
|
||||
server.Serve()
|
||||
}()
|
||||
path := flag.String("config", "./config.yml", "Set the configuration file path")
|
||||
flag.Parse()
|
||||
appConfiguration, err := config.Load(*path)
|
||||
if err != nil {
|
||||
dialog.Alert("An error occured while starting the server: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := initLogger(); err != nil {
|
||||
dialog.Alert("An error occured while starting the server: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
appServer := server.NewServer(appConfiguration)
|
||||
go func(s *http.Server) {
|
||||
err := s.ListenAndServe()
|
||||
defer systray.Quit()
|
||||
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(appServer.Server)
|
||||
startWindowsApp(appServer.Server)
|
||||
}
|
||||
|
||||
func startWindowsApp(appServer *http.Server) {
|
||||
onReady := func() {
|
||||
systray.SetIcon(icon)
|
||||
systray.SetTitle("Open Save Cloud Server")
|
||||
systray.SetTooltip("The server is up and ready")
|
||||
systray.AddMenuItem("Open Save Cloud "+constant.Version, "").Disable()
|
||||
systray.AddMenuItem("Running on "+appServer.Addr, "").Disable()
|
||||
systray.AddSeparator()
|
||||
mQuit := systray.AddMenuItem("Shutdown", "Quit the server")
|
||||
for {
|
||||
select {
|
||||
case <-mQuit.ClickedCh:
|
||||
func(s *http.Server) {
|
||||
mQuit.Disable()
|
||||
systray.SetTooltip("Shutting down the server...")
|
||||
s.Shutdown()
|
||||
}(appServer)
|
||||
}
|
||||
}
|
||||
}
|
||||
onExit := func() {}
|
||||
systray.Run(onReady, onExit)
|
||||
}
|
||||
|
||||
func onReady() {
|
||||
systray.SetIcon(icon)
|
||||
systray.SetTitle("Open Save Cloud Server")
|
||||
systray.SetTooltip("Open Save Cloud Server")
|
||||
systray.AddMenuItem("Open Save Cloud", "").Disable()
|
||||
systray.AddMenuItem(constant.Version, "").Disable()
|
||||
systray.AddSeparator()
|
||||
mQuit := systray.AddMenuItem("Quit", "Quit the server")
|
||||
select {
|
||||
case <-mQuit.ClickedCh:
|
||||
quit()
|
||||
}
|
||||
}
|
||||
|
||||
func quit() {
|
||||
systray.Quit()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func onExit() {
|
||||
systray.Quit()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user