go-blueprint

repository·main·Indexed 27 days ago

https://github.com/melkeydev/go-blueprint

A CLI tool for rapidly scaffolding Go projects with pre-defined structures, integrated web frameworks (such as Gin, Echo, Fiber, and Chi), and database drivers (including Postgres, MySQL, MongoDB, and Redis). It supports interactive and non-interactive modes to configure project names, Git initialization, and advanced features like Docker, GitHub Actions, WebSockets, and frontend integrations with React, HTMX, and Tailwind CSS.

Tokens
24.9K
Snippets
62
Records
171
Agent score
92%

What's inside go-blueprint

  1. Overview of Go Blueprint

    main
    Go Blueprint is a CLI tool designed to streamline the creation of Go projects by providing a robust, standardized project structure. It automates project initialization and offers seamless integration with popular Go HTTP frameworks, allowing developers to focus on application logic rather than scaffolding.
  2. Understand the HTMX/Templ project structure

    main

    Projects using the htmx advanced flag follow this directory structure within the web/ folder:

    • assets/js/htmx.min.js: The htmx library.
    • base.templ: The base HTML structure template.
    • base_templ.go: Generated Go code for the base template.
    • efs.go: Logic to embed static files into the Go binary.
    • hello.go: Go handler for the 'Hello Web' functionality.
    • hello.templ: Template for rendering the Hello form and post data.
    • hello_templ.go: Generated Go code for the hello template.
  3. Select a Go web framework for your project

    main

    When generating a project with go-blueprint, you can choose from several supported Go web frameworks to handle HTTP routing and server functionality:

    • Chi: Lightweight and flexible router.
    • Echo: High-performance, extensible, and minimalist framework.
    • Fiber: Express-inspired framework designed for speed and simplicity.
    • Gin: High-performance framework with a martini-like API.
    • Gorilla/mux: Powerful URL router and dispatcher.
    • HttpRouter: High-performance HTTP request router that scales well.
  4. Understand Redis Health Functionality

    main

    The Health function performs a multi-step assessment of the Redis server:

    1. Check Redis Health: Pings the server. If the ping fails, the program logs an error and terminates.
    2. Retrieve Redis Information: Fetches server metadata including version, mode, connected clients, memory usage, and uptime.
    3. Evaluate Redis Statistics: Analyzes metrics to identify potential issues such as:
      • High number of connected clients
      • Stale connections
      • High memory usage
      • Recent restarts (low uptime)
      • High idle connections
      • High connection pool utilization
  5. Understand the generated project structure

    main

    Projects generated by go-blueprint follow a standardized layout focused on the cmd, internal, and tests directories to maintain a clean separation of concerns:

    • /cmd/api/main.go: The entry point of the application.
    • /internal/server/: Contains the core server logic, including server.go and routes.go (along with corresponding tests).
    • go.mod / go.sum: Go module dependency files.
    • Makefile: Automation commands for the project.
    • README.md: Project documentation.
    /(Root)
    ├── /cmd
    │   └── /api
    │       └── main.go
    ├── /internal
    │   └── /server
    │       ├── routes.go
    │       ├── routes_test.go
    │       └── server.go
    ├── go.mod
    ├── go.sum
    ├── Makefile
    └── README.md
  6. Implement a Database Driver

    main

    To integrate a database, follow these steps:

    1. Select your desired driver from the supported list.
    2. Import the chosen driver into your project.
    3. Update the internal/database/database.go file to establish the connection and manage interactions with the selected database.

    The resulting project structure will include a new internal/database directory:

    /(Root)
    ├── /cmd
    │   └── /api
    │       └── main.go
    ├── /internal
    │   ├── /database
    │   │   ├── database_test.go
    │   │   └── database.go
    │   └── /server
    │       ├── routes.go
    │       ├── routes_test.go
    │       └── server.go
    ├── go.mod
    ├── go.sum
    ├── Makefile
    └── README.md
  7. Install and build Tailwind CSS via Makefile

    main

    You can manage the Tailwind CLI and your project build using a Makefile. The following configuration automatically detects the OS (example provided for Linux), downloads the latest Tailwind release, and compiles your CSS from cmd/web/styles/input.css to cmd/web/assets/css/output.css.

    all: build
    templ-install:
    	@if ! command -v templ > /dev/null; then \
    		read -p "Go's 'templ' is not installed on your machine. Do you want to install it? [Y/n] " choice; \
    		if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
    			go install github.com/a-h/templ/cmd/templ@latest; \
    			if [ ! -x "$$(command -v templ)" ]; then \
    				echo "templ installation failed. Exiting..."; \
    				exit 1; \
    			fi; \
    		else \
    			echo "You chose not to install templ. Exiting..."; \
    			exit 1; \
    		fi; \
    	fi
    
    tailwind-install:
    	@if [ ! -f tailwindcss ]; then curl -sL https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64 -o tailwindcss; fi
    	@chmod +x tailwindcss
    
    build: tailwind-install templ-install
    	@echo "Building..."
    	@templ generate
    	@./tailwindcss -i cmd/web/styles/input.css -o cmd/web/assets/css/output.css
    	@go build -o main cmd/api/main.go
  8. Manage projects using Makefile targets

    main

    The project includes a Makefile to automate building, running, testing, and managing dependencies like templ, tailwindcss, and air. The Makefile handles OS-specific logic for Unix-based systems (Linux/macOS) and Windows.

    Core Targets

    • all: The default target. Runs build and test.
    • build: Builds the Go application and generates assets using templ and tailwindcss (if enabled).
    • run: Executes the application via cmd/api/main.go. If the React flag is used, it also performs npm install and npm run dev.
    • test: Runs unit tests using go test.
    • itest: Runs integration tests (requires a database other than SQLite).
    • clean: Removes the compiled binary (main or main.exe).
    • watch: Enables live reload using the air tool. It will prompt for installation on Unix or use PowerShell on Windows if air is missing.
  9. Configure specific advanced features via --feature flag

    main

    For non-interactive setup, you can specify exactly which advanced features to include by using the --feature flag multiple times. This allows you to bypass the interactive selection and define your stack explicitly.

    go-blueprint create --name my-project --framework chi --driver mysql --advanced --feature htmx --feature githubaction --feature websocket --feature tailwind
  10. Enable advanced features with the --advanced flag

    main

    The --advanced flag can be used with the create command to unlock additional project features. When using this flag, you can choose to include one or all of the following features:

    • HTMX Support using Templ: Integration of HTMX for dynamic web pages using Templ.
    • CI/CD Workflow Setup using GitHub Actions: Automated CI/CD workflow configuration.
    • Websocket Support: WebSocket endpoints for continuous data streams.
    • Tailwind: Tailwind CSS support.
    • Docker: Docker configuration for the Go project.
    • React: A TypeScript-based frontend including an example fetch request to the backend.
    go-blueprint create --name <project_name> --framework <selected_framework> --driver <selected_driver> --advanced