Compare commits

...

2 commits

Author SHA1 Message Date
wtf911
2691acfd08
Merge feb6647f23 into 630f2957de 2025-05-07 05:43:34 +00:00
wtf911
feb6647f23 Adds the ability to run ntfy as a Windows service.
Example service creation command:

sc.exe create ntfy start= auto binPath= "YOURPATH\ntfy.exe serve"

To use a server.yml file you would create the service like this:

sc.exe create ntfy start= auto binPath= "YOURPATH\ntfy.exe serve --config YOURPATH\server.yml"
2025-05-07 00:38:29 -04:00

70
cmd/serve_windows.go Normal file
View file

@ -0,0 +1,70 @@
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found at: https://github.com/kardianos/minwinsvc
//go:build windows && !noserver
package cmd
import (
"os"
"sync"
"golang.org/x/sys/windows/svc"
)
var (
onExit func()
guard sync.Mutex
)
func init() {
isService, err := svc.IsWindowsService()
if err != nil {
panic(err)
}
if !isService {
return
}
go func() {
_ = svc.Run("", runner{})
guard.Lock()
f := onExit
guard.Unlock()
// Don't hold this lock in user code.
if f != nil {
f()
}
// Make sure we exit.
os.Exit(0)
}()
}
func setOnExit(f func()) {
guard.Lock()
onExit = f
guard.Unlock()
}
type runner struct{}
func (runner) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
for {
c := <-r
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending}
return false, 0
}
}
return false, 0
}