An open API service indexing awesome lists of open source software.

https://github.com/alamgir-ahosain/go-postgres-crud-rest-api

Build a CRUD REST API in Go (Golang) using PostgreSQL database and Gorilla Mux framework.
https://github.com/alamgir-ahosain/go-postgres-crud-rest-api

crud-api go golang gorilla-mux postgresql rest-api

Last synced: about 2 months ago
JSON representation

Build a CRUD REST API in Go (Golang) using PostgreSQL database and Gorilla Mux framework.

Awesome Lists containing this project

README

          

## GO PostgreSQL CRUD REST API
Build a `CRUD` `REST API` in `Go (Golang)` using `PostgreSQL` database and `Gorilla/Mux` framework.

>For PostgreSQL
## Installation
1. **Clone the Repository**
```bash
https://github.com/alamgir-ahosain/go-postgres-crud-rest-api.git
```
2. **Install Dependencies**

```bash
go get github.com/lib/pq
go get -u github.com/gorilla/mux
go get github.com/joho/godotenv
```

## Database Setup
Login to PostgreSQL and create the database and table:
```sql
CREATE DATABASE goDB;
USE goDB;

CREATE TABLE users (
id SERIAL PRIMARY KEY,
sid VARCHAR(128) NOT NULL,
name VARCHAR(128) NOT NULL,
cgpa NUMERIC(5,2)
);
```
## Run the Application:
1. Start PostgreSQL server.
2. Update database connection in `db/postgreSql.go`
3. Configure `.env` in `config/.env`
4. Run the Project:
```go
go run main.go
```

## Project Structure
```plaintext
GO-POSTGRES-CRUD-REST-API

│── cmd/
│ │─── MyApp
│ │ ├── server.go # main serve file
│ │
│── internal/
│ ├── api/
│ │ ├── handlers/
│ │ │ ├── get_users/
│ │ │ │ ├── get_user_by_id.go # get user by ID function
│ │ │ │ ├── get_users.go # get all users function
│ │ │ │── create_user.go # create user function
│ │ │ │── delete_user.go # delete user function
│ │ │ │── update_user.go # update user function
│ │ ├── routes/
│ │ └── routes.go # all routes declared here
│ │
│ │── config/
│ │ │── .env # configuration environment
│ │
│ │── db/
│ │ │── postgreSql.go # postgreSQL database connection setup
│ │
│ │── models/
│ │ │── model.go # User model definition
│ │
│ │── services/
│ │ │── service.go # Helper function

│── main.go # Main entry file
│── go.mod # Go module definition
│── go.sum # Go dependencies checksums
│── README.md # Project documentation

```
---
## API endpoints table

| number | method | endpoint | description |
| ------ | ------ | -------------- | ------------------------------|
| 1 | GET | /users | get all users |
| 2 | GET | /users/{id} | get user by id |
| 3 | POST | /users | create new user |
| 4 | PUT | /users/{id} | full update user by id |
| 5 | DELETE | /users/{id} | delete user by id |

## Testing the CRUD API with Postman or Thunder Client( VS code extension)
#### 1. Get All Users
Method: GET
URL: http://localhost:8000/users


#### 2. Get a single User
Method: GET
URL: http://localhost:8000/users/1


#### 3. Create a new Users
Method: POST
URL: http://localhost:8000/users


#### 4. Update a User
Method: PUT
URL: http://localhost:8000/users/1

#### 5. Delete a User
Method: DELETE
URL: http://localhost:8000/users/1

---
# Thank You for Checking Out This Repository!
Your feedback is valuable! Please share your thoughts and suggestions for improvement via [GitHub Issues](https://github.com/alamgir-ahosain/go-postgres-crud-rest-api/issues).

# Contributing
Contributions are welcome! Feel free to fork the repo and create a pull request.