From 4f7b2fcd1738df39fbee920ede8b45cf8313732a Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 10 Mar 2024 19:24:30 +0000 Subject: [PATCH 1/5] init --- .envrc | 1 + .gitignore | 1 + flake.lock | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 24 ++++++++++++++++++++++ flake.nix~ | 0 go.mod | 3 +++ 6 files changed, 89 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 flake.nix~ create mode 100644 go.mod diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29963da --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.direnv/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ca0a864 --- /dev/null +++ b/flake.lock @@ -0,0 +1,60 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1709126324, + "narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "d465f4819400de7c8d874d50b982301f28a84605", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1710097495, + "narHash": "sha256-B7Ea7q7hU7SE8wOPJ9oXEBjvB89yl2csaLjf5v/7jr8=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d40e866b1f98698d454dad8f592fe7616ff705a4", + "type": "github" + }, + "original": { + "owner": "nixos", + "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..11744c9 --- /dev/null +++ b/flake.nix @@ -0,0 +1,24 @@ +{ + description = "templ-practice"; + inputs.nixpkgs.url = "github:nixos/nixpkgs"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let pkgs = nixpkgs.legacyPackages.${system}; + in { + devShells.default = pkgs.mkShell { + buildInputs = [ + pkgs.go + pkgs.wgo + pkgs.semgrep + pkgs.gopls + pkgs.gnumake + ]; + shellHook = '' + export SOME_SHELL_VAR=ITS_VALUE + ''; + }; + }); + # see https://serokell.io/blog/practical-nix-flakes +} diff --git a/flake.nix~ b/flake.nix~ new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..eef8b4b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module sunshine.industries/temp-exercise + +go 1.21.7 From 81235e3ce6465b848f8778e5eaaff9c0bd863dd7 Mon Sep 17 00:00:00 2001 From: efim Date: Mon, 11 Mar 2024 04:38:48 +0000 Subject: [PATCH 2/5] feat: enabling templ --- .gitignore | 3 ++ flake.lock | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++- flake.nix | 26 ++++----- go.mod | 2 + go.sum | 2 + hello.templ | 5 ++ main.go | 16 ++++++ 7 files changed, 193 insertions(+), 14 deletions(-) create mode 100644 go.sum create mode 100644 hello.templ create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 29963da..11129db 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /.direnv/ +/.go/ +*~ +*_templ.go \ No newline at end of file diff --git a/flake.lock b/flake.lock index ca0a864..d8ba0cb 100644 --- a/flake.lock +++ b/flake.lock @@ -18,6 +18,82 @@ "type": "github" } }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_3": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "templ", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1694102001, + "narHash": "sha256-vky6VPK1n1od6vXbqzOXnekrQpTL4hbPAwUhT5J9c9E=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "9e21c80adf67ebcb077d75bd5e7d724d21eeafd6", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "gomod2nix": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": [ + "templ", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1705314449, + "narHash": "sha256-yfQQ67dLejP0FLK76LKHbkzcQqNIrux6MFe32MMFGNQ=", + "owner": "nix-community", + "repo": "gomod2nix", + "rev": "30e3c3a9ec4ac8453282ca7f67fca9e1da12c3e6", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "gomod2nix", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1710097495, @@ -33,10 +109,27 @@ "type": "github" } }, + "nixpkgs_2": { + "locked": { + "lastModified": 1701282334, + "narHash": "sha256-MxCVrXY6v4QmfTwIysjjaX0XUhqBbxTWWB4HXtDYsdk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "057f9aecfb71c4437d2b27d3323df7f93c010b7e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "23.11", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "templ": "templ" } }, "systems": { @@ -53,6 +146,64 @@ "repo": "default", "type": "github" } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "templ": { + "inputs": { + "gitignore": "gitignore", + "gomod2nix": "gomod2nix", + "nixpkgs": "nixpkgs_2", + "xc": "xc" + }, + "locked": { + "lastModified": 1709917943, + "narHash": "sha256-zDQxUFSmG/VX+xtK+nZ3ObRMVcMjjx+EUAxHLNcHHF8=", + "owner": "a-h", + "repo": "templ", + "rev": "df2a32403bb4a8e4745ac10ddc8b3e77386d8045", + "type": "github" + }, + "original": { + "owner": "a-h", + "repo": "templ", + "type": "github" + } + }, + "xc": { + "inputs": { + "flake-utils": "flake-utils_3", + "nixpkgs": [ + "templ", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1703164129, + "narHash": "sha256-kCcCqqwvjN07H8FPG4tXsRVRcMqT8dUNt9pwW1kKAe8=", + "owner": "joerdav", + "repo": "xc", + "rev": "0655cccfcf036556aeaddfb8f45dc7e8dd1b3680", + "type": "github" + }, + "original": { + "owner": "joerdav", + "repo": "xc", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 11744c9..05cdf9e 100644 --- a/flake.nix +++ b/flake.nix @@ -1,22 +1,22 @@ -{ +rec { description = "templ-practice"; - inputs.nixpkgs.url = "github:nixos/nixpkgs"; - inputs.flake-utils.url = "github:numtide/flake-utils"; - - outputs = { self, nixpkgs, flake-utils }: + inputs = { + nixpkgs.url = "github:nixos/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + templ.url = "github:a-h/templ"; + }; + + outputs = { self, nixpkgs, flake-utils, templ }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; + templPkg = templ.packages.${system}.templ; in { devShells.default = pkgs.mkShell { - buildInputs = [ - pkgs.go - pkgs.wgo - pkgs.semgrep - pkgs.gopls - pkgs.gnumake - ]; + buildInputs = + [ pkgs.go pkgs.wgo pkgs.semgrep pkgs.gopls pkgs.gnumake templPkg ]; shellHook = '' - export SOME_SHELL_VAR=ITS_VALUE + export GOPATH=$PWD/.go + export PATH=$GOPATH/bin:$PATH ''; }; }); diff --git a/go.mod b/go.mod index eef8b4b..798174e 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module sunshine.industries/temp-exercise go 1.21.7 + +require github.com/a-h/templ v0.2.598 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..55b8a53 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/a-h/templ v0.2.598 h1:6jMIHv6wQZvdPxTuv87erW4RqN/FPU0wk7ZHN5wVuuo= +github.com/a-h/templ v0.2.598/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= diff --git a/hello.templ b/hello.templ new file mode 100644 index 0000000..5df3590 --- /dev/null +++ b/hello.templ @@ -0,0 +1,5 @@ +package main + +templ hello(name string) { +
Hello, {name}
+} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..68d9e1e --- /dev/null +++ b/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "github.com/a-h/templ" + "net/http" +) + +func main() { + component := hello("some name") + + http.Handle("/", templ.Handler(component)) + + fmt.Println("starting to serve on :3000") + http.ListenAndServe(":3000", nil) +} From de2fd2bdc063b24639757395415844ba1e5a704b Mon Sep 17 00:00:00 2001 From: efim Date: Mon, 11 Mar 2024 07:32:28 +0000 Subject: [PATCH 3/5] feat: Makefile for local development would be even more useful when i'll get tailwind and stuff --- Makefile | 20 ++++++++++++++++++++ hello.templ | 9 +++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ca525d8 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +# List of all .templ files +TEMPL_FILES := $(wildcard *.templ) + +# Name of the generated file(s) from templ +GENERATED_FILES := $(TEMPL_FILES:.templ=_templ.go) + +# Generate Go files from .templ files only if they have changed +$(GENERATED_FILES): $(TEMPL_FILES) + templ generate + +# Run task depends on the generated files +run: $(GENERATED_FILES) + go run . + +# this uses wgo to re-execute 'make run' +# which will toggle templ generate, when needed +# and then do `go run .` to rebuild all else +.PHONY: run/live +run/live: + wgo -verbose -file=.go -file=.templ make run diff --git a/hello.templ b/hello.templ index 5df3590..644efd9 100644 --- a/hello.templ +++ b/hello.templ @@ -1,5 +1,10 @@ package main +var myVar string = "some string, changed. and more" +var anotherVar string = "hoho, cool" + templ hello(name string) { -
Hello, {name}
-} \ No newline at end of file +
Hello, {name}. referencing var: {myVar}
+} + + From 9c19bd7b6ba72d27f6d61d98dc5cdf9231236f6a Mon Sep 17 00:00:00 2001 From: efim Date: Mon, 11 Mar 2024 10:45:47 +0000 Subject: [PATCH 4/5] feat: initialized tailwind basic conig targetting templ templ html page with import Makefile job that generates for the run --- .gitignore | 3 ++- Makefile | 12 ++++++++++-- flake.nix | 18 +++++++++++++----- hello.templ | 12 +++++++++--- input.css | 3 +++ main.go | 3 +++ tailwind.config.js | 9 +++++++++ 7 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 input.css create mode 100644 tailwind.config.js diff --git a/.gitignore b/.gitignore index 11129db..a397892 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.direnv/ /.go/ *~ -*_templ.go \ No newline at end of file +*_templ.go +static/output.css diff --git a/Makefile b/Makefile index ca525d8..73ba7c1 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,16 @@ GENERATED_FILES := $(TEMPL_FILES:.templ=_templ.go) $(GENERATED_FILES): $(TEMPL_FILES) templ generate -# Run task depends on the generated files -run: $(GENERATED_FILES) +TAILWIND_CONFIG := tailwind.config.js +INPUT_CSS := input.css +OUTPUT_CSS := static/output.css + +# generate tailwindcss output +$(OUTPUT_CSS): $(TEMPL_FILES) $(TAILWIND_CONFIG) $(INPUT_CSS) + tailwindcss -i $(INPUT_CSS) -o $(OUTPUT_CSS) + +# Run the server, with dependencies on templ and tailwind generation +run: $(GENERATED_FILES) $(OUTPUT_CSS) go run . # this uses wgo to re-execute 'make run' diff --git a/flake.nix b/flake.nix index 05cdf9e..b998fb7 100644 --- a/flake.nix +++ b/flake.nix @@ -5,15 +5,23 @@ rec { flake-utils.url = "github:numtide/flake-utils"; templ.url = "github:a-h/templ"; }; - + outputs = { self, nixpkgs, flake-utils, templ }: flake-utils.lib.eachDefaultSystem (system: - let pkgs = nixpkgs.legacyPackages.${system}; - templPkg = templ.packages.${system}.templ; + let + pkgs = nixpkgs.legacyPackages.${system}; + templPkg = templ.packages.${system}.templ; in { devShells.default = pkgs.mkShell { - buildInputs = - [ pkgs.go pkgs.wgo pkgs.semgrep pkgs.gopls pkgs.gnumake templPkg ]; + buildInputs = [ + pkgs.go + pkgs.wgo + pkgs.semgrep + pkgs.gopls + pkgs.gnumake + templPkg + pkgs.tailwindcss + ]; shellHook = '' export GOPATH=$PWD/.go export PATH=$GOPATH/bin:$PATH diff --git a/hello.templ b/hello.templ index 644efd9..7087105 100644 --- a/hello.templ +++ b/hello.templ @@ -4,7 +4,13 @@ var myVar string = "some string, changed. and more" var anotherVar string = "hoho, cool" templ hello(name string) { -
Hello, {name}. referencing var: {myVar}
+ + + This is the title of the webpage! + + + +

