@marcel91200 / caddy Mirror · read-only / commits / 253d97c9
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 09:15 UTC

core: Chdir to executable location on Windows (#5115)

Since all Windows services are run from the Windows system directory, make it easier for users by switching to our program directory right after the start.

Tobias Gruetzmacher committed Oct 4, 2022 at 19:04 UTC 253d97c93dce9a3365e67d0360b2e47dd165578e
1 file changed +11
service_windows.go
+11
@@ -15,6 +15,9 @@
15 package caddy
16
17 import (
18 + "os"
19 + "path/filepath"
20 +
21 "github.com/caddyserver/caddy/v2/notify"
22 "golang.org/x/sys/windows/svc"
23 )
@@ -24,6 +27,14 @@ func init() {
27 if err != nil || !isService {
28 return
29 }
30 +
31 + // Windows services always start in the system32 directory, try to
32 + // switch into the directory where the caddy executable is.
33 + execPath, err := os.Executable()
34 + if err == nil {
35 + _ = os.Chdir(filepath.Dir(execPath))
36 + }
37 +
38 go func() {
39 _ = svc.Run("", runner{})
40 }()