Let's built a ROBUST microservice.
Just for func
go func(){
}()
- RESTful operations
- DB connection with
gorm
and RAW SQL usage alongside GORM - CLI tooling with Cobra
- Queues and Messaging with
RabbitMQ
orRedis
. - Logging Errors
- Authentication and Authorization Middlewares
- CORS with Gorilla Handlers
- Unit Testing
- GraphQL APIs
- gRPC
- Docker (containerization)
- CI/CD
- Grafana and Prometheus Integrations (Later on)
For set up on your machine .
- Clone the repo
git clone https://github.com/vonmutinda/crafted.git
. - Run
go mod init
to check if go modules is already initialized. - Touch
.env
file and paste the following configurations.
ENV="local"
PORT=":9000"
DB_DRIVER="postgres" # <-provide your own-->
DB_HOST="localhost"
DB_PORT="5432"
DB_USER="username" # <-provide your own-->
DB_NAME="db_name" # <-provide your own-->
DB_PASS="db_pass" # <-provide your own-->
API_SECRET="ajsdlfjeo129pusfgan309rudlnlh34ouofOU&)O&UO#jr" # can you keep a secret?
If you are using a different db
from postgres
, make sure you import its corresponding dialect in package database
package database
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/mysql"
)
// rest of code goes here
Create db
and add it's name in .env
file.
Run go run main.go crafted
or go build && ./crafted crafted
Install Docker
Start RabbitMQ container
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
Since we constantly want to listen for any messages hitting the queue, run the cobra command
go run main.go consume
You realise this application is growing too big and once users begin interacting with it in production, We'll need a way to know where it fails.
Later on we'll configure our logger and other parts of our app with Prometheus
are Grafana.
Here's a list of technologies used in this project
- Golang version
go1.13.7
- Cobra
- gorilla/mux HTTP framework. You could as well use Gin
- Gorm ORM. However I'd highly recommend writing raw SQL.
- RabbitMQ Messaging and Queues.
- JWT (JSON Web Tokens)
- Use
Sentence case
when naming funcs intended for global usage. - Receivers must be pointers.
- Channels are used when Feedback is expected from a
go routine
- Waitgroups are used when we don't care about Feedback. We only want the job done.
- A WaitGroup is of type
sync.WaitGroup
but we use a pointer of that type in ago routine
.
Below are helpful resources on where to read more about Go/Golang
.
- Go docs
- Go by Example
- Nic Jackson's Tutorial Series