Skip to content

This is a simple and modern example of a CRUD API for managing movies, built using Go.

Notifications You must be signed in to change notification settings

JawherKl/go-crud-movie-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎬 Go CRUD Movies API Example

A modern and robust CRUD (Create, Read, Update, Delete) API for managing movies, built with Go and GORM, featuring JWT authentication, rate limiting, and more.

📋 Table of Contents

  • Features
  • Getting Started
  • Prerequisites
  • Installation
  • Running the Application
  • API Endpoints
  • Environment Variables
  • Contributing
  • License

✨ Features

  • CRUD Operations: Manage movies with Create, Read, Update, and Delete operations.
  • JWT Authentication: Secure API access with JSON Web Tokens.
  • Rate Limiting: Control API usage and prevent abuse.
  • Pagination, Filtering, and Sorting: Efficient movie listings.
  • Swagger/OpenAPI Documentation: Explore the API effortlessly.
  • PostgreSQL: Robust database support.
  • Environment Variable Configuration: Easy database connection setup.
  • JSON-based API: Seamless integration with other services.

🚀 Getting Started

Follow these instructions to set up and run the project on your local machine.

🛠 Prerequisites

📥 Installation

  1. Clone the repository:

    git clone https://github.com/your-username/go-crud-movies-api.git
    cd go-crud-movies-api
  2. Install dependencies:

    go mod tidy
  3. Set up the database: Create a PostgreSQL database and configure the environment variables as described below.

  4. Set up environment variables: Create a .env file in the project root with the following content:

    DB_HOST=localhost
    DB_PORT=5432
    DB_USER=your_db_user
    DB_NAME=your_db_name
    DB_PASSWORD=your_db_password
    JWT_SECRET_KEY=your_secret_key

🏃 Running the Application

  1. Run the project:

    go run main.go
  2. The API will be running at:

    • http://localhost:8080

🔗 API Endpoints

🔑 Authentication

  • Login (Generate JWT Token):
    POST /auth/login
    Request body:
    {
      "username": "user",
      "password": "password"
    }
    Response:
    {
      "token": "your_jwt_token"
    }

🎥 Movie Operations

  • Create a movie:

    POST /movies

    Request body:

    {
      "name": "Movie Name",
      "description": "Movie Description"
    }

    Response:

    {
      "id": 1,
      "name": "Movie Name",
      "description": "Movie Description"
    }
  • Get all movies with pagination and sorting:

    GET /movies?page=1&limit=10&sort=desc

    Response:

    [
      {
        "id": 1,
        "name": "Movie Name",
        "description": "Movie Description"
      }
    ]
  • Get a single movie by ID:

    GET /movies/{id}

    Response:

    {
      "id": 1,
      "name": "Movie Name",
      "description": "Movie Description"
    }
  • Update a movie by ID:

    PUT /movies/{id}

    Request body:

    {
      "name": "Updated Movie Name",
      "description": "Updated Movie Description"
    }

    Response:

    {
      "id": 1,
      "name": "Updated Movie Name",
      "description": "Updated Movie Description"
    }
  • Delete a movie by ID:

    DELETE /movies/{id}

    Response:

    {
      "message": "Movie deleted successfully"
    }

⚙️ Environment Variables

The application uses the following environment variables for configuration:

  • DB_HOST: The database host (default: localhost)
  • DB_PORT: The database port (default: 5432)
  • DB_USER: The database user
  • DB_NAME: The database name
  • DB_PASSWORD: The database password
  • JWT_SECRET_KEY: The secret key for JWT token generation

🤝 Contributing

Contributions are welcome! Feel free to fork the repository, make changes, and submit pull requests.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

⭐ Stargazers over time

Stargazers over time

About

This is a simple and modern example of a CRUD API for managing movies, built using Go.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages