MyApp is a RESTful API built with Go, Fiber, and PostgreSQL. It provides CRUD operations for users and products, with JWT-based authentication.
- User registration and login with JWT authentication
- CRUD operations for users and products
- Pagination support for listing users and products
- Password hashing for secure storage
- Go 1.16 or later
- PostgreSQL
- Git
-
Clone the repository:
git clone https://github.com/huuloc2026/crud-with-fiber cd myapp
-
Install dependencies:
go mod tidy
-
Configure the database:
Update the
config/config.yaml
file with your PostgreSQL credentials:database: host: localhost port: 5433 user: root password: huuloc2026 name: fiber-api sslmode: disable timezone: Asia/Jakarta jwt: secret: "super-secret-key-change-in-production" expires_in: 24h
-
Run the application:
go run main.go
The server will start on the configured port (default is 3000).
- POST /api/auth/register: Register a new user
- POST /api/auth/login: Login and receive a JWT token
- POST /api/users: Create a new user (protected)
- GET /api/users: Get a list of users (protected)
- GET /api/users/:id: Get a specific user by ID (protected)
- PUT /api/users/:id: Update a user by ID (protected)
- DELETE /api/users/:id: Delete a user by ID (protected)
- POST /api/products: Create a new product (protected)
- GET /api/products: Get a list of products (protected)
- GET /api/products/:id: Get a specific product by ID (protected)
- PUT /api/products/:id: Update a product by ID (protected)
- DELETE /api/products/:id: Delete a product by ID (protected)
-
Register a new user:
curl -X POST http://localhost:3000/api/auth/register \ -H "Content-Type: application/json" \ -d '{"name":"John Doe","email":"john@example.com","password":"secret123"}'
-
Login to get a JWT token:
curl -X POST http://localhost:3000/api/auth/login \ -H "Content-Type: application/json" \ -d '{"email":"john@example.com","password":"secret123"}'
-
Use the token for protected routes:
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" http://localhost:3000/api/users
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.