A sane and simple Go REST API template.

Gosane 🧘‍♀️

License: MIT

A sane and simple Go REST API template. Clone me and edit me to fit your usecase.

What is Gosane?

Gosane is a cloneable API template to get you up and running quickly. It has made a lot of decisions for you, but easily allows you to swap out the things you don't like.

Features

Service Description
Auth 🔑 Social (FB / Google) as well as email based JWT authentication.
Database 💽 Database support using the amazing https://github.com/ent/ent package.
Email ✉️ There's an example AWS SES implementation and an easily extendable interface.
Config 🗃 Simple JSON and environment based configuration via https://github.com/sno6/config.
Monitoring 🕵️ Prometheus handlers for monitoring.
Errors 🔦 Automatic sentry error logging via: https://sentry.io
Validation 👮‍♀️ Validation using an extended version of the https://github.com/go-playground/validator package.
Build / Test 💪 Automatically build and test your code with built in Github pipelines.
Server 💻 The underlying server framework is Gin, so you benefit from all the goodness you can find over at: https://github.com/gin-gonic/gin

Structure

Browse the codebase in VS Code here: https://github1s.com/sno6/gosane

Gosane is structured as follows:

Handlers

Each handler (or endpoint) is grouped and encapsulated in its own folder as can be seen here. Firstly, you must define the relative path for the handler group in a file such as this, and then define each endpoint as a separate handler.

Services & Stores

A handler interacts with your business logic through services, which are aptly defined in /service. These services interact with your database entities (using ent) via stores. The flow of information should look something like the following:

Handler <-> Services <-> Stores

A store should never be used directly in a handler, and a service should never be used in a store.

Internal

Anything that isn't considered business logic should live here. Typically you want to structure these as small modules that you could rip out and run isolated from the rest of the project, if you had to. Examples include, email, database, sentry (error management), etc.

Dependencies

Gosane follows a simple dependency injection plan. All dependencies for your API are defined in api/register.go. The server initialises all dependencies and passes them through to the handlers via the Register method.

That's about it, the rest is up to you.

How to use Gosane

1. Clone the project

git clone [email protected]:sno6/gosane.git

2. Run the damn thing

./run.sh

Note that if the above command errors you may need to give the script executive permissions by running: chmod +x ./run.sh

Postman

You can download the exported Postman collection here.

Potential issues

Gosane is complaining about a Sentry DSN, what's that?

In order for Sentry to know where to log errors to it needs a URL. To get one, follow these steps:

  1. Sign up for a free account over at https://sentry.io/welcome
  2. Select Go as the platform.
  3. Copy the Dsn value in the sample code to your .env file.

That's it.

But what if I don't want social OAuth?

Just simply rip out everything for social OAuth. Here's where everything will be:

  1. /api/handler/oauth This whole folder can go.
  2. /api/register.go Remove the reference to the OAuth handler here.
  3. /config/config.go Remove everything to do with OAuth from the config.
  4. /internal/server.go Remove the fb/google configs that are passed to the API as dependencies.

But what if I don't want Sentry?

Similarly, do the following:

  1. /internal/sentry This whole folder can go.
  2. /config/config.go Remove all Sentry related config params.
  3. /api/register.go Remove the reference to Sentry from the dependencies list here.
  4. /internal/server.go Remove the Sentry dependency and deferred handler from here.

For any other problems feel free to create an issue and ping me @sno6.

Future work (to be completed in the near future)

  • ~80-100% testing coverage.
  • Add missing auth related endpoints: "forgot password", "re-send verification email".
  • Handle database migrations.
  • Transaction rollbacks on recovery state.
  • Command line tool to generate new project based on feature requirements.
  • Add an example handler to show pagination & sorting usage.
Owner
Farley
Open for work! More info at https://farley.ai
Farley
Similar Resources

A simple template using Fiber for me to bootstrap API services quickly.

Fiber Template A simple template using Fiber for me to bootstrap API services quickly. Features Fiber GORM air for hot reloading ... and possibly more

Dec 16, 2021

Simple and fast template engine for Go

fasttemplate Simple and fast template engine for Go. Fasttemplate performs only a single task - it substitutes template placeholders with user-defined

Dec 30, 2022

A shot API template following MVC model

Go API template This project is mainly personnal, to have a quick ready-to-go API template in go using gin gonic How does it works ? Main call interna

Nov 23, 2021

Simple template suitable for building a webapp backend MVP written in go

A Simple Go Project Template - Suited for Webapp MVPs A simple go project structure setup with all dependencies you need to get your MVP off the groun

Oct 23, 2022

This is a simple GoLang script template.

This is a simple GoLang script template.

Oct 25, 2022

Package damsel provides html outlining via css-selectors and common template functionality.

Damsel Markup language featuring html outlining via css-selectors, extensible via pkg html/template and others. Library This package expects to exist

Oct 23, 2022

A handy, fast and powerful go template engine.

A handy, fast and powerful go template engine.

Hero Hero is a handy, fast and powerful go template engine, which pre-compiles the html templates to go code. It has been used in production environme

Dec 27, 2022

A general purpose golang CLI template for Github and Gitlab

golang-cli-template A general purpose project template for golang CLI applications This template serves as a starting point for golang commandline app

Dec 2, 2022

⚗ The most advanced CLI template on earth! Featuring automatic releases, website generation and a custom CI-System out of the box.

⚗ The most advanced CLI template on earth! Featuring automatic releases, website generation and a custom CI-System out of the box.

cli-template ✨ ⚗ A template for beautiful, modern, cross-platform compatible CLI tools written with Go! Getting Started | Wiki This template features

Dec 4, 2022
Go-api-template - A rough template to give you a starting point for your API

Golang API Template This is only a rough template to give you a starting point f

Jan 14, 2022
Simple system for writing HTML/XML as Go code. Better-performing replacement for html/template and text/template

Simple system for writing HTML as Go code. Use normal Go conditionals, loops and functions. Benefit from typing and code analysis. Better performance than templating. Tiny and dependency-free.

Dec 5, 2022
Template for Golang rest API using Fiber

Rest API Setup DB sudo -S docker-compose -f db.yml up -d Build container sudo -S docker build -t rest-api-image . Run container from image sudo -S doc

Dec 5, 2021
Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

goview Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Inst

Dec 25, 2022
A template to build dynamic web apps quickly using Go, html/template and javascript
A template to build dynamic web apps quickly using Go, html/template and javascript

gomodest-template A modest template to build dynamic web apps in Go, HTML and sprinkles and spots of javascript. Why ? Build dynamic websites using th

Dec 29, 2022
Wrapper package for Go's template/html to allow for easy file-based template inheritance.

Extemplate Extemplate is a small wrapper package around html/template to allow for easy file-based template inheritance. File: templates/parent.tmpl <

Dec 6, 2022
Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

quicktemplate A fast, powerful, yet easy to use template engine for Go. Inspired by the Mako templates philosophy. Features Extremely fast. Templates

Dec 26, 2022
Made from template temporalio/money-transfer-project-template-go
Made from template temporalio/money-transfer-project-template-go

Temporal Go Project Template This is a simple project for demonstrating Temporal with the Go SDK. The full 20 minute guide is here: https://docs.tempo

Jan 6, 2022
Go-project-template - Template for a golang project

This is a template repository for golang project Usage Go to github: https://git

Oct 25, 2022
Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering
 Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering

Go-Mojito is a super-modular library to bootstrap your next Go web project. It can be used for strict API-only purposes as well as server-side renderi

May 1, 2022