• Stars
    star
    885
  • Rank 49,521 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go Server/API boilerplate using best practices DDD CQRS ES gRPC

🧰 Golang API Starter Kit

Build Status Test Go Report Card codecov license baker sponsor

logo

Go Server/API boilerplate using best practices, DDD, CQRS, ES, gRPC.

Table of Contents

πŸ“– ABOUT

The main purpose of this project is to provide boilerplate project setup using best practices, DDD, CQRS, ES, gRPC. Featuring kubernetes for both development and production environments. Allowing to work with environment reflecting production one, allowing to reduce any misconfigurations.

This is mono-repository of many services such as authentication or user domain. Each service has it own code base with exception of shared packages to simplify things for this boilerplate. Services communicate witch each other using gRPC. Each service might expose HTTP API for external communication or/and gRPC.

This project setup should reduce the time spent on environment configuration for the whole kubernetes cluster and/or each of microservice. Extracting each of services to own repository or keeping it as mono-repo should be a matter of preference.

Please look for comments like @TODO and @FIXME to better understand things than need attention.

Web UI example (React)

This boilerplate includes simple Web UI to demonstrate example interaction with API. Once deployed and hosts are set please visit https://api.go-api-boilerplate.local to access UI.

Web UI

Key concepts:

  1. Rest API
  2. Docker
  3. Kubernetes
  4. Helm chart
  5. Terraform
  6. gRPC
  7. Domain Driven Design (DDD)
  8. CQRS
  9. Event Sourcing
  10. Hexagonal, Onion, Clean Architecture
  11. oAuth2

Worth getting to know packages used in this boilerplate:

  1. gorouter
  2. message-bus
  3. gollback
  4. shutdown
  5. pubsub
  6. pushpull
  7. gocontainer

πŸ“š DOCUMENTATION

🏫 EXAMPLE

Quick start

Localhost alias

Edit /etc/hosts to add localhost alias

➜ go-api-boilerplate git:(master) cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 go-api-boilerplate.local api.go-api-boilerplate.local maildev.go-api-boilerplate.local mysql.go-api-boilerplate.local

Build release

Local image

make docker-build BIN=auth
make docker-build BIN=migrate
make docker-build BIN=user
make docker-build BIN=web

GitHub Package Registry

Creating tag with metadata will trigger github workflow and publish docker image to GitHub Package Registry.

Tag v1.0.0+user will trigger build for user service releasing 1.0.0 docker image tag. you can create release for all services in cmd directory.

v1.0.0+auth
v1.0.0+user
v1.0.0+web
v1.0.0+migrate

Replace image details in main.yaml

  image:
-    repository: go-api-boilerplate-user
+    repository: docker.pkg.github.com/vardius/go-api-boilerplate/go-api-boilerplate-user
-    tag: latest
+    tag: 1.0.0
    pullPolicy: IfNotPresent

repeat for all services and migrate init containers.

Private Registry

Log in to Docker

docker login

Copy docker config

cp ~/.docker/config.json ./k8s/.docker/config.json

Verify config.json

Deploy release

make terraform-install

Destroy

make terraform-destroy

If persistent volume is stack in terminating, this happens when persistent volume is protected. You should be able to cross verify this:

kubectl describe pvc PVC_NAME --namespace=go-api-boilerplate | grep Finalizers

Output:
Finalizers:    [kubernetes.io/pvc-protection]

You can fix this by setting finalizers to null using kubectl patch:

kubectl patch pvc PVC_NAME --namespace=go-api-boilerplate -p '{"metadata":{"finalizers": []}}' --type=merge

Build tags

Build flags are used for different persistence layers. Please see services.go file for details. Provided layers are mysql, mongo and memory. If desired in similar way new layer can be easily added, following given patter.

go build -tags=persistence_mysql

Available build tags

  • persistence_mysql (mysql service container)
  • persistence_mongodb (mongodb service container)

Important persistence layer defaults to memory if no flag is provided (Docker image sets persistence_mysql flag), see each service Dockerfile for details.

Domain

Dispatching command

Send example JSON via POST request

curl -d '{"email":"[email protected]"}' -H "Content-Type: application/json" -X POST https://api.go-api-boilerplate.local/users/v1/dispatch/user/user-register-with-email --insecure

View

Public routes

Get user details https://api.go-api-boilerplate.local/users/v1/34e7ed39-aa94-4ef2-9422-401bba9fc812

{"id":"34e7ed39-aa94-4ef2-9422-401bba9fc812","email":"[email protected]"}

Get list of users https://api.go-api-boilerplate.local/users/v1?page=1&limit=10

{"page":1,"limit":20,"total":1,"users":[{"id":"34e7ed39-aa94-4ef2-9422-401bba9fc812","email":"[email protected]"}]}

Protected routes

Access protected route using auth token https://api.go-api-boilerplate.local/users/v1/me.

{"code": "401","message": "Unauthorized"}

Request access token for user

