This is a simple CRUD application built with Go, GORM, PostgreSQL, and Gin.
- User registration with password hashing
- JWT-based authentication
- User login with password verification
- CRUD operations for users
- Soft and hard delete options
- Go 1.23.4 or later
- PostgreSQL
- Git
-
Clone the repository:
git clone https://github.com/Rudra-Sankha-Sinhamahapatra/Go-Crud cd Go-Crud
-
Install dependencies:
go install github.com/githubnemo/CompileDaemon@latest go get github.com/joho/godotenv go get -u github.com/gin-gonic/gin go get -u gorm.io/gorm go get -u gorm.io/driver/postgres go get -u github.com/golang-jwt/jwt/v5 go get -u golang.org/x/crypto/bcrypt
-
Set up the environment variables:
cp .env.example .env
-
Run database migrations:
go run ./src/migrate/migrate.go
-
Clean up the module dependencies:
go mod tidy
-
Start the application:
CompileDaemon -directory=./src -command="go run ./src/main.go"
-
The application will be available at
http://localhost:8000
.
GET /
: Health check endpoint.POST /create-user
: Create a new user and generate a JWT token.POST /login
: Authenticate a user and generate a JWT token.GET /getById/:id
: Get a user by ID.GET /all-users
: Get all users.PUT /update-user/:id
: Update a user by ID.DELETE /soft/delete-user/:id
: Soft delete a user by ID.DELETE /hard/delete-user/:id
: Hard delete a user by ID.GET /swagger/*any
: Access the Swagger documentation UI.
All user passwords are securely hashed using bcrypt before being stored in the database. This ensures that even if the database is compromised, actual passwords remain secure.
The application uses JWT (JSON Web Tokens) for authentication. When a user registers or logs in, a JWT token is generated and returned, which should be included in subsequent requests for authentication.
The system enforces email uniqueness, preventing duplicate user registrations with the same email address.
When a new user is created using the POST /create-user
endpoint, a JWT token is generated and returned in the response. This token can be used for authentication in subsequent requests.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
- User Roles & Authentication: Expand the current JWT authentication with role-based access control.
- New Entities & Relationships: Introduce new models (e.g., comments, orders) and link them via foreign keys.
- Pagination & Search: Implement query parameters for advanced filtering and searching.
- Docker & Containerization: Containerize the app for easier deployment.
- Cloud Managed DB: Explore Neon, Aiven, or Supabase for PostgreSQL hosting.
- CI/CD: Set up automated tests and deployments (e.g., GitHub Actions).
To run a local Postgres container for development:
docker run --name go-crud-db -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
This starts a Postgres instance exposed at port 5432.
docker exec -it go-crud-db psql -U postgres
Use commands like \dt
to list tables.
Instead of running Postgres locally, you can connect to a managed database provider:
- Neon: https://neon.tech/
- Aiven: https://aiven.io/
- Supabase: https://supabase.com/
Update your .env
file’s DATABASE_URL
accordingly to point to your hosted PostgreSQL instance.