66 lines
1.1 KiB
Go

package main
import (
"daemonSocketExample/daemonconn"
"daemonSocketExample/msghandlers"
"github.com/sevlyar/go-daemon"
"log"
"log/slog"
"os"
"time"
)
func main() {
ctx := &daemon.Context{
PidFileName: "liberum-daemon.pid",
PidFilePerm: 0644,
LogFileName: "liberum-daemon.log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
}
d, err := ctx.Reborn()
if err != nil {
panic(err)
}
if d != nil {
return
}
defer func(ctx *daemon.Context) {
slog.Info("Stopping daemon")
_ = ctx.Release()
}(ctx)
log.Println("Daemon started")
_ = os.Remove("/tmp/liberum.sock")
connChan, err := daemonconn.UnixSocketListen(daemonconn.DefaultUnixSocketPath)
if err != nil {
panic(err)
}
go func() {
for conn := range connChan {
err := msghandlers.HandleDaemonConn(conn)
if err != nil {
panic(err)
}
}
}()
conn, err := daemonconn.UnixSocketConnect(daemonconn.DefaultUnixSocketPath)
if err != nil {
panic(err)
}
err = conn.WriteMessage(daemonconn.EchoRequest{EchoByte: 123})
if err != nil {
panic(err)
}
time.Sleep(time.Second * 5)
}