diff options
author | Emile <git@emile.space> | 2025-02-12 21:24:31 +0100 |
---|---|---|
committer | Emile <git@emile.space> | 2025-02-12 21:24:31 +0100 |
commit | c0a8852e4ec21f15c5a862201515518c3eee7734 (patch) | |
tree | eb906ede104475df681d3b61ecae234d5bb67b62 | |
parent | 2e1c7e7c033a8d1819c65a65dbed71f884e2fec1 (diff) |
template: a basic golang app template
This template allows building golang apps as well as a corresponding docker container from the built package
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | nix/templates/goapp/README.md | 39 | ||||
-rw-r--r-- | nix/templates/goapp/backend/default.nix | 21 | ||||
-rw-r--r-- | nix/templates/goapp/backend/go.mod | 5 | ||||
-rw-r--r-- | nix/templates/goapp/backend/go.sum | 2 | ||||
-rw-r--r-- | nix/templates/goapp/backend/src/main.go | 29 | ||||
-rw-r--r-- | nix/templates/goapp/flake.lock | 62 | ||||
-rw-r--r-- | nix/templates/goapp/flake.nix | 52 | ||||
-rw-r--r-- | nix/templates/goapp/frontend/default.nix | 21 | ||||
-rw-r--r-- | nix/templates/goapp/frontend/go.mod | 5 | ||||
-rw-r--r-- | nix/templates/goapp/frontend/go.sum | 2 | ||||
-rw-r--r-- | nix/templates/goapp/frontend/src/main.go | 29 |
12 files changed, 268 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore index e43b0f9..d5fec98 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +result diff --git a/nix/templates/goapp/README.md b/nix/templates/goapp/README.md new file mode 100644 index 0000000..e819496 --- /dev/null +++ b/nix/templates/goapp/README.md @@ -0,0 +1,39 @@ +# goapp + +A template for a go app. + +```bash +; nix flake show +warning: Git tree '/Users/emile/hefe' is dirty +git+file:///Users/emile/hefe?dir=nix/templates/goapp +├───devShells +│ ├───aarch64-darwin +│ │ └───default: development environment 'nix-shell' +│ ├───aarch64-linux +│ │ └───default omitted (use '--all-systems' to show) +│ ├───x86_64-darwin +│ │ └───default omitted (use '--all-systems' to show) +│ └───x86_64-linux +│ └───default omitted (use '--all-systems' to show) +└───packages + ├───aarch64-darwin + │ ├───backend: package 'backend-0.0.1' + │ ├───backend-docker: package 'docker-image-backend.tar.gz' + │ ├───frontend: package 'frontend-0.0.1' + │ └───frontend-docker: package 'docker-image-frontend.tar.gz' + ├───aarch64-linux + │ ├───backend omitted (use '--all-systems' to show) + │ ├───backend-docker omitted (use '--all-systems' to show) + │ ├───frontend omitted (use '--all-systems' to show) + │ └───frontend-docker omitted (use '--all-systems' to show) + ├───x86_64-darwin + │ ├───backend omitted (use '--all-systems' to show) + │ ├───backend-docker omitted (use '--all-systems' to show) + │ ├───frontend omitted (use '--all-systems' to show) + │ └───frontend-docker omitted (use '--all-systems' to show) + └───x86_64-linux + ├───backend omitted (use '--all-systems' to show) + ├───backend-docker omitted (use '--all-systems' to show) + ├───frontend omitted (use '--all-systems' to show) + └───frontend-docker omitted (use '--all-systems' to show) +``` diff --git a/nix/templates/goapp/backend/default.nix b/nix/templates/goapp/backend/default.nix new file mode 100644 index 0000000..1156621 --- /dev/null +++ b/nix/templates/goapp/backend/default.nix @@ -0,0 +1,21 @@ +{ pkgs, packagename, ... }: + +let + version = "0.0.1"; +in +pkgs.buildGoModule { + name = "${packagename}-${version}"; + pname = "${packagename}"; + version = "${version}"; + + src = ./.; + subPackages = [ "src" ]; + vendorHash = "sha256-8wYERVt3PIsKkarkwPu8Zy/Sdx43P6g2lz2xRfvTZ2E="; + + postInstall = '' + mkdir -p $out + mv $out/bin/src $out/bin/${packagename} + ''; + + doCheck = true; +} diff --git a/nix/templates/goapp/backend/go.mod b/nix/templates/goapp/backend/go.mod new file mode 100644 index 0000000..41401c9 --- /dev/null +++ b/nix/templates/goapp/backend/go.mod @@ -0,0 +1,5 @@ +module github.com/hanemile/goapp/backend + +go 1.23.5 + +require github.com/gorilla/mux v1.8.1 diff --git a/nix/templates/goapp/backend/go.sum b/nix/templates/goapp/backend/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/nix/templates/goapp/backend/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/nix/templates/goapp/backend/src/main.go b/nix/templates/goapp/backend/src/main.go new file mode 100644 index 0000000..b9d9214 --- /dev/null +++ b/nix/templates/goapp/backend/src/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World from the backend") +} + +func main() { + r := mux.NewRouter() + r.HandleFunc("/", indexHandler) + + srv := &http.Server{ + Handler: r, + Addr: ":8080", + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Printf("[i] Running the server on %s", srv.Addr) + log.Fatal(srv.ListenAndServe()) +} diff --git a/nix/templates/goapp/flake.lock b/nix/templates/goapp/flake.lock new file mode 100644 index 0000000..bde8278 --- /dev/null +++ b/nix/templates/goapp/flake.lock @@ -0,0 +1,62 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "ref": "refs/heads/main", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "revCount": 102, + "type": "git", + "url": "https://github.com/numtide/flake-utils" + }, + "original": { + "type": "git", + "url": "https://github.com/numtide/flake-utils" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1739380326, + "narHash": "sha256-rsuM666LNSFbJXyAAoy6IjZ1nAMUtxyNL4qU4ji2lQQ=", + "ref": "refs/heads/master", + "rev": "911b3572a0dd4978144f7b2ca54ce51e79013d83", + "revCount": 752360, + "shallow": false, + "type": "git", + "url": "https://github.com/nixos/nixpkgs.git" + }, + "original": { + "shallow": false, + "type": "git", + "url": "https://github.com/nixos/nixpkgs.git" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "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/nix/templates/goapp/flake.nix b/nix/templates/goapp/flake.nix new file mode 100644 index 0000000..414acb8 --- /dev/null +++ b/nix/templates/goapp/flake.nix @@ -0,0 +1,52 @@ +{ + inputs.nixpkgs.url = "git+https://github.com/nixos/nixpkgs.git?shallow=1?ref=nixos-24.11"; + inputs.flake-utils.url = "git+https://github.com/numtide/flake-utils"; + + outputs = + { nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ ]; + }; + + package-and-docker = packagename: { + # the raw package + "${packagename}" = import ./${packagename} { inherit pkgs packagename; }; + + # the docker image + "${packagename}-docker" = pkgs.dockerTools.buildImage { + name = "${packagename}"; + config.Cmd = [ "${packagename}/bin/${packagename}" ]; + }; + }; + in + { + packages = { } // (package-and-docker "backend") // (package-and-docker "frontend"); + + devShells.default = pkgs.mkShell { + buildInputs = builtins.attrValues { + inherit (pkgs) + go + gopls + helix + ripgrep + fd + tokei + tree + + eza + ; + }; + + shellHook = '' + alias ls=eza + echo "goapp shell" + export PS1='>; ' + ''; + }; + } + ); +} diff --git a/nix/templates/goapp/frontend/default.nix b/nix/templates/goapp/frontend/default.nix new file mode 100644 index 0000000..1156621 --- /dev/null +++ b/nix/templates/goapp/frontend/default.nix @@ -0,0 +1,21 @@ +{ pkgs, packagename, ... }: + +let + version = "0.0.1"; +in +pkgs.buildGoModule { + name = "${packagename}-${version}"; + pname = "${packagename}"; + version = "${version}"; + + src = ./.; + subPackages = [ "src" ]; + vendorHash = "sha256-8wYERVt3PIsKkarkwPu8Zy/Sdx43P6g2lz2xRfvTZ2E="; + + postInstall = '' + mkdir -p $out + mv $out/bin/src $out/bin/${packagename} + ''; + + doCheck = true; +} diff --git a/nix/templates/goapp/frontend/go.mod b/nix/templates/goapp/frontend/go.mod new file mode 100644 index 0000000..41401c9 --- /dev/null +++ b/nix/templates/goapp/frontend/go.mod @@ -0,0 +1,5 @@ +module github.com/hanemile/goapp/backend + +go 1.23.5 + +require github.com/gorilla/mux v1.8.1 diff --git a/nix/templates/goapp/frontend/go.sum b/nix/templates/goapp/frontend/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/nix/templates/goapp/frontend/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/nix/templates/goapp/frontend/src/main.go b/nix/templates/goapp/frontend/src/main.go new file mode 100644 index 0000000..adb15cd --- /dev/null +++ b/nix/templates/goapp/frontend/src/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World from the frontend") +} + +func main() { + r := mux.NewRouter() + r.HandleFunc("/", indexHandler) + + srv := &http.Server{ + Handler: r, + Addr: ":8080", + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Printf("[i] Running the server on %s", srv.Addr) + log.Fatal(srv.ListenAndServe()) +} |