parent
e4f23c6805
commit
900670add0
@ -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()
|
@ -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
|
||||
}
|
||||
|
44
src/components/daemon/daemon.go
Normal file
44
src/components/daemon/daemon.go
Normal 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)
|
||||
}
|
||||
}
|
13
src/components/daemon/handler.go
Normal file
13
src/components/daemon/handler.go
Normal 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
|
||||
}
|
@ -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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user