From 63a1e0c3d749febd971a3d0164cf8477bf6fd047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Wed, 6 Mar 2024 13:59:27 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + default.nix | 23 +++++++++++++ flake.lock | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 29 +++++++++++++++++ go.mod | 5 +++ go.sum | 2 ++ gomod2nix.toml | 6 ++++ main.go | 38 ++++++++++++++++++++++ server/server.go | 1 + shell.nix | 26 +++++++++++++++ 10 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 go.mod create mode 100644 go.sum create mode 100644 gomod2nix.toml create mode 100644 main.go create mode 100644 server/server.go create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dfba653 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/gomod2nix-template diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..796fccd --- /dev/null +++ b/default.nix @@ -0,0 +1,23 @@ +{ pkgs ? ( + let + inherit (builtins) fetchTree fromJSON readFile; + inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix; + in + import (fetchTree nixpkgs.locked) { + overlays = [ + (import "${fetchTree gomod2nix.locked}/overlay.nix") + ]; + } + ) +, buildGoApplication ? pkgs.buildGoApplication +}: + +buildGoApplication { + pname = "notlistener"; + version = "0.1.0"; + pwd = ./.; + src = ./.; + modules = ./gomod2nix.toml; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ pkgs.libnotify ]; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2461ddb --- /dev/null +++ b/flake.lock @@ -0,0 +1,85 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1709126324, + "narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "d465f4819400de7c8d874d50b982301f28a84605", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gomod2nix": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1705314449, + "narHash": "sha256-yfQQ67dLejP0FLK76LKHbkzcQqNIrux6MFe32MMFGNQ=", + "owner": "nix-community", + "repo": "gomod2nix", + "rev": "30e3c3a9ec4ac8453282ca7f67fca9e1da12c3e6", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "gomod2nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1709479366, + "narHash": "sha256-n6F0n8UV6lnTZbYPl1A9q1BS0p4hduAv1mGAP17CVd0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b8697e57f10292a6165a20f03d2f42920dfaf973", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "gomod2nix": "gomod2nix", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..c5a32cb --- /dev/null +++ b/flake.nix @@ -0,0 +1,29 @@ +{ + description = "A small listener for notifications"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.gomod2nix.url = "github:nix-community/gomod2nix"; + inputs.gomod2nix.inputs.nixpkgs.follows = "nixpkgs"; + inputs.gomod2nix.inputs.flake-utils.follows = "flake-utils"; + + outputs = { self, nixpkgs, flake-utils, gomod2nix }: + (flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + # The current default sdk for macOS fails to compile go projects, so we use a newer one for now. + # This has no effect on other platforms. + callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage; + in + { + packages.default = callPackage ./. { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; + }; + devShells.default = callPackage ./shell.nix { + inherit (gomod2nix.legacyPackages.${system}) mkGoEnv gomod2nix; + }; + }) + ); +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a001e85 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.t-juice.club/torjus/notlistener + +go 1.21.6 + +require github.com/codegoalie/golibnotify v0.1.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ebb34f4 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/codegoalie/golibnotify v0.1.0 h1:klJMgE+elOsuTLxFvZN+0q1XFgYGr6aT5RyIBb2dZ0c= +github.com/codegoalie/golibnotify v0.1.0/go.mod h1:+v6J4ss13rISdS08ENaHtBlxYOyIAPXVEUM4KyRYpoY= diff --git a/gomod2nix.toml b/gomod2nix.toml new file mode 100644 index 0000000..8cdfff7 --- /dev/null +++ b/gomod2nix.toml @@ -0,0 +1,6 @@ +schema = 3 + +[mod] + [mod."github.com/codegoalie/golibnotify"] + version = "v0.1.0" + hash = "sha256-PwdomBgGbBhNz5F479NX0i9kGlpW/AreuvaJt12wV8Y=" diff --git a/main.go b/main.go new file mode 100644 index 0000000..c9efc67 --- /dev/null +++ b/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + + "github.com/codegoalie/golibnotify" +) + +type Notify struct { + Summary string `json:"summary"` + Body string `json:"body"` + Icon string `json:"icon"` +} + +func main() { + mux := http.NewServeMux() + mux.HandleFunc("/api/notify", func(w http.ResponseWriter, r *http.Request) { + var n Notify + defer r.Body.Close() + + if err := json.NewDecoder(r.Body).Decode(&n); err != nil { + w.WriteHeader(http.StatusBadRequest) + } + + // Send the notification + sn := golibnotify.NewSimpleNotifier("notlistener") + if err := sn.Show(n.Summary, n.Body, n.Icon); err != nil { + log.Printf("Unable to send notification: %v", err) + } + }) + + log.Printf("Starting server on :8080") + if err := http.ListenAndServe(":8080", mux); err != nil { + log.Printf("Error starting listener: %v", err) + } +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..abb4e43 --- /dev/null +++ b/server/server.go @@ -0,0 +1 @@ +package server diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..fc9af16 --- /dev/null +++ b/shell.nix @@ -0,0 +1,26 @@ +{ pkgs ? ( + let + inherit (builtins) fetchTree fromJSON readFile; + inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix; + in + import (fetchTree nixpkgs.locked) { + overlays = [ + (import "${fetchTree gomod2nix.locked}/overlay.nix") + ]; + } + ) +, mkGoEnv ? pkgs.mkGoEnv +, gomod2nix ? pkgs.gomod2nix +}: + +let + goEnv = mkGoEnv { pwd = ./.; }; +in +pkgs.mkShell { + packages = [ + goEnv + gomod2nix + pkgs.libnotify + pkgs.pkg-config + ]; +}