Basic flake with go http server and debugging

This commit is contained in:
Zuckerberg 2023-05-09 23:01:25 -06:00
parent e9ded62523
commit 8a3022477b
9 changed files with 155 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
result

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"nixEnvSelector.nixFile": "${workspaceRoot}/shell.nix"
}

61
flake.lock generated Normal file
View File

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

23
flake.nix Normal file
View File

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

10
server/default.nix Normal file
View File

@ -0,0 +1,10 @@
{ pkgs, ... }:
pkgs.buildGoModule rec {
pname = "dynamic-frame-server";
version = "0.0.1";
src = ./.;
vendorSha256 = "uTqGdqswbnmPsEm5NISD0Nh9ry4GLMAsuVF7+U8BRdA=";
}

5
server/go.mod Normal file
View File

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

2
server/go.sum Normal file
View File

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

38
server/main.go Normal file
View File

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

12
shell.nix Normal file
View File

@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell {
buildInputs = [
go
];
# Disabling hardening is required for go debugger to work. Not needed for packaging.
NIX_HARDENING_ENABLE = "";
}