From eea30c731827ca1cb5239c7f123a8c3f4b8f9b1a Mon Sep 17 00:00:00 2001 From: Leyla Becker Date: Sat, 29 Mar 2025 19:06:07 -0500 Subject: [PATCH] set up project --- .gitignore | 9 +++++ flake.lock | 27 ++++++++++++++ flake.nix | 76 ++++++++++++++++++++++++++++++++++++++++ go.mod | 17 +++++++++ go.sum | 21 +++++++++++ shell.nix | 14 ++++++++ src/database/database.go | 29 +++++++++++++++ 7 files changed, 193 insertions(+) create mode 100644 .gitignore create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 go.mod create mode 100644 go.sum create mode 100644 shell.nix create mode 100644 src/database/database.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c329722 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ + +# development environment +.envrc +.direnv + +# postgres +postgres.db +postgres.log +.s.PGSQL.5432* diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2d674d0 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1742272065, + "narHash": "sha256-ud8vcSzJsZ/CK+r8/v0lyf4yUntVmDq6Z0A41ODfWbE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3549532663732bfd89993204d40543e9edaec4f2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..fbb608e --- /dev/null +++ b/flake.nix @@ -0,0 +1,76 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + }; + + outputs = { + self, + nixpkgs, + ... + }: let + # System types to support. + supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"]; + + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + + # Nixpkgs instantiated for supported system types. + nixpkgsFor = forAllSystems (system: import nixpkgs {inherit system;}); + in { + devShells = forAllSystems (system: let + pkgs = nixpkgsFor.${system}; + startPostgres = pkgs.writeScriptBin "postgres_start" '' + background() { + exec 0>&- + exec 1>&- + exec 2>&- + exec 3>&- + "$@" & + disown $! + } + + echo "Starting postgres server..." + # start postgres server + background ${pkgs.postgresql}/bin/pg_ctl -l $DB_LOG -o "--unix_socket_directories='$PWD'" start + echo "Postgres server started." + ''; + in { + default = pkgs.mkShell { + buildInputs = with pkgs; [go gopls gotools go-tools postgresql]; + + PGDATA = "postgres.db"; + DB_LOG = "postgres.log"; + DB_NAME = "vegan-barcode"; + shellHook = '' + echo "Using ${pkgs.postgresql.name}." + + create_db=0 + # initialize postgres if we have not done so already + if [ ! -d $PGDATA ]; then + echo "Postgres not found. Initializing postgres at $PGDATA" + ${pkgs.postgresql}/bin/initdb > /dev/null + + echo "Setting postgres unix socket directories to $PWD/$SOCKET_DIRECTORIES..." + echo "unix_socket_directories = '$PWD'" >> $PGDATA/postgresql.conf + echo "unix socket directories set." + + echo "Creating database $DB_NAME..." + echo "CREATE DATABASE \"$DB_NAME\";" | postgres --single -E postgres + echo "Database created." + fi + + export PGHOST="$PWD" + + if [ ! -f "$PWD/.s.PGSQL.5432.lock" ]; then + ${startPostgres}/bin/postgres_start + fi + ''; + }; + }); + + # The default package for 'nix build'. This makes sense if the + # flake provides only one package or there is a clear "main" + # package. + defaultPackage = forAllSystems (system: self.packages.${system}.go-hello); + }; +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..85b9814 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module vegan-barcode/backend + +go 1.24.1 + +require github.com/jackc/pgx v3.6.2+incompatible + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect +) + +require ( + github.com/jackc/pgx/v5 v5.7.4 + github.com/pkg/errors v0.9.1 // indirect + golang.org/x/crypto v0.36.0 // indirect + golang.org/x/text v0.23.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8c3bd84 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o= +github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= +github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg= +github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..d7c46b9 --- /dev/null +++ b/shell.nix @@ -0,0 +1,14 @@ +( + import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + {src = ./.;} +) +.shellNix diff --git a/src/database/database.go b/src/database/database.go new file mode 100644 index 0000000..18f695b --- /dev/null +++ b/src/database/database.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/jackc/pgx/v5" +) + +func main() { + // urlExample := "postgres://username:password@localhost:5432/database_name" + conn, err := pgx.Connect(context.Background(), os.Getenv("PGHOST")) + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err) + os.Exit(1) + } + defer conn.Close(context.Background()) + + var name string + var weight int64 + err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight) + if err != nil { + fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err) + os.Exit(1) + } + + fmt.Println(name, weight) +}