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, *http.Server, error,
) { ) {
l, err := net.Listen("unix", daemonEnvVars.HTTPSocketPath) socketPath := daemon.HTTPSocketPath()
l, err := net.Listen("unix", socketPath)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"failed to listen on socket %q: %w", "failed to listen on socket %q: %w", socketPath, err,
daemonEnvVars.HTTPSocketPath,
err,
) )
} }

View File

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

View File

@ -13,15 +13,18 @@ import (
"github.com/adrg/xdg" "github.com/adrg/xdg"
) )
// DEPRECATED
//
// EnvVars are variables which are derived based on the environment which the // EnvVars are variables which are derived based on the environment which the
// process is running in. // process is running in.
//
// TODO EnvVars should be private to this package.
type EnvVars struct { type EnvVars struct {
RuntimeDirPath string // TODO should be private to this package RuntimeDirPath string
StateDirPath string // TODO should be private to this package StateDirPath string
HTTPSocketPath string
} }
func getRPCSocketDirPath() string { func getDefaultHTTPSocketDirPath() string {
path, err := firstExistingDir( path, err := firstExistingDir(
"/run", "/run",
"/var/run", "/var/run",
@ -29,12 +32,27 @@ func getRPCSocketDirPath() string {
"/dev/shm", "/dev/shm",
) )
if err != nil { 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 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 // GetEnvVars will return the EnvVars of the current processes, as determined by
// the process's environment. // the process's environment.
var GetEnvVars = sync.OnceValue(func() (v EnvVars) { 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") }, func() string { return filepath.Join(xdg.StateHome, "isle") },
) )
v.HTTPSocketPath = envOr(
"ISLE_SOCKET_PATH",
func() string {
return filepath.Join(getRPCSocketDirPath(), "isle-daemon.sock")
},
)
return return
}) })

View File

@ -12,6 +12,6 @@ cat <<EOF
export TMPDIR="$TMPDIR" export TMPDIR="$TMPDIR"
export XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" export XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR"
export XDG_STATE_HOME="$XDG_STATE_HOME" 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" cd "$TMPDIR"
EOF EOF