Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Template for GO Project.

Table of Contents

  1. Introduction
  2. Project Structure
  3. Installation and Setup
  4. Database Setup
  5. Code Generation
  6. Running the Application
  7. Configuration
  8. Logging
  9. Middleware
  10. Additional Notes

Introduction

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.

Project Structure

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

Installation and Setup

Prerequisites

Ensure you have the following tools installed on your system:

Installing Dependencies

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 tidy

Install sqlc and goose:

go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
go install github.com/pressly/goose/v3/cmd/goose@latest

Database Setup

Configure 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/attempt2

Running Migrations

To apply all pending migrations, run:

make goose_up

To roll back the last migration, run:

make goose_down

Code Generation

Generate Go code from SQL queries using sqlc:

sqlc generate

This will generate Go code based on the SQL queries defined in internal/db/queries.sql. Running the Application

Run

To start the application, use the following command:

make run

This 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.

About

๐Ÿš€ Go Project Starter Template: Go-Chi, SQLC, and Goose integration for rapid API development. Set up your Go backend with PostgreSQL, routing, and migrations effortlessly.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages