From 8a3022477bbe9beb59c8c2874138b8b50dc24b05 Mon Sep 17 00:00:00 2001 From: Zuckerberg Date: Tue, 9 May 2023 23:01:25 -0600 Subject: [PATCH] Basic flake with go http server and debugging --- .gitignore | 1 + .vscode/settings.json | 3 +++ flake.lock | 61 +++++++++++++++++++++++++++++++++++++++++++ flake.nix | 23 ++++++++++++++++ server/default.nix | 10 +++++++ server/go.mod | 5 ++++ server/go.sum | 2 ++ server/main.go | 38 +++++++++++++++++++++++++++ shell.nix | 12 +++++++++ 9 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 server/default.nix create mode 100644 server/go.mod create mode 100644 server/go.sum create mode 100644 server/main.go create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2be92b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +result diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fdccc76 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "nixEnvSelector.nixFile": "${workspaceRoot}/shell.nix" +} \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..dc2a70f --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1683627095, + "narHash": "sha256-8u9SejRpL2TrMuHBdhYh4FKc1OGPDLyWTpIbNTtoHsA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a08e061a4ee8329747d54ddf1566d34c55c895eb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-22.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "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/flake.nix b/flake.nix new file mode 100644 index 0000000..34e31bc --- /dev/null +++ b/flake.nix @@ -0,0 +1,23 @@ +{ + description = "A simple Go project built with Nix"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + server = pkgs.callPackage ./server/default.nix { }; + in + { + packages."dynamic-frame-server" = server; + packages.default = server; + devShell = pkgs.mkShell { + buildInputs = [ pkgs.go ]; + }; + } + ); +} diff --git a/server/default.nix b/server/default.nix new file mode 100644 index 0000000..5384ce4 --- /dev/null +++ b/server/default.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: + +pkgs.buildGoModule rec { + pname = "dynamic-frame-server"; + version = "0.0.1"; + + src = ./.; + + vendorSha256 = "uTqGdqswbnmPsEm5NISD0Nh9ry4GLMAsuVF7+U8BRdA="; +} diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..cebeea4 --- /dev/null +++ b/server/go.mod @@ -0,0 +1,5 @@ +module git.neet.dev/zuckerberg/dynamic-frame/server + +go 1.19 + +require github.com/go-chi/chi/v5 v5.0.8 diff --git a/server/go.sum b/server/go.sum new file mode 100644 index 0000000..2ad8d91 --- /dev/null +++ b/server/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= +github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..e00344b --- /dev/null +++ b/server/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/go-chi/chi/v5" +) + +// Define a function to handle incoming requests +func requestHandler(w http.ResponseWriter, r *http.Request) { + // Get the 'name' variable from the request + name := chi.URLParam(r, "name") + + // Use a default value if 'name' is not present + if name == "" { + name = "World" + } + + // Respond with a greeting message + fmt.Fprintf(w, "Hello, %s!\n", name) +} + +func main() { + fmt.Println("Hello, Nix!") + + // Create a new Chi router + router := chi.NewRouter() + + // Register the requestHandler function to handle requests at the root path + // and a path with the 'name' parameter + router.Get("/", requestHandler) + router.Get("/{name}", requestHandler) + + // Start the HTTP server on port 8080 and log any errors + log.Fatal(http.ListenAndServe(":8080", router)) +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..3dd8680 --- /dev/null +++ b/shell.nix @@ -0,0 +1,12 @@ +{ pkgs ? import { } }: + +with pkgs; + +mkShell { + buildInputs = [ + go + ]; + + # Disabling hardening is required for go debugger to work. Not needed for packaging. + NIX_HARDENING_ENABLE = ""; +}