|
1 |
| -# golang-crud-rest-api-gin |
| 1 | +# Golang CRUD REST API with Gin |
| 2 | + |
| 3 | +This is a simple CRUD (Create, Read, Update, Delete) REST API in Go using the Gin web framework. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +Follow these instructions to get a copy of the project up and running on your local machine. |
| 8 | + |
| 9 | +### Prerequisites |
| 10 | + |
| 11 | +- Go (at least version 1.11) |
| 12 | + |
| 13 | +### Cloning the Project |
| 14 | + |
| 15 | +```bash |
| 16 | +git clone https://github.com/Shikha-code36/golang-crud-rest-api-gin.git |
| 17 | +cd golang-crud-rest-api-gin |
| 18 | +``` |
| 19 | + |
| 20 | +### Initializing the Project |
| 21 | + |
| 22 | +```bash |
| 23 | +go mod init github.com/Shikha-code36/golang-crud-rest-api-gin |
| 24 | +``` |
| 25 | + |
| 26 | +### Downloading Dependencies |
| 27 | + |
| 28 | +```bash |
| 29 | +go mod tidy |
| 30 | +``` |
| 31 | + |
| 32 | +## Running the Application |
| 33 | + |
| 34 | +```bash |
| 35 | +go run main.go |
| 36 | +``` |
| 37 | + |
| 38 | +The application will start running at `http://localhost:8080`. |
| 39 | + |
| 40 | +## Explanation of Code |
| 41 | + |
| 42 | +### main.go |
| 43 | + |
| 44 | +The `main.go` file is the entry point of our application. Here's a breakdown of its structure and functionality: |
| 45 | + |
| 46 | +```go |
| 47 | +package main |
| 48 | +``` |
| 49 | +This line defines the package that this file belongs to. In Go, the `main` package is special. It's the entry point of the application, and it must contain a `main` function. |
| 50 | + |
| 51 | +```go |
| 52 | +import ( |
| 53 | + "github.com/gin-gonic/gin" |
| 54 | + "github.com/Shikha-code36/golang-crud-rest-api-gin/handlers" |
| 55 | +) |
| 56 | +``` |
| 57 | +This block imports two packages. The first one, `github.com/gin-gonic/gin`, is the Gin web framework that we're using to build our API. The second one, `github.com/Shikha-code36/golang-crud-rest-api-gin/handlers`, is the package that contains our handler functions. |
| 58 | + |
| 59 | +```go |
| 60 | +func main() { |
| 61 | + r := gin.Default() |
| 62 | +``` |
| 63 | +This is the `main` function where our application starts. `gin.Default()` creates a new Gin engine with some default middleware (logger and recovery). This engine is used to set up our routes and start the server. |
| 64 | +
|
| 65 | +```go |
| 66 | + r.GET("/users", handlers.GetUsers) |
| 67 | + r.GET("/user/:id", handlers.GetUser) |
| 68 | + r.POST("/create_user", handlers.CreateUser) |
| 69 | + r.PUT("/update_user/:id", handlers.UpdateUser) |
| 70 | + r.DELETE("/delete_user/:id", handlers.DeleteUser) |
| 71 | +``` |
| 72 | +These lines define our API routes. For each route, we specify an HTTP method (GET, POST, PUT, DELETE), a path, and a handler function. When a request is received that matches a route's method and path, the corresponding handler function is called. |
| 73 | +
|
| 74 | +```go |
| 75 | + r.Run() |
| 76 | +} |
| 77 | +``` |
| 78 | +Finally, `r.Run()` starts the Gin server and begins listening for requests. By default, it listens on `localhost:8080`, but you can pass a different address as an argument to `Run` if you want. |
| 79 | + |
| 80 | +That's the basic structure of our `main.go` file. It sets up our server, defines our routes, and starts the server. |
| 81 | + |
| 82 | +### users.go in handlers |
| 83 | + |
| 84 | +The `users.go` file in the `handlers` directory contains the handler functions for our routes. You can find the detailed explanation [here](handlers). |
| 85 | + |
| 86 | +## API Endpoints |
| 87 | + |
| 88 | +- **GET /users**: Fetch all users. |
| 89 | +- **GET /user/:id**: Fetch a user by ID. |
| 90 | +- **POST /create_user**: Create a new user. The request body should be a JSON object with a `name` field, like this: `{"name": "Alice"}`. The ID will be assigned automatically. |
| 91 | +- **PUT /update_user/:id**: Update a user by ID. The request body should be a JSON object with a `name` field, like this: `{"name": "Bob"}`. |
| 92 | +- **DELETE /delete_user/:id**: Delete a user by ID. |
| 93 | + |
| 94 | +## Testing the API |
| 95 | + |
| 96 | +You can use a tool like curl or Postman to test the API. Here are some examples: |
| 97 | + |
| 98 | +- Fetch all users: |
| 99 | + |
| 100 | +```bash |
| 101 | +curl -X GET http://localhost:8080/users |
| 102 | +``` |
| 103 | + |
| 104 | +- Fetch a user by ID: |
| 105 | + |
| 106 | +```bash |
| 107 | +curl -X GET http://localhost:8080/user/1 |
| 108 | +``` |
| 109 | + |
| 110 | +- Create a new user: |
| 111 | + |
| 112 | +```bash |
| 113 | +curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice"}' http://localhost:8080/create_user |
| 114 | +``` |
| 115 | + |
| 116 | +- Update a user by ID: |
| 117 | + |
| 118 | +```bash |
| 119 | +curl -X PUT -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/update_user/1 |
| 120 | +``` |
| 121 | + |
| 122 | +- Delete a user by ID: |
| 123 | + |
| 124 | +```bash |
| 125 | +curl -X DELETE http://localhost:8080/delete_user/1 |
| 126 | +``` |
0 commit comments