{"id":26544096,"url":"https://github.com/lucasvmiguel/stock-api","last_synced_at":"2026-05-01T16:33:53.596Z","repository":{"id":44341900,"uuid":"511851451","full_name":"lucasvmiguel/stock-api","owner":"lucasvmiguel","description":"A Stock API is a REST API written in Go where products can be created, read, updated and deleted","archived":false,"fork":false,"pushed_at":"2023-07-29T12:26:24.000Z","size":431,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-04T22:13:08.873Z","etag":null,"topics":["chi","go","postgresql","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucasvmiguel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-08T10:14:56.000Z","updated_at":"2023-06-21T10:04:38.000Z","dependencies_parsed_at":"2023-01-31T12:00:32.165Z","dependency_job_id":null,"html_url":"https://github.com/lucasvmiguel/stock-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lucasvmiguel/stock-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasvmiguel%2Fstock-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasvmiguel%2Fstock-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasvmiguel%2Fstock-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasvmiguel%2Fstock-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucasvmiguel","download_url":"https://codeload.github.com/lucasvmiguel/stock-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasvmiguel%2Fstock-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32505106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chi","go","postgresql","rest-api"],"created_at":"2025-03-22T03:17:19.434Z","updated_at":"2026-05-01T16:33:53.578Z","avatar_url":"https://github.com/lucasvmiguel.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stock API\n\n[![Go](https://github.com/lucasvmiguel/stock-api/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/lucasvmiguel/stock-api/actions/workflows/build-and-test.yml)\n\n## Description\n\nA Stock API is a REST API written in Go where products can be created, read, updated and deleted.\n\nNote: _This API has been configured for `development` environment. To use in a `production` environment, further setup will be required._\n\n## Installing the app\n\n**Requirements:**\n\n- [Go](https://go.dev/) (\u003e= 1.20)\n- [Docker](https://www.docker.com/)\n- [Docker Compose](https://docs.docker.com/compose/install/)\n\nTo install the go modules required to run the application, run the following command:\n\n```bash\ngit clone git@github.com:lucasvmiguel/stock-api.git\ncd stock-api\nmake install\n```\n\n## Running the app\n\n1. Open a terminal and run the following command to start the persistence (database) required:\n\n```bash\nmake persistence-up\n```\n\n2. In another terminal, start the application with the following command:\n\n```bash\nmake run\n```\n\n## Testing\n\n### Unit test\n\n```\nmake test-unit\n```\n\n### Integration test\n\n1. Open a terminal and run the following command to start the persistence (database) required:\n\n```bash\nmake persistence-up\n```\n\n2. In another terminal, run the integration test with the following command:\n\n```bash\nmake test-integration\n```\n\n### Stress test\n\n**Requirements:**\n\n- [Apache AB](https://httpd.apache.org/docs/2.4/programs/ab.html)\n\n1. Open a terminal and run the following command to start the persistence (database) required:\n\n```bash\nmake persistence-up\n```\n\n2. In a new terminal, start the application with the following command:\n\n```bash\nmake run\n```\n\n3. In another terminal, run the stress test with the following command\n\n```bash\nmake test-stress\n```\n\n## Configuration\n\n- To configure how the app will run, check the following file: [.env](.env)\n- To configure how the app will be built and released, check the following file: [Makefile](Makefile)\n\n## Architecture\n\nThis section describes what are the goals of the system and some of its design and implementation.\n\n### Requirements\n\nThe following list shows all user requirements implemented by the system.\n\n- As a user, I can fetch all products using a REST API.\n- As a user, I can fetch a paginated list of products using a REST API.\n- As a user, I can fetch a product by its id using a REST API.\n- As a user, I can create a product using a REST API.\n- As a user, I can update some fields (`name` and/or `code`) of a product by its id using a REST API.\n- As a user, I can delete a product by its id using a REST API.\n\n### Schema\n\nThe following picture shows all the entities of the system.\n\n![schema](/assets/schema.png)\n\n### System Design\n\nThe following pictures shows some of the details of how the system is designed and implemented.\n\n![system design](/assets/system-design.png)\n![layers](/assets/layer.png)\n\n### Folder/File structure\n\n- `/cmd`: Main applications for this project.\n- `/cmd/api`: Package responsible for starting the API application.\n- `/cmd/api/starter`: Package containing all code required for starting the API application. (eg: config, routes, etc)\n- `/internal`: Private application and library code.\n- `/internal/product`: Product domain, where every code related to product should be placed. (Inspired by [DDD](https://en.wikipedia.org/wiki/Domain-driven_design))\n- `/pkg`: Library code that's ok to use by external applications (eg: `/pkg/mypubliclib`).\n- `/test`: Integration tests that run with external apps. (eg: database)\n- `/.github`: CI/CD from Github.\n- `docker-compose.yml`: Used to spin up the persistence layer in development and testing.\n- `.env`: configures project.\n- `Makefile`: Project's executable tasks.\n\nNote: _inspired by https://github.com/golang-standards/project-layout_\n\n### Stack\n\n- Language: [Go](https://go.dev/)\n- API/REST framework: [chi](https://github.com/go-chi/chi)\n- Database ORM: [GORM](https://gorm.io/)\n- Config reader: [godotenv](https://github.com/joho/godotenv)\n- Database: [Postgres](https://www.postgresql.org/)\n- Message Queue: [Asynq](https://github.com/hibiken/asynq)\n\n## API Docs\n\nIn this section is described the REST API's endpoints (URL, request, response, etc).\n\n\u003cdetails\u003e\n\u003csummary\u003eCreate product\u003c/summary\u003e\n\nEndpoint that creates a product\n\n#### Request\n\n```\nEndpoint: [POST] /api/v1/products\n\nHeaders:\n  Content-Type: application/json\n\nBody:\n  {\n    \"name\": \"Product name\",\n    \"stock_quantity\": 10\n  }\n```\n\n#### Response\n\n**Success**\n\n```\nStatus: 201\n\nBody:\n  {\n    \"id\": 1,\n    \"name\": \"Product name\",\n    \"code\": \"70a17d32-a670-4396-9706-bd0940152fc7\",\n    \"stock_quantity\": 10,\n    \"created_at\": \"2022-07-08T18:53:57.936433+01:00\",\n    \"updated_at\": \"2022-07-08T18:53:57.936433+01:00\"\n  }\n```\n\n**Bad Request**\n\n```\nStatus: 400\n```\n\n**Internal Server Error**\n\n```\nStatus: 500\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eGet products paginated\u003c/summary\u003e\n\nEndpoint to get products paginated\n\n#### Request\n\n##### Query Parameters\n\n- `cursor`: use the response's `next_cursor` field\n- `limit`: limit of products to be returned (min=1, max=100)\n\n```\nEndpoint: [GET] /api/v1/products?limit=10\u0026cursor=2\n\nHeaders:\n  Content-Type: application/json\n```\n\n#### Response\n\n**Success**\n\n```\nStatus: 200\n\nBody:\n  {\n    \"items\": [\n      {\n        \"id\": 1,\n        \"name\": \"foo\",\n        \"code\": \"70a17d32-a670-4396-9706-bd0940152fc7\",\n        \"stock_quantity\": 1,\n        \"created_at\": \"2022-07-08T18:53:57.936433+01:00\",\n        \"updated_at\": \"2022-07-08T18:53:57.936433+01:00\"\n      }\n    ],\n    \"next_cursor\": 2\n  }\n```\n\n**Bad Request**\n\n```\nStatus: 400\n```\n\n**Internal Server Error**\n\n```\nStatus: 500\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eGet all products\u003c/summary\u003e\n\nEndpoint to get all products (does not have pagination)\n\n#### Request\n\n```\nEndpoint: [GET] /api/v1/products/all\n\nHeaders:\n  Content-Type: application/json\n```\n\n#### Response\n\n**Success**\n\n```\nStatus: 200\n\nBody:\n  [\n    {\n      \"id\": 1,\n      \"name\": \"foo\",\n      \"code\": \"70a17d32-a670-4396-9706-bd0940152fc7\",\n      \"stock_quantity\": 1,\n      \"created_at\": \"2022-07-08T18:53:57.936433+01:00\",\n      \"updated_at\": \"2022-07-08T18:53:57.936433+01:00\"\n    }\n  ]\n```\n\n**Internal Server Error**\n\n```\nStatus: 500\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eGet product by id\u003c/summary\u003e\n\nEndpoint to get a product by id\n\n#### Request\n\n```\nEndpoint: [GET] /api/v1/products/{id}\n\nHeaders:\n  Content-Type: application/json\n```\n\n#### Response\n\n**Success**\n\n```\nStatus: 200\n\nBody:\n  {\n    \"id\": 1,\n    \"name\": \"foo\",\n    \"code\": \"70a17d32-a670-4396-9706-bd0940152fc7\",\n    \"stock_quantity\": 1,\n    \"created_at\": \"2022-07-08T18:53:57.936433+01:00\",\n    \"updated_at\": \"2022-07-08T18:53:57.936433+01:00\"\n  }\n```\n\n**Not Found**\n\n```\nStatus: 404\n```\n\n**Internal Server Error**\n\n```\nStatus: 500\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eUpdate product by id\u003c/summary\u003e\n\nEndpoint that updates a product by id\n\n#### Request\n\n```\nEndpoint: [PUT] /api/v1/products/{id}\n\nHeaders:\n  Content-Type: application/json\n\nBody:\n  {\n    \"name\": \"new product name\",\n    \"stock_quantity\": 5\n  }\n```\n\n#### Response\n\n**Success**\n\n```\nStatus: 200\n\nBody:\n  {\n    \"id\": 1,\n    \"name\": \"new product name\",\n    \"code\": \"70a17d32-a670-4396-9706-bd0940152fc7\",\n    \"stock_quantity\": 5,\n    \"created_at\": \"2022-07-08T18:53:57.936433+01:00\",\n    \"updated_at\": \"2022-07-08T18:53:57.936433+01:00\"\n  }\n```\n\n**Bad Request**\n\n```\nStatus: 400\n```\n\n**Not Found**\n\n```\nStatus: 404\n```\n\n**Internal Server Error**\n\n```\nStatus: 500\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDelete product by id\u003c/summary\u003e\n\nEndpoint to delete a product by id\n\n#### Request\n\n```\nEndpoint: [DELETE] /api/v1/products/{id}\n\nHeaders:\n  Content-Type: application/json\n```\n\n#### Response\n\n**Success**\n\n```\nStatus: 204\n```\n\n**Not Found**\n\n```\nStatus: 404\n```\n\n**Internal Server Error**\n\n```\nStatus: 500\n```\n\n\u003c/details\u003e\n\n## Configuration\n\nA file called `.env` has all config used in the project.\n\nIn the future, a service like [Doppler](https://www.doppler.com/) or [Vault](https://www.vaultproject.io/) could (and should) be used in the project.\n\n## CI/CD\n\nThe project uses Github CI to run tests, builds (and possibly deployments). You can see the badge below:\n\u003cbr /\u003e\n[![Go](https://github.com/lucasvmiguel/stock-api/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/lucasvmiguel/stock-api/actions/workflows/build-and-test.yml)\n\nSteps:\n\n1. Set up Go\n2. Build\n3. Unit Test\n4. Integration Test\n5. Log in to the Container registry (Github)\n6. Build and push Docker images\n\n## Important notes\n\n- command `make docker-run` in `development` will only work correctly if the container's network is configured right. (More info [here](https://docs.docker.com/config/containers/container-networking/))\n\n## Roadmap\n\n- `Improvement`: Add [Google's wire DI library](https://github.com/google/wire) to the project.\n- `Improvement`: If it's needed to add more entities (eg: [Product](internal/product/entity/product.go)), we might need to centralize all entities in just one package. (Something like a `entity` package) That way, we would prevent cycle dependencies. (Check [this link](https://www.reddit.com/r/golang/comments/vcy5xq/ddd_file_structure_cyclic_dependencies/))\n- `Improvement`: API docs are being described on the Readme. However, [OpenAPI](https://swagger.io/specification/) might be a good improvement in the future.\n- `Improvement`: Using a secret management service like [Doppler](https://www.doppler.com/) or [Vault](https://www.vaultproject.io/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasvmiguel%2Fstock-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucasvmiguel%2Fstock-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasvmiguel%2Fstock-api/lists"}