Skip to content

i7tsov/example-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example microservice

This is an example microservice that iimplements HTTP API for adding users to database and listing existing users.

Building

Use build.sh script, it will set the correct version of the binary.

Otherwise standard go build ./cmd/server works as well for the developement purposes.

Configuring

To find list of applicable config options, run

./bin/server --help

Service accepts all of command line flags, environment variables or config file in YAML format.

Config file config.yaml is first checked at the current folder, then in /etc/<service-name>.

Example config file is located in the root of repository.

Options from the environment variables are using capital case with underscores instead of dots.

Full example:

export SERVER_PORT=11111
./bin/server --log.level=debug
# other options are taken from config.yaml

Running database

To run postgres database in container, first install docker. Then, run:

docker run --name exampledb -e POSTGRES_PASSWORD=example -e POSTGRES_USER=example -e POSTGRES_DB=users -v ~/db/example:/var/lib/postgresql/data -d -p 5432:5432 postgres:16.3-alpine3.20

Running service

Use:

./bin/server

Alternatively, build temporary executable and run:

go run ./cmd/server

Example requests

Add user:

curl localhost:10002/v1/users -X POST -d '{"name": "Joe", "email": "example@domain.org"}'

Output:

{"id":"4ea7e667-88aa-4dad-9e39-772ec8848da5"}

List users:

curl localhost:10002/v1/users -X GET

Output:

{"users":[{"id":"4ea7e667-88aa-4dad-9e39-772ec8848da5","name":"Joe","email":"example@domain.org"}]}

About

Example microservice written in idiomatic Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published