A simple RESTful API built with Go, PostgreSQL, and GORM. It includes JWT-based authentication, database migrations with Goose, and containerization with Docker.
- CRUD Operations: Manage users with basic Create, Read, Update, and Delete actions.
- JWT Authentication: Protected routes using JSON Web Tokens.
- Database Migrations: Automatic schema updates using Goose.
- Containerized: Ready to run with Docker and Docker Compose.
- Validation: Input validation for user data.
- Go 1.24+
- Docker and Docker Compose
- PostgreSQL
-
Clone the repository:
git clone https://github.com/abdullah-hamada7/go-crud.git cd go-crud -
Set environment variables:
Create a
.envfile or export the following:DATABASE_URL=postgres://admin:admin123@localhost:5432/users_db?sslmode=disable JWT_SECRET=your_secret_key -
Run with Docker Compose:
docker compose up -d --build
POST /register: Create a new account.POST /login: Authenticate and receive a JWT.
GET /api/users: List all users.GET /api/users/{id}: Get a specific user.POST /api/users: Create a new user.PUT /api/users/{id}: Update an existing user.DELETE /api/users/{id}: Soft delete a user.POST /api/users/{id}/restore: Restore a soft-deleted user.
cmd/server/main.go: Application entry point and router setup.pkg/handlers/: HTTP request handlers (Auth, Users).pkg/models/: Database models (GORM).pkg/db/: Database connection and migration logic.pkg/db/migrations/: SQL migration files.
Run tests using the Go toolchain:
go test ./...