From 900670add0dd3a342a30322db3a06788c08aae53 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sat, 26 Oct 2024 18:21:31 -0700 Subject: [PATCH] initial --- main.go => cmd/gspot-daemon/main.go | 4 +-- src/components/cli/cli.go | 24 +++++++++++++++- src/components/daemon/daemon.go | 44 +++++++++++++++++++++++++++++ src/components/daemon/handler.go | 13 +++++++++ src/config/config.go | 1 + 5 files changed, 83 insertions(+), 3 deletions(-) rename main.go => cmd/gspot-daemon/main.go (95%) create mode 100644 src/components/daemon/daemon.go create mode 100644 src/components/daemon/handler.go diff --git a/main.go b/cmd/gspot-daemon/main.go similarity index 95% rename from main.go rename to cmd/gspot-daemon/main.go index 04fdecd..568685e 100644 --- a/main.go +++ b/cmd/gspot-daemon/main.go @@ -10,8 +10,8 @@ import ( "go.uber.org/fx/fxevent" "git.asdf.cafe/abs3nt/gspot/src/components/cache" - "git.asdf.cafe/abs3nt/gspot/src/components/cli" "git.asdf.cafe/abs3nt/gspot/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/components/daemon" "git.asdf.cafe/abs3nt/gspot/src/components/logger" "git.asdf.cafe/abs3nt/gspot/src/services" ) @@ -33,7 +33,7 @@ func main() { logger.NewLogger, ), fx.Invoke( - cli.Run, + daemon.Run, ), ) app.Run() diff --git a/src/components/cli/cli.go b/src/components/cli/cli.go index 8c5f52d..e831d4e 100644 --- a/src/components/cli/cli.go +++ b/src/components/cli/cli.go @@ -3,6 +3,7 @@ package cli import ( "context" "fmt" + "net/rpc" "os" "strconv" "strings" @@ -38,7 +39,7 @@ func Run(c *commands.Commander, s fx.Shutdowner) { if cmd.Args().Present() { return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " ")) } - return c.Play() + return sendCommandRPC("Play", "hello") }, Category: "Playback", }, @@ -449,3 +450,24 @@ func Run(c *commands.Commander, s fx.Shutdowner) { } s.Shutdown() } + +type GenericReply struct { + Message string +} + +func sendCommandRPC(method string, args interface{}) error { + client, err := rpc.Dial("unix", "/tmp/gspot.sock") + if err != nil { + return fmt.Errorf("could not connect to daemon: %v", err) + } + defer client.Close() + + var reply string + err = client.Call("Handler."+method, args, &reply) + if err != nil { + return fmt.Errorf("error calling %s: %v", method, err) + } + + fmt.Println(reply) + return nil +} diff --git a/src/components/daemon/daemon.go b/src/components/daemon/daemon.go new file mode 100644 index 0000000..11dd415 --- /dev/null +++ b/src/components/daemon/daemon.go @@ -0,0 +1,44 @@ +package daemon + +import ( + "log" + "net" + "net/rpc" + "os" + + "go.uber.org/fx" + + "git.asdf.cafe/abs3nt/gspot/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/config" +) + +func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) { + socketPath := conf.SocketPath + if _, err := os.Stat(socketPath); err == nil { + os.Remove(socketPath) + } + + CommandHandler := Handler{ + Commander: c, + } + + rpc.Register(&CommandHandler) + + listener, err := net.Listen("unix", socketPath) + if err != nil { + log.Fatal("Listen error:", err) + } + defer listener.Close() + os.Chmod(socketPath, 0o666) + + log.Println("Daemon is listening on", socketPath) + + for { + conn, err := listener.Accept() + if err != nil { + log.Println("Accept error:", err) + continue + } + go rpc.ServeConn(conn) + } +} diff --git a/src/components/daemon/handler.go b/src/components/daemon/handler.go new file mode 100644 index 0000000..2603fad --- /dev/null +++ b/src/components/daemon/handler.go @@ -0,0 +1,13 @@ +package daemon + +import "git.asdf.cafe/abs3nt/gspot/src/components/commands" + +type Handler struct { + Commander *commands.Commander +} + +func (h *Handler) Play(args string, reply *string) error { + err := h.Commander.Play() + *reply = "hello fucker" + return err +} diff --git a/src/config/config.go b/src/config/config.go index b7550b9..c80c75c 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -7,4 +7,5 @@ type Config struct { Port string `yaml:"port"` LogLevel string `yaml:"log_level" default:"info"` LogOutput string `yaml:"log_output" default:"stdout"` + SocketPath string `yaml:"socket_path" default:"/tmp/gspot.sock"` }