Skip to content

Project aim is share knowledge about golang, microservice, grpc, rest and database.

License

Notifications You must be signed in to change notification settings

yurivvdantas/go-microservice-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-microservice-sample

Project aim is share knowledge about golang, microservice, grpc, rest and database.

What you will found here

This is a project with a simulation of a rest service exposes and with call internal grpc. The context is a cryptcurrency service with is possible to get info of registered coins, upvotes, adding a new coin and have a streaming with the real time upvotes given a cryptocurrency. In this way, its possible test grpc with a browser or with a rest client.

Endpoints

Get infos given a crypto id:

curl --request GET \
  --url http://localhost:8080/crypto/1

Get all registred cryptos

curl --request GET \
  --url http://localhost:8080/crypto/

Upvote a crypto by id:

curl --request POST \
  --url http://localhost:8080/crypto/1/upvote 

Register a new crypto:

curl --request POST \
  --url http://localhost:8080/crypto/ \
  --header 'Content-Type: application/json' \
  --data '{
"name":"Ethereum",
"code":"ETC",
"description":"A amazing coin"
}'

Stream with a live crypto upvote by id:

curl --request GET \
  --url http://localhost:8080/crypto/1/upvote

Struct of project

I have inspiration on this repo .

Architecture of project

Demonstration of de architecture This diagram was made on Excalidraw

There is two service on this project (so far), a gateway rest service and a grpc service. The gateway will receive a rest requisition, call grpc on another service and return a json to the client.

Setup

Database

A complete instrunctions how to install and do a initial config of a database using golang, docker and MySql can be found on this link of the offcial documentation.

I have set de password "admin" for this project and the user "root".

After this, must have create a database on the docker console image of the MySql: create database cryptos;

And then: use cryptos;

Run the script Create_crypto_table.sql for init the database.

Prerequists

For running this application, you have to:

-Configure database

-Installed golang

-Installed protobuf

Commands

Use the next command for generating or updating of protobuf files:

protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    api/crypto.proto

Tests

For run tests, use command from the root path of project:

go test ./test/

This will run all tests in location of tests folder.

For run all tests, the database must be on. Maybe in future i fix that making a mock of a database connection or something like that.

Running

Must have running two application: gateway_main and crypto_votes_service_main. For run them just go to the path on a console and use the command:

go run gateway_main.go
go run crypto_votes_service_main.go

Or just use VScode navigate to the files and press F5. For execute a second service, you have to access Run menu and then Start Debuggin.

About

Project aim is share knowledge about golang, microservice, grpc, rest and database.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages