This code is 100% in compliance with golint, go_vet and gofmt. Check this for more details:
API Cab Data provides rest endpoints to query how many trips a particular cab made for a pick up date(using date part in pick up datetime). Endpoint accepts one or many medallions(comma separated) and returns trips made by each medallion. Results are cached for faster access next time. Endpoint allows user to bypass cache for results.
/trips/v1/medallion/:medallion/pickupdate/:pickupdate?bypasscache=:bypasscache
- GET
API provides a second endpoint to query trips by medallions
/trips/v1/medallions/:medallions?bypasscache=:bypasscache
- GET
API provides a third endpoint to clear the cache entries
/trips/v1/cache/contents
- DELETE
API Health Check
/health
- GET
Application is designed with a simple three layered architecture.
Controller -> Service -> Data Access Objects
GO version 1.9 is used for building the app with MYSQL server the database providing cab trip data.
Projects have been packaged based on their responsibility(SINGLE RESPONSIBILITY principle)
- handler -> Responsible for handling http requests.
- wiring -> Initialisation and Wiring up the components.
- database -> Data access layer for DB operations.
- service -> Business logic layer that interacts with database and cache
- cache -> Provides interface to Get / Set / Clear cache entries
- output -> Defines the output JSON structure
- github.com/gorilla/mux (http request routing and dispatching)
- github.com/muesli/cache2go (Concurrency-safe golang caching library with expiration capabilities)
- go.uber.org/zap (provides fast, structured, leveled logging)
- github.com/pkg/errors(error handling)
- github.com/stretchr/testify (unit test suite, mocking and assertion)
- gopkg.in/DATA-DOG/go-sqlmock.v1(SQL mocking library)
- github.com/jmoiron/sqlx (lib with set of extensions on go's standard database/sql library)
Dep is the dependency management tool.
- Follows data table approach
- Consistent pattern using Args/Fields/Want format
- Assertion/Mocking using testify
- Supplied through .env to run locally
- Docker env file .env.docker
- Git (just to clone the repo)
- Docker and Docker-compose
Clone this repository
https://github.com/nikhil-github/api-cab-data.git
Note : Please run the database migration before consuming the API.
make run
make run-docker
API will be listening on port 3000 , endpoint:
http://localhost:3000/trips/v1/medallion/:medallion/pickupdate/:pickupdate?bypasscache=:bypasscache
http://localhost:3000/trips/v1/medallions/:medallions?bypasscache=:bypasscache
make
- build the projectmake fmt
- format the codebase usinggo fmt
andgoimports
make test
- run unit tests for the project
make start-db
- start a mysql database containerdocker exec -it mysql bash
- log in to mysql containermysql -u root -p cabtrips < ny_cab_data_cab_trip_data_full.sql
- load SQL
password : password
Import SQL takes a little while (~30 minutes) due to the large size of the SQL
Migration is required just one time unless DB volumes are removed.
Simple client is added to the project that consumes the rest endpoints. To run the client
go run cmd/api-cab-data-client/main.go
- Request trips by medallion on pickup date
curl http://localhost:3000/trips/v1/medallion/67EB082BFFE72095EAF18488BEA96050/pickupdate/2013-12-31?bypasscache=true
{
"medallion": "67EB082BFFE72095EAF18488BEA96050",
"trips": 39
}
- Request with multiple medallions
curl http://localhost:3000/trips/v1/medallions/67EB082BFFE72095EAF18488BEA96050,D7D598CD99978BD012A87A76A7C891B7?bypasscache=true
[
{
"medallion": "67EB082BFFE72095EAF18488BEA96050",
"trips": 39
},
{
"medallion": "D7D598CD99978BD012A87A76A7C891B7",
"trips": 3
}
]
- Clear Cache
curl -X DELETE http://localhost:3000/trips/v1/cache/contents
Cache Cleared
- No requirement for distributed cache and in memory caching is allowed.
- The date format used in this API is YYYY-MM-DD.
- By Passing cache is an optional parameter and if no supplied its value is false.
- Endpoint allows maximum of 100 medallions per request.
- Secrets/Configs are supplied as env variables.