initial
Some checks failed
builder / build (push) Failing after 25s

This commit is contained in:
abs3nt 2024-10-26 18:21:31 -07:00
parent e4f23c6805
commit 900670add0
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
5 changed files with 83 additions and 3 deletions

View File

@ -10,8 +10,8 @@ import (
"go.uber.org/fx/fxevent" "go.uber.org/fx/fxevent"
"git.asdf.cafe/abs3nt/gspot/src/components/cache" "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/commands"
"git.asdf.cafe/abs3nt/gspot/src/components/daemon"
"git.asdf.cafe/abs3nt/gspot/src/components/logger" "git.asdf.cafe/abs3nt/gspot/src/components/logger"
"git.asdf.cafe/abs3nt/gspot/src/services" "git.asdf.cafe/abs3nt/gspot/src/services"
) )
@ -33,7 +33,7 @@ func main() {
logger.NewLogger, logger.NewLogger,
), ),
fx.Invoke( fx.Invoke(
cli.Run, daemon.Run,
), ),
) )
app.Run() app.Run()

View File

@ -3,6 +3,7 @@ package cli
import ( import (
"context" "context"
"fmt" "fmt"
"net/rpc"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -38,7 +39,7 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
if cmd.Args().Present() { if cmd.Args().Present() {
return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " ")) return fmt.Errorf("unexpected arguments: %s", strings.Join(cmd.Args().Slice(), " "))
} }
return c.Play() return sendCommandRPC("Play", "hello")
}, },
Category: "Playback", Category: "Playback",
}, },
@ -449,3 +450,24 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
} }
s.Shutdown() 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
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -7,4 +7,5 @@ type Config struct {
Port string `yaml:"port"` Port string `yaml:"port"`
LogLevel string `yaml:"log_level" default:"info"` LogLevel string `yaml:"log_level" default:"info"`
LogOutput string `yaml:"log_output" default:"stdout"` LogOutput string `yaml:"log_output" default:"stdout"`
SocketPath string `yaml:"socket_path" default:"/tmp/gspot.sock"`
} }