|
| 1 | +# REST API in Go |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +The project utilizes the following libraries: |
| 8 | +- **go-sql-driver/mysql**: A MySQL driver for Go's database/sql package, used for database operations. |
| 9 | +- **gorilla/mux**: A powerful URL router and dispatcher for matching incoming requests to their respective handler functions. |
| 10 | + |
| 11 | +With this setup, the API ensures efficient and reliable data handling and routing, making it a robust solution for backend services. |
| 12 | + |
| 13 | +## Table of Contents |
| 14 | + |
| 15 | +- [Requirements](#requirements) |
| 16 | +- [Setup](#setup) |
| 17 | +- [Running the Application](#running-the-application) |
| 18 | +- [API Endpoints](#api-endpoints) |
| 19 | +- [Running Tests](#running-tests) |
| 20 | +- [Code Overview](#code-overview) |
| 21 | +- [Continuous Integration](#continuous-integration) |
| 22 | +- [Contributions](#contributions) |
| 23 | +- [License](#license) |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +## Requirements |
| 28 | + |
| 29 | +- Go 1.22.2 or later |
| 30 | +- MySQL |
| 31 | + |
| 32 | +## Setup |
| 33 | + |
| 34 | +1. **Clone the repository:** |
| 35 | + |
| 36 | + ```sh |
| 37 | + git clone https://github.com/deviant101/REST-API.git |
| 38 | + cd REST-API |
| 39 | + ``` |
| 40 | + |
| 41 | +2. **Install dependencies:** |
| 42 | + |
| 43 | + ```sh |
| 44 | + go mod download |
| 45 | + ``` |
| 46 | + |
| 47 | +3. **Set up MySQL:** |
| 48 | + |
| 49 | + Ensure MySQL is running and create a database for the application. Update the database credentials in the environment variables. |
| 50 | + |
| 51 | + |
| 52 | +## Running the Application |
| 53 | + |
| 54 | +To run the application, use the following command: |
| 55 | + |
| 56 | +```sh |
| 57 | +go run main.go |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | +## API Endpoints |
| 62 | + |
| 63 | +- **GET/products:** Retrieve all products. |
| 64 | +- **GET/product/{id}:** Retrieve a product by ID |
| 65 | +- **POST/product:** Create a new product. |
| 66 | +- **PUT/product/{id}:** Update a product by ID. |
| 67 | +- **DELETE/product/{id}:** Delete a product by ID. |
| 68 | + |
| 69 | +### Example Requests |
| 70 | + |
| 71 | +### GET /products |
| 72 | +```sh |
| 73 | +curl -X GET http://localhost:8080/products |
| 74 | +``` |
| 75 | + |
| 76 | +### POST /products |
| 77 | +```sh |
| 78 | +curl -X POST http://localhost:8080/product -d '{"name": "New Product", "price": 100.00}' |
| 79 | +``` |
| 80 | +### PUT /product/{id} |
| 81 | +```sh |
| 82 | +curl -X PUT http://localhost:8080/product/1 -d '{"name": "Updated Product", "price": 150.00}' |
| 83 | +``` |
| 84 | + |
| 85 | +### DELETE /product/{id} |
| 86 | +```sh |
| 87 | +curl -X DELETE http://localhost:8080/product/1 |
| 88 | +``` |
| 89 | + |
| 90 | +## Running Tests |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +1. **Run all tests:** |
| 95 | + |
| 96 | + To run all the tests in the project, use the following command: |
| 97 | + |
| 98 | + ```sh |
| 99 | + go test ./... |
| 100 | + ``` |
| 101 | + |
| 102 | +2. **View detailed test output:** |
| 103 | + |
| 104 | + To see detailed output from the tests, use the `-v` (verbose) flag: |
| 105 | + |
| 106 | + ```sh |
| 107 | + go test -v ./... |
| 108 | + ``` |
| 109 | + |
| 110 | +These commands will help you verify that your API is functioning correctly and that any changes you make do not introduce new issues. |
| 111 | + |
| 112 | +## Code Overview |
| 113 | +`main.go` The entry point of the application. It initializes the application and starts the server. |
| 114 | + |
| 115 | +`app.go` Defines the application structure and routes: |
| 116 | +```sh |
| 117 | +func (app *App) handleRoutes() { |
| 118 | + app.Router.HandleFunc("/products", app.getProducts).Methods("GET") |
| 119 | + app.Router.HandleFunc("/product/{id}", app.getProduct).Methods("GET") |
| 120 | + app.Router.HandleFunc("/product", app.createProduct).Methods("POST") |
| 121 | + app.Router.HandleFunc("/product/{id}", app.updateProduct).Methods("PUT") |
| 122 | + app.Router.HandleFunc("/product/{id}", app.deleteProduct).Methods("DELETE") |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +`app_test.go` Contains tests for the API: |
| 127 | +```sh |
| 128 | +func TestGetProduct(t *testing.T) { |
| 129 | + clearTable() |
| 130 | + addProduct("Product1", 10, 100.00) |
| 131 | + request, _ := http.NewRequest("GET", "/products", nil) |
| 132 | + response := sendRequest(request) |
| 133 | + checkStatusCode(t, http.StatusOK, response.Code) |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +`go.mod` Module dependencies: |
| 138 | +```sh |
| 139 | +module github.com/deviant101/REST-API |
| 140 | +
|
| 141 | +go 1.22.2 |
| 142 | +
|
| 143 | +require ( |
| 144 | + github.com/go-sql-driver/mysql v1.8.1 |
| 145 | + github.com/gorilla/mux v1.8.1 |
| 146 | +) |
| 147 | +require filippo.io/edwards25519 v1.1.0 // indirect |
| 148 | +``` |
| 149 | + |
| 150 | +## Continuous Integration |
| 151 | + |
| 152 | +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. |
| 153 | + |
| 154 | +### CI Pipeline Features |
| 155 | + |
| 156 | +- **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. |
| 157 | +- **Dependency Installation:** The pipeline installs all necessary dependencies, ensuring that the environment is correctly set up for testing. |
| 158 | + |
| 159 | +The CI pipeline is defined in the [`.github/workflows/ci.yaml`](command:_github.copilot.openRelativePath?%5B%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fdeviant%2FData%2FREST-API%2F.github%2Fworkflows%2Fci.yaml%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%5D "/home/deviant/Data/REST-API/.github/workflows/ci.yaml") file. Below is an example of what the configuration might look like: |
| 160 | + |
| 161 | +## Contributions |
| 162 | + |
| 163 | +Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes. |
| 164 | + |
| 165 | +## License |
| 166 | + |
| 167 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 168 | + |
| 169 | +Feel free to customize this [`README.md`](command:_github.copilot.openRelativePath?%5B%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fdeviant%2FData%2FREST-API%2FREADME.md%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%5D "/home/deviant/Data/REST-API/README.md") file further based on your specific project details and requirements. |
0 commit comments