This project is a REST API built in Go that performs CRUD (Create, Read, Update, Delete) operations and provides data from a persistent backend MySQL database. The API returns responses in JSON format, making it easy to integrate with various front-end applications and services.
The project utilizes the following libraries:
- go-sql-driver/mysql: A MySQL driver for Go's database/sql package, used for database operations.
- gorilla/mux: A powerful URL router and dispatcher for matching incoming requests to their respective handler functions.
With this setup, the API ensures efficient and reliable data handling and routing, making it a robust solution for backend services.
- Requirements
- Setup
- Running the Application
- API Endpoints
- Running Tests
- Code Overview
- Continuous Integration
- Contributions
- License
- Go 1.22.2 or later
- MySQL
-
Clone the repository:
git clone https://github.com/deviant101/REST-API.git cd REST-API
-
Install dependencies:
go mod download
-
Set up MySQL:
Ensure MySQL is running and create a database for the application. Update the database credentials in the environment variables.
-
Set Environment Variables:
Create a
.env
file in the root directory of the project and add the following environment variables:DB_HOST=localhost DB_PORT=3306 DB_USER=root DB_PASSWORD=yourpassword DB_NAME=yourdatabase
-
Docker Setup:
This project includes Docker files to set up the entire stack easily. Ensure you have Docker installed on your machine.
-
Build the Docker image:
docker build -t rest-api .
-
Run the Docker container:
docker run -d -p 8080:8080 --env-file .env rest-api
-
-
Docker Compose Setup:
This project includes a
docker-compose.yml
file to set up the entire stack, including MySQL, easily. Ensure you have Docker and Docker Compose installed on your machine.-
Update the
.env
file to use the MySQL container:DB_HOST=db DB_PORT=3306 DB_USER=root DB_PASSWORD=yourpassword DB_NAME=yourdatabase
-
Start the services using Docker Compose:
docker-compose up -d
-
To run the application, use the following command:
go run main.go
- GET/products: Retrieve all products.
- GET/product/{id}: Retrieve a product by ID
- POST/product: Create a new product.
- PUT/product/{id}: Update a product by ID.
- DELETE/product/{id}: Delete a product by ID.
curl -X GET http://localhost:8080/products
curl -X POST http://localhost:8080/product -d '{"name": "New Product", "price": 100.00}'
curl -X PUT http://localhost:8080/product/1 -d '{"name": "Updated Product", "price": 150.00}'
curl -X DELETE http://localhost:8080/product/1
To ensure the functionality and reliability of the API, a suite of tests has been included. These tests can be run using the Go testing framework.
-
Run all tests:
To run all the tests in the project, use the following command:
go test ./...
-
View detailed test output:
To see detailed output from the tests, use the
-v
(verbose) flag:go test -v ./...
These commands will help you verify that your API is functioning correctly and that any changes you make do not introduce new issues.
main.go
The entry point of the application. It initializes the application and starts the server.
app.go
Defines the application structure and routes:
func (app *App) handleRoutes() {
app.Router.HandleFunc("/products", app.getProducts).Methods("GET")
app.Router.HandleFunc("/product/{id}", app.getProduct).Methods("GET")
app.Router.HandleFunc("/product", app.createProduct).Methods("POST")
app.Router.HandleFunc("/product/{id}", app.updateProduct).Methods("PUT")
app.Router.HandleFunc("/product/{id}", app.deleteProduct).Methods("DELETE")
}
app_test.go
Contains tests for the API:
func TestGetProduct(t *testing.T) {
clearTable()
addProduct("Product1", 10, 100.00)
request, _ := http.NewRequest("GET", "/products", nil)
response := sendRequest(request)
checkStatusCode(t, http.StatusOK, response.Code)
}
go.mod
Module dependencies:
module github.com/deviant101/REST-API
go 1.22.2
require (
github.com/go-sql-driver/mysql v1.8.1
github.com/gorilla/mux v1.8.1
)
require filippo.io/edwards25519 v1.1.0 // indirect
This project includes a Continuous Integration (CI) pipeline configured using GitHub Actions. The CI pipeline ensures that the codebase remains stable and that all tests pass before any changes are merged into the main branch.
- Automated Testing: The pipeline automatically runs all tests in the project whenever a new commit is pushed or a pull request is opened. This helps catch any issues early in the development process.
- Dependency Installation: The pipeline installs all necessary dependencies, ensuring that the environment is correctly set up for testing.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to customize this README.md
file further based on your specific project details and requirements.