commit 8a9cc4a33a1eb7d810c4079bdfacbd9a36f2640b Author: Christine Dodrill Date: Mon Apr 13 11:32:12 2020 -0400 envspew example diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..be81fed --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +eval "$(lorri direnv)" \ No newline at end of file diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..210f159 --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "encoding/json" + "net/http" + "os" + "time" +) + +type config map[string]interface{} + +func envOr(name, val string) string { + if result := os.Getenv(name); result != "" { + return result + } + + return val +} + +func loadConfig(cfg config) error { + fin, err := os.Open(envOr("CONFIG_PATH", "./envspew.json")) + if err != nil { + return err + } + defer fin.Close() + + return json.NewDecoder(fin).Decode(&cfg) +} + +func main() { + cfg := config{} + loadConfig(cfg) + cfg["now"] = time.Now().String() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(cfg) + }) + + http.ListenAndServe(":"+envOr("PORT", "9001"), nil) +} diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..db5dd33 --- /dev/null +++ b/default.nix @@ -0,0 +1,18 @@ +{sources ? import ./nix/sources.nix, pkgs ? import sources.nixpkgs { } }: + +let + envspew = pkgs.buildGoPackage { + name = "envspew"; + goPackagePath = "tulpa.dev/cadey/envspew"; + src = ./.; + allowGoReference = false; + preBuild = '' + export CGO_ENABLED=0 + buildFlagsArray+=(-pkgdir "$TMPDIR") + ''; + }; +in pkgs.dockerTools.buildLayeredImage { + name = "xena/envspew"; + contents = [ envspew ]; + config.Cmd = [ "/bin/web" ]; +} diff --git a/envspew.json b/envspew.json new file mode 100644 index 0000000..040f533 --- /dev/null +++ b/envspew.json @@ -0,0 +1 @@ +{"TODAY": "is gonna be the day", "THAT_IM": "gonna bring it back to you"} diff --git a/envspew.yml b/envspew.yml new file mode 100644 index 0000000..54b32bd --- /dev/null +++ b/envspew.yml @@ -0,0 +1,80 @@ +kind: Namespace +apiVersion: v1 +metadata: + name: envspew +--- +kind: ConfigMap +apiVersion: v1 +metadata: + name: envspew + namespace: envspew +data: + config.json: | + { + "WERE_NO": "strangers to love", + "YOU_KNOW": "the rules and so do i" + } +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: envspew + namespace: envspew + annotations: + reloader.stakater.com/auto: "true" +spec: + replicas: 2 + selector: + matchLabels: + app: envspew + template: + metadata: + labels: + app: envspew + spec: + volumes: + - name: config + configMap: + name: envspew + containers: + - name: web + image: xena/envspew:n2ws8s1i02j0n14xxz7993sxb5ff382n + ports: + - containerPort: 9001 + env: + - name: CONFIG_PATH + value: /cfg/config.json + volumeMounts: + - name: config + mountPath: /cfg +--- +kind: Service +apiVersion: v1 +metadata: + name: envspew + namespace: envspew + labels: + app: envspew +spec: + ports: + - port: 9001 + targetPort: 9001 +--- +kind: Ingress +apiVersion: networking.k8s.io/v1beta1 +metadata: + name: envspew + namespace: envspew + labels: + app: envspew + annotations: + kubernetes.io/ingress.class: nginx +spec: + rules: + - host: envspew.local.cetacean.club + http: + paths: + - backend: + serviceName: envspew + servicePort: 9001 + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e6c8f90 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module tulpa.dev/cadey/envspew + +go 1.14 + +require github.com/rogpeppe/go-internal v1.5.2 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..49dd7e7 --- /dev/null +++ b/go.sum @@ -0,0 +1,7 @@ +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/rogpeppe/go-internal v1.5.2 h1:qLvObTrvO/XRCqmkKxUlOBc48bI3efyDuAZe25QiF0w= +github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/nix/sources.json b/nix/sources.json new file mode 100644 index 0000000..297fa54 --- /dev/null +++ b/nix/sources.json @@ -0,0 +1,38 @@ +{ + "niv": { + "branch": "master", + "description": "Easy dependency management for Nix projects", + "homepage": "https://github.com/nmattia/niv", + "owner": "nmattia", + "repo": "niv", + "rev": "f73bf8d584148677b01859677a63191c31911eae", + "sha256": "0jlmrx633jvqrqlyhlzpvdrnim128gc81q5psz2lpp2af8p8q9qs", + "type": "tarball", + "url": "https://github.com/nmattia/niv/archive/f73bf8d584148677b01859677a63191c31911eae.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, + "nixpkgs": { + "branch": "nixos-20.03", + "description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to", + "homepage": "https://github.com/NixOS/nixpkgs", + "owner": "NixOS", + "repo": "nixpkgs-channels", + "rev": "29eddfc36d720dcc4822581175217543b387b1e8", + "sha256": "1gqv2m7plkladd3va664xyqb962pqs4pizzibvkm1nh0f4rfpxvy", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs-channels/archive/29eddfc36d720dcc4822581175217543b387b1e8.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, + "xepkgs": { + "branch": "master", + "description": "Xe's packages", + "homepage": "", + "owner": "Xe", + "repo": "xepkgs", + "rev": "b781bddd6a0f0db266637431a4663150ed5c1927", + "sha256": "0dbxy3sgndb3934ci39ib18pz5zi53cfhd75p6c9gr3fy4lni7qk", + "type": "tarball", + "url": "https://github.com/Xe/xepkgs/archive/b781bddd6a0f0db266637431a4663150ed5c1927.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + } +} diff --git a/nix/sources.nix b/nix/sources.nix new file mode 100644 index 0000000..8a725cb --- /dev/null +++ b/nix/sources.nix @@ -0,0 +1,134 @@ +# This file has been generated by Niv. + +let + + # + # The fetchers. fetch_ fetches specs of type . + # + + fetch_file = pkgs: spec: + if spec.builtin or true then + builtins_fetchurl { inherit (spec) url sha256; } + else + pkgs.fetchurl { inherit (spec) url sha256; }; + + fetch_tarball = pkgs: spec: + if spec.builtin or true then + builtins_fetchTarball { inherit (spec) url sha256; } + else + pkgs.fetchzip { inherit (spec) url sha256; }; + + fetch_git = spec: + builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; }; + + fetch_builtin-tarball = spec: + builtins.trace + '' + WARNING: + The niv type "builtin-tarball" will soon be deprecated. You should + instead use `builtin = true`. + + $ niv modify -a type=tarball -a builtin=true + '' + builtins_fetchTarball { inherit (spec) url sha256; }; + + fetch_builtin-url = spec: + builtins.trace + '' + WARNING: + The niv type "builtin-url" will soon be deprecated. You should + instead use `builtin = true`. + + $ niv modify -a type=file -a builtin=true + '' + (builtins_fetchurl { inherit (spec) url sha256; }); + + # + # Various helpers + # + + # The set of packages used when specs are fetched using non-builtins. + mkPkgs = sources: + let + sourcesNixpkgs = + import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {}; + hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; + hasThisAsNixpkgsPath = == ./.; + in + if builtins.hasAttr "nixpkgs" sources + then sourcesNixpkgs + else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then + import {} + else + abort + '' + Please specify either (through -I or NIX_PATH=nixpkgs=...) or + add a package called "nixpkgs" to your sources.json. + ''; + + # The actual fetching function. + fetch = pkgs: name: spec: + + if ! builtins.hasAttr "type" spec then + abort "ERROR: niv spec ${name} does not have a 'type' attribute" + else if spec.type == "file" then fetch_file pkgs spec + else if spec.type == "tarball" then fetch_tarball pkgs spec + else if spec.type == "git" then fetch_git spec + else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec + else if spec.type == "builtin-url" then fetch_builtin-url spec + else + abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; + + # Ports of functions for older nix versions + + # a Nix version of mapAttrs if the built-in doesn't exist + mapAttrs = builtins.mapAttrs or ( + f: set: with builtins; + listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) + ); + + # fetchTarball version that is compatible between all the versions of Nix + builtins_fetchTarball = { url, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchTarball; + in + if lessThan nixVersion "1.12" then + fetchTarball { inherit url; } + else + fetchTarball attrs; + + # fetchurl version that is compatible between all the versions of Nix + builtins_fetchurl = { url, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchurl; + in + if lessThan nixVersion "1.12" then + fetchurl { inherit url; } + else + fetchurl attrs; + + # Create the final "sources" from the config + mkSources = config: + mapAttrs ( + name: spec: + if builtins.hasAttr "outPath" spec + then abort + "The values in sources.json should not have an 'outPath' attribute" + else + spec // { outPath = fetch config.pkgs name spec; } + ) config.sources; + + # The "config" used by the fetchers + mkConfig = + { sourcesFile ? ./sources.json + , sources ? builtins.fromJSON (builtins.readFile sourcesFile) + , pkgs ? mkPkgs sources + }: rec { + # The sources, i.e. the attribute set of spec name to spec + inherit sources; + + # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers + inherit pkgs; + }; +in +mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..f065ca2 --- /dev/null +++ b/shell.nix @@ -0,0 +1,14 @@ +let + sources = import ./nix/sources.nix; + pkgs = import sources.nixpkgs { }; + xepkgs = import sources.xepkgs { inherit pkgs; }; +in pkgs.mkShell { + buildInputs = with pkgs; [ + go + goimports + kubectl + dive + + xepkgs.gopls + ]; +}