This is a sample RESTful API project built with Go (httprouter). It demonstrates how to build a modern backend application that includes a PostgreSQL database, with everything containerized and managed by Docker Compose. The API provides standard CRUD operations for managing a collection of movies.
- HTTP Router: Uses
github.com/julienschmidt/httprouterfor high-performance routing. - CRUD Operations: Create, Read, Update, and Delete movies.
- Filtering: Filter movies by title and genres.
- Pagination: Paginate through the list of movies.
- Sorting: Sort movies by ID, title, year, or runtime.
- Health Check: An endpoint to check the status of the API.
- Middleware: Includes IP based rate limiting and panic recovery.
- Go (version 1.24 or newer)
- PostgreSQL
- Docker (for running PostgreSQL in a container)
- make
git clone https://github.com/0xemrekaya/moovie_project.git
cd moovie_projectThere is a .env file in the root of the project and add the following environment variables. These will be used by docker-compose.
The easiest way to get the application running is by using Docker. The provided docker-compose.yml file will set up both the API service and the PostgreSQL database.
Run the following command to build and start the containers:
make docker-upThis will start the API server on http://localhost:8000.
To stop the application and the database container, you can run:
make docker-downDatabase migrations are handled automatically by the application on startup. The migration files are located in the /migrations directory.
make build: Builds the application binary.make run: Runs the application locally (without Docker).make test: Runs the tests.make clean: Removes the build artifacts.make docker-up: Starts the PostgreSQL container.make docker-down: Stops the PostgreSQL container.
All endpoints are prefixed with /v1.
| Method | Endpoint | Description |
|---|---|---|
GET |
/healthcheck |
Checks the health of the application. |
GET |
/movies |
Returns a list of movies (with filtering/sorting). |
POST |
/movies |
Creates a new movie. |
GET |
/movies/:id |
Retrieves a specific movie by its ID. |
PATCH |
/movies/:id |
Updates a specific movie. |
DELETE |
/movies/:id |
Deletes a specific movie. |
You can query the /v1/movies endpoint with the following parameters:
title: Filter by movie title (e.g.,?title=Inception).genres: Filter by genres, comma-separated (e.g.,?genres=action,sci-fi).page: The page number for pagination (e.g.,?page=2).page_size: The number of results per page (e.g.,?page_size=10).sort: Sort order. Use a field name (e.g.,?sort=year). Prepend with-for descending order (e.g.,?sort=-year).- Allowed sort fields:
id,title,year,runtime.
- Allowed sort fields:
Create a movie:
curl -X POST -H "Content-Type: application/json" -d '{
"title": "Inception",
"year": 2010,
"runtime": 148,
"genres": ["action", "sci-fi", "thriller"]
}' http://localhost:8000/v1/moviesGet a list of movies:
curl http://localhost:8000/v1/moviesGet a list of movies with filtering and sorting:
curl "http://localhost:8000/v1/movies?genres=sci-fi,action&sort=-year"Get a specific movie:
curl http://localhost:8000/v1/movies/1