- Introduction
- Project Structure
- Installation and Setup
- Database Setup
- Code Generation
- Running the Application
- Configuration
- Logging
- Middleware
- Additional Notes
This project template sets up a Go application with key features like database migrations, SQL code generation, logging, and middleware. It is designed to help you get started quickly by providing a foundational structure and essential tools.
Here's a brief overview of the project's directory structure:
project-root/
|-- cmd/
| |-- main.go // Main application entry point; initializes and starts the application
|
|-- http/
| |-- handlers/ // Directory for HTTP handlers which process incoming requests and return responses
| | |-- user_handler/ // Handlers specific to user-related operations
| | | |-- user_handler.go // Contains functions to handle user-related HTTP requests (e.g., user login, registration)
| | | |-- user_model.go // Defines data structures (models) for user data processed by handlers
| | |-- util/ // Utility functions that can be used across different handlers
| | | |-- util.go // Common utility functions (e.g., data validation, formatting)
| |-- helper/ // Contains helper functions for common operations (e.g., cookie management, JWT handling)
| | |-- cookies.go // Functions to manage HTTP cookies
| | |-- jwt.go // Functions for JWT (JSON Web Token) creation and validation
| | |-- passwords.go // Functions for password hashing and verification
| |-- middleware/ // Middleware components that process HTTP requests before they reach the handlers
| | |-- jwtMiddleware.go // Middleware to handle JWT authentication
| | |-- logger.go // Middleware for logging HTTP requests and responses
| |-- response/ // Utility for building and sending HTTP responses
| | |-- response.go // Functions to format and send HTTP responses consistently
| |-- router/ // Routing configuration for the application
| | |-- router.go // Sets up HTTP routes and associates them with handlers and middleware
|
|-- init/
| |-- db.go // Database initialization and connection setup
|
|-- sql/
| |-- database/ // Database-related Go code and models
| | |-- db.go // Functions to interact with the database (e.g., queries, transactions)
| | |-- models.go // Data structures representing database tables
| | |-- users.sql.go // SQL query execution functions for user-related operations (auto-generated by `sqlc`)
| |-- queries/ // Raw SQL queries used for code generation
| | |-- users.sql // SQL queries for user-related operations
| |-- schema/ // SQL migration files to set up and update the database schema
| | |-- 001_users.sql // Initial migration to create the `users` table
|
|-- Makefile // Automation of build, run, and other tasks (e.g., running migrations, generating code)
|-- README.md // Project documentation; provides setup and usage instructions
Ensure you have the following tools installed on your system:
- Go (version 1.18+ recommended)
- PostgreSQL
- sqlc (SQL code generator)
- Goose (Database migration tool)
Clone the repository and install Go modules:
git clone https://github.com/high-horse/go-chi-pg-sqlc-goose-template.git
cd yourproject
go mod tidygo install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
go install github.com/pressly/goose/v3/cmd/goose@latestConfigure your database connection in the .env file or directly in your configuration. The default PostgreSQL connection string looks like this:
postgres://postgres:root@localhost:5432/attempt2To apply all pending migrations, run:
make goose_upTo roll back the last migration, run:
make goose_downGenerate Go code from SQL queries using sqlc:
sqlc generateThis will generate Go code based on the SQL queries defined in internal/db/queries.sql. Running the Application
To start the application, use the following command:
make runThis will compile and run your Go application. Configuration
Application configurations are typically handled in config/config.go. You can set up environment variables or use a configuration file to manage your settings. Logging
Logging is set up in internal/logger/. The project uses a structured logging approach to capture logs efficiently. Adjust the logger setup in logger.go to suit your requirements. Middleware
Middleware is managed in the http/helper/ package. Middleware functions provide a way to process requests and responses, and can be used for tasks like authentication, logging, and error handling. Additional Notes
Ensure your database is running and accessible before running migrations or starting the application. Customize the project structure and components to fit the needs of your application. Refer to the documentation of tools like sqlc and goose for advanced usage and configuration options.
This template provides a comprehensive guide to setting up and running a Go project with the mentioned components. Adjust the details according to your project's requirements and structure.