From df5e2a26cb5b317ab7c85a2bd0b767399ab16dbe Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Mon, 17 Jun 2024 16:28:19 -0300 Subject: [PATCH] chore: improve development workflow --- .gitignore | 1 + flake.nix | 1 - main.go | 3 ++- makefile | 78 ++++++++++++++++++++++-------------------------------- 4 files changed, 35 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index f8214ad..785dc37 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ Session.vim .direnv .vercel *_templ.go +*_templ.txt dist tmp bin diff --git a/flake.nix b/flake.nix index 90fabf3..2695201 100644 --- a/flake.nix +++ b/flake.nix @@ -21,7 +21,6 @@ { devShells.default = pkgs.mkShell { buildInputs = with pkgs; [ - air go golangci-lint templ diff --git a/main.go b/main.go index db90409..9b92467 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { staticDir := flag.String("s", "./static", "the directory to copy static files from") port := flag.Int("p", 8080, "the port to run the server") dev := flag.Bool("d", false, "if the server is in development mode") + cache := flag.Bool("c", true, "if the static files are cached") flag.Parse() @@ -66,7 +67,7 @@ func main() { logger.Printf("Running server at port: %v", *port) - middleware := internals.NewMiddleware(mux, *dev, log.Default()) + middleware := internals.NewMiddleware(mux, *dev, !*cache, log.Default()) err := http.ListenAndServe(fmt.Sprintf(":%v", *port), middleware) if err != nil { logger.Fatalf("Server crashed due to:\n%s", err) diff --git a/makefile b/makefile index 37bbcf7..17d5cec 100644 --- a/makefile +++ b/makefile @@ -1,58 +1,44 @@ PORT?=8080 -all: run +build: + pnpm unocss + templ generate + go build -o bin/www -dev: - $(MAKE) -j 2 dev-unocss dev-air +dev/templ: + templ generate --watch \ + --proxy=http://localhost:$(PORT) \ + --open-browser=false -dev-unocss: - pnpm unocss -w - -dev-air: - air -build.pre_cmd 'make templ' \ - -build.include_ext 'templ,css,go' \ - -build.exclude_dir 'node_modules' \ - -proxy.enabled true \ - -proxy.app_port $(PORT) \ - -proxy.proxy_port $$(($(PORT) + 1)) \ +dev/server: + go run github.com/air-verse/air@v1.52.2 \ + --build.cmd "go build -o tmp/bin/main" \ + --build.bin "tmp/bin/main" \ + --build.exclude_dir "node_modules" \ + --build.include_ext "go" \ + --build.stop_on_error "false" \ + --misc.clean_on_exit true \ -- -p $(PORT) -d -dev-vercel: - air -build.pre_cmd 'make build-vercel' \ - -build.include_ext 'templ,css' \ - -build.exclude_dir 'node_modules' \ - -build.cmd 'make build-vercel' \ - -build.bin './bin/vercel' \ - -proxy.enabled true \ - -proxy.app_port $(PORT) \ - -proxy.proxy_port $$(($(PORT) + 1)) \ - -- -p $(PORT) +dev/unocss: + pnpm unocss -w -run: bin/www +dev/sync_assets: + go run github.com/air-verse/air@v1.52.2 \ + --build.cmd "templ generate --notify-proxy" \ + --build.bin "true" \ + --build.delay "100" \ + --build.exclude_dir "" \ + --build.include_dir "static" \ + --build.include_ext "js,css" + +dev: + make -j4 dev/templ dev/server dev/unocss dev/sync_assets + +run: build ./bin/www -run-vercel: bin/vercel - ./bin/vercel - -build-static: templ - pnpm unocss - go run ./cmd/build/main.go - -build-vercel: bin/vercel build-static - -bin/www: main.go templ - go build -o ./bin/www ./main.go - -bin/vercel: cmd/vercel/main.go templ - go build -o ./bin/vercel ./cmd/vercel/main.go - -# For some reason "templ generate" does not detect the files, so this is a -# workaround. -TEMPL_FILES=$(patsubst %.templ, %_templ.go, $(wildcard **/*.templ)) -templ: $(TEMPL_FILES) - @echo Generating templ files -%_templ.go: %.templ - templ generate -f $^ > /dev/null +all: build clean: if [[ -d "dist" ]]; then rm -r ./dist; fi