go-blueprint
repository·main·Indexed 27 days ago
https://github.com/melkeydev/go-blueprintA 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.
What's inside go-blueprint
- 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.
Understand the HTMX/Templ project structure
mainProjects using the
htmxadvanced flag follow this directory structure within theweb/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.
Select a Go web framework for your project
mainWhen 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.
Understand Redis Health Functionality
mainThe
Healthfunction performs a multi-step assessment of the Redis server:- Check Redis Health: Pings the server. If the ping fails, the program logs an error and terminates.
- Retrieve Redis Information: Fetches server metadata including version, mode, connected clients, memory usage, and uptime.
- 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
Blueprint UI
mainBlueprint UI is a web application at go-blueprint.dev that allows you to visually create commands for the CLI and preview the directory and file structure that will be generated by your command.Understand the generated project structure
mainProjects generated by
go-blueprintfollow a standardized layout focused on thecmd,internal, andtestsdirectories to maintain a clean separation of concerns:/cmd/api/main.go: The entry point of the application./internal/server/: Contains the core server logic, includingserver.goandroutes.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.mdImplement a Database Driver
mainTo integrate a database, follow these steps:
- Select your desired driver from the supported list.
- Import the chosen driver into your project.
- Update the
internal/database/database.gofile to establish the connection and manage interactions with the selected database.
The resulting project structure will include a new
internal/databasedirectory:/(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.mdInstall and build Tailwind CSS via Makefile
mainYou 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.csstocmd/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.goManage projects using Makefile targets
mainThe project includes a
Makefileto automate building, running, testing, and managing dependencies liketempl,tailwindcss, andair. The Makefile handles OS-specific logic for Unix-based systems (Linux/macOS) and Windows.Core Targets
all: The default target. Runsbuildandtest.build: Builds the Go application and generates assets usingtemplandtailwindcss(if enabled).run: Executes the application viacmd/api/main.go. If the React flag is used, it also performsnpm installandnpm run dev.test: Runs unit tests usinggo test.itest: Runs integration tests (requires a database other than SQLite).clean: Removes the compiled binary (mainormain.exe).watch: Enables live reload using theairtool. It will prompt for installation on Unix or use PowerShell on Windows ifairis missing.
Run the React frontend development server
mainTo run the React frontend locally, navigate to the
frontenddirectory, install dependencies, and start the Vite development server:cd frontend npm install npm run devOnce running, the application is accessible at
http://localhost:5173.Configure specific advanced features via --feature flag
mainFor non-interactive setup, you can specify exactly which advanced features to include by using the
--featureflag 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 tailwindEnable advanced features with the --advanced flag
mainThe
--advancedflag can be used with thecreatecommand 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