Initial commit

This commit is contained in:
Torjus Håkestad 2024-03-06 13:59:27 +01:00
commit 63a1e0c3d7
10 changed files with 216 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/gomod2nix-template

23
default.nix Normal file
View File

@ -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 ];
}

85
flake.lock Normal file
View File

@ -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
}

29
flake.nix Normal file
View File

@ -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;
};
})
);
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.t-juice.club/torjus/notlistener
go 1.21.6
require github.com/codegoalie/golibnotify v0.1.0 // indirect

2
go.sum Normal file
View File

@ -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=

6
gomod2nix.toml Normal file
View File

@ -0,0 +1,6 @@
schema = 3
[mod]
[mod."github.com/codegoalie/golibnotify"]
version = "v0.1.0"
hash = "sha256-PwdomBgGbBhNz5F479NX0i9kGlpW/AreuvaJt12wV8Y="

38
main.go Normal file
View File

@ -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)
}
}

1
server/server.go Normal file
View File

@ -0,0 +1 @@
package server

26
shell.nix Normal file
View File

@ -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
];
}