A simple Go service that provides a RESTful API for retrieving emission and air pollution data for all countries over the last 300 years. This microservice serves as a backend service that handles requests to fetch historical environmental data, including CO₂ emissions, air pollutants, and other environmental factors, for every country around the globe.
The required raw data can be downloaded here https://www.kaggle.com/datasets/rejeph/air-pollution.
Name | HTTP Method | Route |
---|---|---|
OpenAPI Documentation | GET | /swagger/index.html |
List all countries | GET | /countries |
Returns a single country | GET | /countries/{id} |
List all emissions of all countries for each year | GET | /emissions/year/ |
Get all emissions of a specific year | GET | /emissions/year/{id} |
List all emissions of all years for each country | GET | /emissions/country/ |
Get all emissions of a specific country | GET | /emissions/country/{id} |
Prometheus Metrics | GET | /metrics |
Name | Default | Route |
---|---|---|
SERVER_PORT | 8080 | Port used by the service. |
AIR_POLLUTION_FILE | air-pollution.csv | Path to CSV file with raw data. |
- Download the raw data to a file, e.g.
/data/air-pollution.csv
. - Point env variable
AIR_POLLUTION_FILE
to the file path. - Run the service:
When the Go SDK is installed, the service can be installed and run with the following commands:
go install github.com/HBTGmbH/air-pollution-service@latest
air-pollution-service
The binary air-pollution-service
is now available in the $GOPATH/bin
directory, such as ~/go/bin/
by default.
When working with the source code, the service can be run with the following command:
go run main.go
- Download the raw data to a file, e.g.
/data/air-pollution.csv
.
docker pull ghcr.io/hbtgmbh/air-pollution-service:latest
docker run -v /data:/data -p 8080:8080 -e AIR_POLLUTION_FILE=/data/air-pollution.csv ghcr.io/hbtgmbh/air-pollution-service:latest
go build -o server
docker build -t air-pollution-service .
make build
For the Swagger API documentation, the project https://github.com/swaggo/swag is used. This generates the Swagger API documentation and integrates with a web framework (as such "Chi" used here) to render the Swagger Web UI at runtime.
The sources of this api-pollution-service come with an up-to-date generated Swagger documentation to facilitate easier Go compilation after checking out this project, as well as to make use of Git diffs between commits to review changes in the generated files.
To (re-)generate the Swagger API documentation, the swag
CLI command must be installed:
go install github.com/swaggo/swag/cmd/swag@latest
After that, the documentation inside of the docs/
directory can be (re-)generated by running:
swag init
Authentication: #3