This is an API service written in Go for Inventory Management.
The purpose of this project is to demonstrate how to develop a complete API service with Database (MySQL) integration and configuration management in Go. The primary packages used in this project are:
- github.com/gorilla/mux
- github.com/go-sql-driver/mysql
- github.com/spf13/viper
For a much scalable & expandable implementation using hexagonal architecture, view the branch, hexagonal-architecture
The following needs to be installed before starting:
- go (1.19)
- docker
To get started with the project locally, first download the required go packages
go mod download
To build the service run the following command:
go build -o inventory ./cmd/main.go
The above command would generate a binary, inventory
, in the project's root. Execute the binary to start the service:
./inventory
Note: The service would expect a MySQL server running on
localhost:3306
by default. If the database server is not running, the inventory service would exit with aconnection refused
error. Make sure you have a MySQL server running prior to running this service.
Following environment variables needs to be set:
Variable | Description | Default Value |
---|---|---|
MYSQL_HOST | MySQL Server Host | "127.0.0.1" |
MYSQL_PORT | MySQL Server Port | 3306 |
MYSQL_USER | MySQL Server Database User | "inventory_admin" |
MYSQL_PASSWORD | MySQL Server User Password | "password" |
MYSQL_DATABASE | MySQL Server Database Name | "inventory" |
SERVER_PORT | Port on which inventory server would be exposed | "8080" |
Alternatively, a config.toml
file in the project's root may be used to set these variables.
As an alternate, docker compose can also be used to build and run the server.
To build the docker image of the inventory server, run the following command:
docker compose build
After the build is complete, run the following command to start the server:
docker compose up
Note: You don't need to spin up a MySQL server before hand in this case, since docker compose takes care of that and starts a MySQL server for you.
Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request