From 424b8367414a9f5ed437523d7a1144bd5f968a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Mon, 9 Jun 2025 20:38:59 +0200 Subject: [PATCH] Add notification lib --- .envrc | 1 + .gitignore | 1 + flake.lock | 27 +++++++++++++++++++++ flake.nix | 41 +++++++++++++++++++++++++++++++ go.mod | 15 ++++++++++++ go.sum | 12 +++++++++ notify/notify.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 160 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 go.mod create mode 100644 go.sum create mode 100644 notify/notify.go diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b42106 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.direnv/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..cd2b62f --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1749285348, + "narHash": "sha256-frdhQvPbmDYaScPFiCnfdh3B/Vh81Uuoo0w5TkWmmjU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3e3afe5174c561dee0df6f2c2b2236990146329f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4a00a7a --- /dev/null +++ b/flake.nix @@ -0,0 +1,41 @@ +{ + description = "Libraries for use in my homelab"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + + outputs = + { self, nixpkgs }: + let + allSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + forAllSystems = + f: + nixpkgs.lib.genAttrs allSystems ( + system: + f { + pkgs = import nixpkgs { inherit system; }; + } + ); + in + { + overlays.default = final: prev: { + ghettoptt = self.packages.${prev.system}.default; + }; + + devShells = forAllSystems ( + { pkgs }: + { + default = pkgs.mkShell { + packages = with pkgs; [ + go + golangci-lint + ]; + }; + } + ); + }; +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a66e30a --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module git.t-juice.club/torjus/libhlab + +go 1.24.3 + +require ( + github.com/nats-io/nats.go v1.43.0 + github.com/nats-io/nkeys v0.4.11 +) + +require ( + github.com/klauspost/compress v1.18.0 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + golang.org/x/crypto v0.37.0 // indirect + golang.org/x/sys v0.32.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..444f7e7 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/nats-io/nats.go v1.43.0 h1:uRFZ2FEoRvP64+UUhaTokyS18XBCR/xM2vQZKO4i8ug= +github.com/nats-io/nats.go v1.43.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g= +github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0= +github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE= +github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= +golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= +golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= +golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/notify/notify.go b/notify/notify.go new file mode 100644 index 0000000..20abd9b --- /dev/null +++ b/notify/notify.go @@ -0,0 +1,63 @@ +package notify + +import ( + "encoding/json" + "fmt" + "time" + + "github.com/nats-io/nats.go" + "github.com/nats-io/nkeys" +) + +const DEFAULT_SUBJECT = "home2rjusnet.notifications" + +type BusNotification struct { + ID uint32 `json:"id,omitempty"` + Summary string `json:"summary"` + Body string `json:"body,omitempty"` + Timeout time.Duration `json:"timeout,omitempty"` +} + +type NotificationService struct { + Subject string + nc *nats.Conn +} + +func NewNotificationService(url, nkey string) (*NotificationService, error) { + kp, err := nkeys.FromSeed([]byte(nkey)) + if err != nil { + return nil, fmt.Errorf("error creating nkey: %v", err) + } + + pub, err := kp.PublicKey() + if err != nil { + return nil, fmt.Errorf("error getting public key: %v", err) + } + + opt := nats.Nkey(pub, kp.Sign) + + nc, err := nats.Connect(url, opt) + if err != nil { + return nil, fmt.Errorf("error connecting to nats: %v", err) + } + + return &NotificationService{nc: nc, Subject: DEFAULT_SUBJECT}, nil +} + +func (n *NotificationService) Notify(msg *BusNotification) error { + data, err := json.Marshal(msg) + if err != nil { + return fmt.Errorf("error marshalling notification: %v", err) + } + + err = n.nc.Publish(n.Subject, data) + if err != nil { + return fmt.Errorf("error publishing notification: %v", err) + } + + return nil +} + +func (n *NotificationService) Close() { + n.nc.Close() +}