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

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go CRUD Movies API Example

This is a simple and modern example of a CRUD (Create, Read, Update, Delete) API for managing movies, built using Go and GORM (Go Object Relational Mapper), along with added features such as JWT authentication, rate limiting, pagination, and Swagger documentation.

Table of Contents

Features

  • Create, Read, Update, and Delete (CRUD) operations for managing movies.
  • JWT-based Authentication for secured API access.
  • Rate Limiting to control API usage and prevent abuse.
  • Pagination, Filtering, and Sorting for movie listings.
  • Swagger/OpenAPI Documentation for easy API exploration.
  • Uses PostgreSQL as the database.
  • Environment variable configuration for database connection.
  • Simple JSON-based API for seamless integration.

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages