Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions directories.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !docker
// +build !docker
//go:build !windows && !darwin && !dragonfly && !freebsd && !netbsd && !openbsd && !docker
// +build !windows,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!docker

package maddy

Expand Down
47 changes: 47 additions & 0 deletions directories_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build (darwin || dragonfly || freebsd || netbsd || openbsd) && !docker
// +build darwin dragonfly freebsd netbsd openbsd
// +build !docker

package maddy

var (
// ConfigDirectory specifies platform-specific value
// that should be used as a location of default configuration.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
ConfigDirectory = "/etc/maddy"

// DefaultStateDirectory specifies platform-specific
// default for StateDirectory.
//
// Most code should use StateDirectory instead since
// it will contain the effective location of the state
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultStateDirectory = "/var/lib/maddy"

// DefaultRuntimeDirectory specifies platform-specific
// default for RuntimeDirectory.
//
// Most code should use RuntimeDirectory instead since
// it will contain the effective location of the state
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultRuntimeDirectory = "/var/run/maddy"

// DefaultLibexecDirectory specifies platform-specific
// default for LibexecDirectory.
//
// Most code should use LibexecDirectory since it will
// contain the effective location of the libexec
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultLibexecDirectory = "/usr/lib/maddy"
)
67 changes: 67 additions & 0 deletions directories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//go:build !docker
// +build !docker

package maddy

import (
"path/filepath"
"runtime"
"testing"
)

func TestDefaultDirectories(t *testing.T) {
if ConfigDirectory == "" {
t.Error("ConfigDirectory is empty")
}
if DefaultStateDirectory == "" {
t.Error("DefaultStateDirectory is empty")
}
if DefaultRuntimeDirectory == "" {
t.Error("DefaultRuntimeDirectory is empty")
}
if DefaultLibexecDirectory == "" {
t.Error("DefaultLibexecDirectory is empty")
}

switch runtime.GOOS {
case "linux":
if ConfigDirectory != "/etc/maddy" {
t.Errorf("ConfigDirectory on Linux: got %q, want %q", ConfigDirectory, "/etc/maddy")
}
if DefaultStateDirectory != "/var/lib/maddy" {
t.Errorf("DefaultStateDirectory on Linux: got %q, want %q", DefaultStateDirectory, "/var/lib/maddy")
}
if DefaultRuntimeDirectory != "/run/maddy" {
t.Errorf("DefaultRuntimeDirectory on Linux: got %q, want %q", DefaultRuntimeDirectory, "/run/maddy")
}
if DefaultLibexecDirectory != "/usr/lib/maddy" {
t.Errorf("DefaultLibexecDirectory on Linux: got %q, want %q", DefaultLibexecDirectory, "/usr/lib/maddy")
}
case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd":
if ConfigDirectory != "/etc/maddy" {
t.Errorf("ConfigDirectory on BSD/Darwin: got %q, want %q", ConfigDirectory, "/etc/maddy")
}
if DefaultStateDirectory != "/var/lib/maddy" {
t.Errorf("DefaultStateDirectory on BSD/Darwin: got %q, want %q", DefaultStateDirectory, "/var/lib/maddy")
}
if DefaultRuntimeDirectory != "/var/run/maddy" {
t.Errorf("DefaultRuntimeDirectory on BSD/Darwin: got %q, want %q", DefaultRuntimeDirectory, "/var/run/maddy")
}
if DefaultLibexecDirectory != "/usr/lib/maddy" {
t.Errorf("DefaultLibexecDirectory on BSD/Darwin: got %q, want %q", DefaultLibexecDirectory, "/usr/lib/maddy")
}
case "windows":
if !filepath.IsAbs(ConfigDirectory) {
t.Errorf("ConfigDirectory on Windows should be absolute: got %q", ConfigDirectory)
}
if !filepath.IsAbs(DefaultStateDirectory) {
t.Errorf("DefaultStateDirectory on Windows should be absolute: got %q", DefaultStateDirectory)
}
if !filepath.IsAbs(DefaultRuntimeDirectory) {
t.Errorf("DefaultRuntimeDirectory on Windows should be absolute: got %q", DefaultRuntimeDirectory)
}
if !filepath.IsAbs(DefaultLibexecDirectory) {
t.Errorf("DefaultLibexecDirectory on Windows should be absolute: got %q", DefaultLibexecDirectory)
}
}
}
58 changes: 58 additions & 0 deletions directories_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//go:build windows && !docker
// +build windows,!docker

package maddy

import (
"os"
"path/filepath"
)

const (
defaultProgramData = `C:\ProgramData`
defaultProgramFiles = `C:\Program Files`
)

func programData() string {
if dir := os.Getenv("ProgramData"); dir != "" {
return dir
}
return defaultProgramData
}

func programFiles() string {
if dir := os.Getenv("ProgramFiles"); dir != "" {
return dir
}
return defaultProgramFiles
}

var (
// ConfigDirectory specifies platform-specific value
// that should be used as a location of default configuration.
ConfigDirectory = filepath.Join(programData(), `maddy`)

// DefaultStateDirectory specifies platform-specific
// default for StateDirectory.
//
// Most code should use StateDirectory instead since
// it will contain the effective location of the state
// directory.
DefaultStateDirectory = filepath.Join(programData(), `maddy`, `data`)

// DefaultRuntimeDirectory specifies platform-specific
// default for RuntimeDirectory.
//
// Most code should use RuntimeDirectory instead since
// it will contain the effective location of the runtime
// directory.
DefaultRuntimeDirectory = filepath.Join(programData(), `maddy`, `run`)

// DefaultLibexecDirectory specifies platform-specific
// default for LibexecDirectory.
//
// Most code should use LibexecDirectory since it will
// contain the effective location of the libexec
// directory.
DefaultLibexecDirectory = filepath.Join(programFiles(), `maddy`)
)
59 changes: 59 additions & 0 deletions directories_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build windows && !docker
// +build windows,!docker

package maddy

import (
"path/filepath"
"testing"
)

func TestWindowsDirectoryHelpers(t *testing.T) {
t.Run("programData default fallback", func(t *testing.T) {
t.Setenv("ProgramData", "")
if got := programData(); got != defaultProgramData {
t.Errorf("programData(): got %q, want %q", got, defaultProgramData)
}
})

t.Run("programData from env", func(t *testing.T) {
want := `D:\CustomData`
t.Setenv("ProgramData", want)
if got := programData(); got != want {
t.Errorf("programData(): got %q, want %q", got, want)
}
})

t.Run("programFiles default fallback", func(t *testing.T) {
t.Setenv("ProgramFiles", "")
if got := programFiles(); got != defaultProgramFiles {
t.Errorf("programFiles(): got %q, want %q", got, defaultProgramFiles)
}
})

t.Run("programFiles from env", func(t *testing.T) {
want := `D:\CustomFiles`
t.Setenv("ProgramFiles", want)
if got := programFiles(); got != want {
t.Errorf("programFiles(): got %q, want %q", got, want)
}
})

t.Run("initialized directory variables", func(t *testing.T) {
wantData := programData()
wantFiles := programFiles()

if got, want := ConfigDirectory, filepath.Join(wantData, `maddy`); got != want {
t.Errorf("ConfigDirectory: got %q, want %q", got, want)
}
if got, want := DefaultStateDirectory, filepath.Join(wantData, `maddy`, `data`); got != want {
t.Errorf("DefaultStateDirectory: got %q, want %q", got, want)
}
if got, want := DefaultRuntimeDirectory, filepath.Join(wantData, `maddy`, `run`); got != want {
t.Errorf("DefaultRuntimeDirectory: got %q, want %q", got, want)
}
if got, want := DefaultLibexecDirectory, filepath.Join(wantFiles, `maddy`); got != want {
t.Errorf("DefaultLibexecDirectory: got %q, want %q", got, want)
}
})
}
6 changes: 3 additions & 3 deletions signal_nonposix.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import (
"github.com/foxcpp/maddy/framework/log"
)

func handleSignals() os.Signal {
func handleSignals() (reload bool) {
sig := make(chan os.Signal, 5)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT)

s := <-sig
go func() {
s := handleSignals()
handleSignals()
log.Printf("forced shutdown due to signal (%v)!", s)
os.Exit(1)
}()

log.Printf("signal received (%v)", s)
return s
return false
}
Loading