This is an example paragraph. Anything in the body tag will appear on the page, just like this p tag and its contents.

+ + } - - diff --git a/input.css b/input.css new file mode 100644 index 0000000..b5c61c9 --- /dev/null +++ b/input.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; diff --git a/main.go b/main.go index 68d9e1e..4798966 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,9 @@ func main() { http.Handle("/", templ.Handler(component)) + staticFs := http.FileServer(http.Dir("./static")) + http.Handle("/static/", http.StripPrefix("/static/", staticFs)) + fmt.Println("starting to serve on :3000") http.ListenAndServe(":3000", nil) } diff --git a/tailwind.config.js b/tailwind.config.js new file mode 100644 index 0000000..6eb134e --- /dev/null +++ b/tailwind.config.js @@ -0,0 +1,9 @@ +/** @type {import('tailwindcss').Config} */ +module.exports = { + content: ["./**/*.templ"], + theme: { + extend: {}, + }, + plugins: [], +} + From 78c9bd1d614c8bdd25a3e1d23bd0a39bb01e65f1 Mon Sep 17 00:00:00 2001 From: efim Date: Tue, 12 Mar 2024 05:46:57 +0000 Subject: [PATCH 5/5] refactor: moving templates into dir routes probably can live in main.go for simpler exercises --- Makefile | 2 +- go.mod | 2 +- go.sum | 2 ++ main.go | 8 +++++--- hello.templ => templates/hello.templ | 12 ++++++------ 5 files changed, 15 insertions(+), 11 deletions(-) rename hello.templ => templates/hello.templ (50%) diff --git a/Makefile b/Makefile index 73ba7c1..cc8de80 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # List of all .templ files -TEMPL_FILES := $(wildcard *.templ) +TEMPL_FILES := $(shell find templates -type f -name '*.templ') # Name of the generated file(s) from templ GENERATED_FILES := $(TEMPL_FILES:.templ=_templ.go) diff --git a/go.mod b/go.mod index 798174e..0a9bbb0 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module sunshine.industries/temp-exercise go 1.21.7 -require github.com/a-h/templ v0.2.598 // indirect +require github.com/a-h/templ v0.2.598 diff --git a/go.sum b/go.sum index 55b8a53..0e72287 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/a-h/templ v0.2.598 h1:6jMIHv6wQZvdPxTuv87erW4RqN/FPU0wk7ZHN5wVuuo= github.com/a-h/templ v0.2.598/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/main.go b/main.go index 4798966..602ddc4 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,15 @@ package main import ( "fmt" - "github.com/a-h/templ" "net/http" + + "sunshine.industries/temp-exercise/templates" + "github.com/a-h/templ" ) func main() { - component := hello("some name") - + component := templates.Hello("some name") + http.Handle("/", templ.Handler(component)) staticFs := http.FileServer(http.Dir("./static")) diff --git a/hello.templ b/templates/hello.templ similarity index 50% rename from hello.templ rename to templates/hello.templ index 7087105..9338eaf 100644 --- a/hello.templ +++ b/templates/hello.templ @@ -1,16 +1,16 @@ -package main +package templates var myVar string = "some string, changed. and more" var anotherVar string = "hoho, cool" -templ hello(name string) { +templ Hello(name string) { - This is the title of the webpage! - + This is the title of the the webpage! + - -

This is an example paragraph. Anything in the body tag will appear on the page, just like this p tag and its contents.

+ +

This is an example paragraph. Anything in the body tag will appear on the page, just like this p tag and its contents.

}