From aa57cfdf36407148af613e5633e264a22c4459de Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 6 Jul 2024 17:57:56 +0530 Subject: api: init --- api/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/redq/main.go | 11 ++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 api/main.go diff --git a/api/main.go b/api/main.go new file mode 100644 index 0000000..5ce05c1 --- /dev/null +++ b/api/main.go @@ -0,0 +1,46 @@ +package api + +import ( + "log" + "net" + + "sinanmohd.com/redq/usage" +) + +const sockPath = "/tmp/redq_ebpf.sock" + +type Api struct { + sock net.Listener +} + +func (a *Api) Init() error { + var err error + + a.sock, err = net.Listen("unix", sockPath) + if err != nil { + log.Printf("listening on unix socket: %s", err) + return err + } + + return nil +} + +func handleConn(conn net.Conn) { + defer conn.Close() +} + +func (a *Api) Run(u *usage.Usage) { + for { + conn, err := a.sock.Accept() + if err != nil { + log.Printf("accepting connection: %s", err) + continue + } + + go handleConn(conn) + } +} + +func (a *Api) CleanUp() { + a.sock.Close() +} diff --git a/cmd/redq/main.go b/cmd/redq/main.go index 4514efc..f368dae 100644 --- a/cmd/redq/main.go +++ b/cmd/redq/main.go @@ -10,11 +10,13 @@ import ( "github.com/jackc/pgx/v5" "sinanmohd.com/redq/db" + "sinanmohd.com/redq/api" "sinanmohd.com/redq/usage" ) func main() { var u usage.Usage + var a api.Api iface, err := net.InterfaceByName("wlan0") if err != nil { @@ -29,17 +31,24 @@ func main() { defer conn.Close(ctx) queries := db.New(conn) + err = a.Init() + if err != nil { + os.Exit(0) + } err = u.Init(iface) if err != nil { os.Exit(0) } + sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, os.Kill, syscall.SIGTERM) go func() { <-sigs u.CleanUp(queries, ctx) + a.CleanUp() os.Exit(0) }() - u.Run(iface, queries, ctx) + go u.Run(iface, queries, ctx) + a.Run(&u) } -- cgit v1.2.3