Giter Club home page Giter Club logo

golang-starter's Introduction

Golang Api Starter

This is a monolithic http api starter with sane defaults. Features include:

  • air - hot-reloading dev server
  • slog - global logging
  • chi/v5 - routing & middleware
  • pgx/v5 - database connectivity with pooling
  • goose - database migrations without adding to app dependency
  • bcrypt - password hashing
  • golang-jwt/jwt/v5 - jwt Authentication
  • validator/v10 - incoming request payload validation
  • envconfig - app configuration parsing & validation
  • prometheus/promhttp for default metrics
  • Makefile commands for toolchain

Folder Structure

The project follows below folder structure:

project-root/
    ├── api/                     # API-related code (e.g. REST)
    │   ├── middleware/          # Middleware for HTTP requests
    │   │   └── ...
    │   ├── routes.go            # All Application routes
    │   └── server.go            # Server Setup
    ├── app/                     # Encapsulted Applicaton Logic
    │   ├── auth/
    │   │   ├── handler.go       # Http handlers specific to auth
    │   │   ├── router.go        # Http routes specific to auth
    │   │   └── types.go         # Types routes specific to auth
    │   └── ...                  # Other application logic folders
    ├── cert/                    # Certificates & Keys
    │   └── ...
    ├── cmd/
    │   ├── main.go              # Server entrypoint
    │   └── ...                  # Other application entrypoints
    ├── config/                  # Configuration logic
    │   ├── config.go
    │   └── ...
    ├── db/
    │   ├── migrations/          # Migrations files for goose
    │   │   └── *.sql
    │   ├── db.go                # Database connection setup
    │   ├── logger.go            # Database logger adapter
    │   └── ...                  # Database model files
    ├── docs/                    # Project documentations
    │   ├── bruno/               # Bruno collection for exploring api
    │   │   └── ...
    │   └── ...
    ├── pkg/                     # Public, reusable packages
    │   ├── <name>/
    │   │   └── ...              # Public package code
    │   └── ...
    ├── .gitignore               # Gitignore file
    ├── Makefile                 # Runnable Scripts
    ├── go.mod                   # Go module file
    ├── go.sum                   # Go module dependencies file
    └── README.md                # Project README

Building Binaries

use below command to build binaries targetted towards supported os & architechture . A github workflow file is also included

make build os=<OPERATING SYSTEM> arch=<ARCHITECHTURE>

Get Started - Development

Environment

Ensure following tools available in your machine

  • Docker >= v27
  • Docker Compose >=v2.29
  • OpenSSL >= 1.1.1

Server

To run the development server use below command:

make init

This command will

  • Copy .env.example to .env, filling in default values
  • Generate TLS cert for serving https locally.
  • Generate ECDSA public-private key pair for JWT Auth locally.
  • Start development server in hot reload mode
  • Migrate database to latest migration

After running above command for the first time, you should only start development server in hot reload mode using below command

make dev

n.b. In production environment, mount your certificates in cert directory, ensure proper name & path supplied in env.

Exploring APIs

Bruno is an Opensource IDE For Exploring and Testing Api's, lightweight alternative to postman/insomnia. Open Directory docs/bruno from the bruno app & the collection will show up. Choose the environment Local.

Download & install bruno -> click here.

Other Commands

install & clean golang dependencies

make tidy
make deps

run the development server with hot reload

make dev

run migration of postgresql database, ensure .env values are correctly given

make migrate-up

revert migrations by 1 level

make migrate-down

create migration files

make migrate-create f=<filename>

Generate TLS cert for serving https locally.

make tls

Generate ECDSA public-private key pair for JWT Auth locally.

make jwt

Run linter

make lint

golang-starter's People

Contributors

axeltahmid avatar

Watchers

 avatar  avatar

Forkers

atiqus

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.