Rename ISLE_SOCKET_PATH and refactor how it's loaded a bit

This commit is contained in:
Brian Picciano 2024-07-06 14:26:06 +02:00
parent 05e91cd657
commit 179059fd3d
4 changed files with 29 additions and 18 deletions

View File

@ -66,12 +66,11 @@ func newHTTPServer(
) (
*http.Server, error,
) {
l, err := net.Listen("unix", daemonEnvVars.HTTPSocketPath)
socketPath := daemon.HTTPSocketPath()
l, err := net.Listen("unix", socketPath)
if err != nil {
return nil, fmt.Errorf(
"failed to listen on socket %q: %w",
daemonEnvVars.HTTPSocketPath,
err,
"failed to listen on socket %q: %w", socketPath, err,
)
}

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"isle/daemon"
"isle/daemon/jsonrpc2"
"os"
"strings"
@ -110,7 +111,7 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error {
}
daemonRCPClient := jsonrpc2.NewUnixHTTPClient(
daemonEnvVars.HTTPSocketPath, daemonHTTPRPCPath,
daemon.HTTPSocketPath(), daemonHTTPRPCPath,
)
err := subCmd.do(subCmdCtx{

View File

@ -13,15 +13,18 @@ import (
"github.com/adrg/xdg"
)
// DEPRECATED
//
// EnvVars are variables which are derived based on the environment which the
// process is running in.
//
// TODO EnvVars should be private to this package.
type EnvVars struct {
RuntimeDirPath string // TODO should be private to this package
StateDirPath string // TODO should be private to this package
HTTPSocketPath string
RuntimeDirPath string
StateDirPath string
}
func getRPCSocketDirPath() string {
func getDefaultHTTPSocketDirPath() string {
path, err := firstExistingDir(
"/run",
"/var/run",
@ -29,12 +32,27 @@ func getRPCSocketDirPath() string {
"/dev/shm",
)
if err != nil {
panic(fmt.Sprintf("Failed to find directory for RPC socket: %v", err))
panic(fmt.Sprintf("Failed to find directory for HTTP socket: %v", err))
}
return path
}
// HTTPSocketPath returns the path to the daemon's HTTP socket which is used for
// RPC and other functionality.
var HTTPSocketPath = sync.OnceValue(func() string {
return envOr(
"ISLE_DAEMON_HTTP_SOCKET_PATH",
func() string {
return filepath.Join(
getDefaultHTTPSocketDirPath(), "isle-daemon.sock",
)
},
)
})
// DEPRECATED
//
// GetEnvVars will return the EnvVars of the current processes, as determined by
// the process's environment.
var GetEnvVars = sync.OnceValue(func() (v EnvVars) {
@ -51,13 +69,6 @@ var GetEnvVars = sync.OnceValue(func() (v EnvVars) {
func() string { return filepath.Join(xdg.StateHome, "isle") },
)
v.HTTPSocketPath = envOr(
"ISLE_SOCKET_PATH",
func() string {
return filepath.Join(getRPCSocketDirPath(), "isle-daemon.sock")
},
)
return
})

View File

@ -12,6 +12,6 @@ cat <<EOF
export TMPDIR="$TMPDIR"
export XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR"
export XDG_STATE_HOME="$XDG_STATE_HOME"
export ISLE_SOCKET_PATH="$ROOT_TMPDIR/$base-daemon.sock"
export ISLE_DAEMON_HTTP_SOCKET_PATH="$ROOT_TMPDIR/$base-daemon.sock"
cd "$TMPDIR"
EOF