Skip to content

abdullah-hamada7/go-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go CRUD API with JWT Authentication

A simple RESTful API built with Go, PostgreSQL, and GORM. It includes JWT-based authentication, database migrations with Goose, and containerization with Docker.

Features

  • 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.

Prerequisites

  • Go 1.24+
  • Docker and Docker Compose
  • PostgreSQL

Getting Started

  1. Clone the repository:

    git clone https://github.com/abdullah-hamada7/go-crud.git
    cd go-crud
  2. Set environment variables:

    Create a .env file or export the following:

    DATABASE_URL=postgres://admin:admin123@localhost:5432/users_db?sslmode=disable
    JWT_SECRET=your_secret_key
  3. Run with Docker Compose:

    docker compose up -d --build

API Endpoints

Public Routes

  • POST /register: Create a new account.
  • POST /login: Authenticate and receive a JWT.

Protected Routes (Requires Authorization: Bearer <token>)

  • 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.

Project Structure

  • 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.

Testing

Run tests using the Go toolchain:

go test ./...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published