feat!: complete rearrangement for multi-module platform-specific modules

This commit is contained in:
2024-10-27 16:50:15 -05:00
parent 75892037b8
commit 1444d3fc2b
21 changed files with 546 additions and 157 deletions

77
cmd/windows/main.go Normal file
View File

@@ -0,0 +1,77 @@
package main
import (
"log"
"log/slog"
"time"
"internal"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
)
type WrapperService struct {
service internal.Service
}
func (wrapper *WrapperService) Execute(args []string, requestChannel <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
const acceptedCommands = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
tick := time.Tick(5 * time.Second)
status <- svc.Status{State: svc.StartPending}
status <- svc.Status{State: svc.Running, Accepts: acceptedCommands}
loop:
for {
select {
case <-tick:
slog.Debug("Tick Handled...!")
case changeRequest := <-requestChannel:
switch changeRequest.Cmd {
case svc.Interrogate:
slog.Debug("Interrogate Requested", "changeRequest", changeRequest)
status <- changeRequest.CurrentStatus
case svc.Stop, svc.Shutdown:
slog.Warn("Shutdown Requested", "changeRequest", changeRequest)
break loop
case svc.Pause:
wrapper.service.Pause()
slog.Warn("Pause Requested", "changeRequest", changeRequest)
status <- svc.Status{State: svc.Paused, Accepts: acceptedCommands}
case svc.Continue:
slog.Info("Continue Requested", "changeRequest", changeRequest)
status <- svc.Status{State: svc.Running, Accepts: acceptedCommands}
default:
slog.Warn("Unexpected Change Request", "changeRequest", changeRequest)
}
}
}
slog.Info("Service Stopping")
status <- svc.Status{State: svc.StopPending}
return false, 1
}
func runService(name string, isDebug bool) {
service := WrapperService{
service: internal.NewApp(),
}
if isDebug {
err := debug.Run(name, &service)
if err != nil {
log.Fatalln("Error running service in debug mode.")
}
} else {
err := svc.Run(name, &service)
if err != nil {
log.Fatalln("Error running service in Service Control mode.")
}
}
}
func main() {
runService("DoorTray", true)
}