curl -d '{"email":"[email protected]"}' -H "Content-Type: application/json" -X POST https://api.go-api-boilerplate.local/users/v1/dispatch/user/user-request-access-token --insecure

Get your access token from mail catcher https://maildev.go-api-boilerplate.local.

Access protected route using auth token https://api.go-api-boilerplate.local/users/v1/me?authToken=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJyXHUwMDE277-977-977-977-9IiwiZXhwIjoxNTU5NjEwOTc2LCJzdWIiOiIzNGU3ZWQzOS1hYTk0LTRlZjItOTQyMi00MDFiYmE5ZmM4MTIifQ.pEkgtDAvNh2D3Dtgfpu4tt-Atn1h6QwMkDhz4KpgFxNX8jE7fQH00J6K5V7CV063pigxWhOMMTRLmQdhzhajzQ

{"id":"34e7ed39-aa94-4ef2-9422-401bba9fc812","email":"[email protected]"}

πŸ’² Sponsoring

πŸš€ Contributing

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

πŸ‘¨πŸ»β€πŸ’»πŸ‘©πŸΎβ€πŸ’» Core Team:


RafaΕ‚ Lorenz

Marin Kirkov

πŸ‘₯ Backers

Support us with a monthly donation and help us continue our activities.

πŸ₯‡ Sponsors

Proudly sponsored by Open Collective sponsors.

πŸ“œ License

More Repositories

1

message-bus

Go simple async message bus
JavaScript
247
star
2

gorouter

Go Server/API micro framework, HTTP request router, multiplexer, mux
Go
151
star
3

gollback

Go asynchronous simple function utilities, for managing execution of closures and callbacks
Go
109
star
4

worker-pool

Go simple async worker pool
Go
87
star
5

peer-data

Library for files, media streaming/sharing using WebRTC
JavaScript
64
star
6

peer-cdn

Lightweight library providing peer to peer CDN functionality
JavaScript
61
star
7

progress-go

Go simple progress bar writing to output
Go
53
star
8

web-components-webpack-es6-boilerplate

Web Components project starter using ES6 and Webpack
JavaScript
44
star
9

pubsub

gRPC message-oriented middleware on top of message-bus, event ingestion and delivery system.
Go
41
star
10

react-webrtc-chat

React WebRTC chat
JavaScript
41
star
11

web-component

Lightweight library providing interface for building web components
JavaScript
39
star
12

webrtc-chat

Serverless chat application useing peer to peer WebRTC
JavaScript
29
star
13

react-peer-data

React wrapper for PeerData library for files, media streaming/sharing using WebRTC.
TypeScript
20
star
14

react-user-media

React wrapper for getUserMedia
TypeScript
19
star
15

shutdown

Simple go signals handler for performing graceful shutdown by executing callback function
Go
18
star
16

gocontainer

Simple Dependency Injection Container
Go
18
star
17

pushpull

gRPC message-oriented middleware on top of worker-pool, event ingestion and delivery system.
Go
10
star
18

peer-data-server

Signaling server, messaging service on Node using socket
JavaScript
10
star
19

invoice-bundle

Symfony invoice bundle
JavaScript
8
star
20

blockchain

Simple gRPC blockchain
Go
8
star
21

crud-bundle

Provides crud actions, crud bundle for Symfony
PHP
7
star
22

trace

Simple helper to trace the function calls, errors or logs reference
Go
7
star
23

list-bundle

Provides list builder, list view bundle for Symfony
PHP
4
star
24

golog

Go logger
Go
4
star
25

goquery

Go query builder for sql
Go
3
star
26

gocrud

Simple Go (Golang) CRUD provider
Go
3
star
27

angular-symfony-acl

ACL component for Angular Js based on symfony2 user roles
JavaScript
2
star
28

angular-gravatar

Angular gravatar component
JavaScript
2
star
29

mean-todos

MEAN (MongoDB, ExpressJS, Angular2, NodeJS) Todo List application
TypeScript
1
star
30

ng2-search

Angular 2 search module
JavaScript
1
star
31

angular2-chat

Socket.io Chat with NodeJS and Angular2
JavaScript
1
star
32

user-bundle

Simple symfony doctrine user bundle
PHP
1
star
33

ng2-pagination

Angular2 pagination module
TypeScript
1
star
34

angular-oauth2

Angular oauth2 provider
JavaScript
1
star
35

vardius.github.io

SCSS
1
star
36

angular2-github

GitHub User Search - Angular2 + Webpack App
JavaScript
1
star
37

menu-bundle

Simple symfony menu builder
PHP
1
star
38

angular2-spotify

Spotify - Angular2 + Webpack App
JavaScript
1
star
39

lru-cache

A Least Recently Used (LRU) Cache organizes items in order of use, allowing you to quickly identify which item hasn't been used for the longest amount of time.
Go
1
star
40

admin-bundle

Symfony admin bundle, CMS
JavaScript
1
star