This project is a simple web service built with Go. It fetches programming jokes and memes from external APIs and stores jokes in a PostgreSQL database. You can access these jokes and memes through HTTP endpoints.
- Fetch a Programming Joke: The application fetches a random programming joke from the JokeAPI and saves it in a PostgreSQL database.
- Fetch a Programming Meme: The application fetches a random programming meme from the Meme API and displays it as an HTML page.
Before running the application, make sure you have Docker installed on your machine. Docker will help you run the application and its dependencies easily in containers.
Follow these steps to get the project running on your local machine using Docker.
-
Clone the Repository: First, clone the repository to your local machine:
git clone https://github.com/Achanandhi-M/golang-api-project.git cd golang-api-project -
Build and Run the Application Using Docker Compose: Docker Compose is used to set up both the Go application and PostgreSQL database. To build and start the application, run:
docker-compose up
This command will download the required images, build the Go application, and start the containers for both the Go app and PostgreSQL database.
-
Access the Application: Once the containers are running, you can access the application in your browser:
- The Go application will be available at: http://localhost:8080
- PostgreSQL will be available at:
localhost:5432(for accessing the database).
Once the application is running, you can interact with it using the following HTTP endpoints:
-
GET /joke:- Fetches a random programming joke from JokeAPI.
- Saves the joke in the PostgreSQL database.
- Returns a JSON response with the joke data.
-
GET /meme:- Fetches a random programming meme from Meme API.
- Displays the meme in an HTML page with a link to the original post on Reddit.
To interact with the PostgreSQL database, you can run commands inside the database container. Here’s how you can do that:
-
Open a terminal and run the following command to access the PostgreSQL container:
docker-compose exec -it <container-name> psql -U postgres -d jokes_db
-
Inside the database, you can run the following commands:
-
List tables:
\dt -
See the first 10 jokes:
SELECT * FROM jokes LIMIT 10;
-
Exit:
\q
-
This project is a beginner-friendly API service that demonstrates how to fetch and display programming jokes and memes using Go, external APIs, and a PostgreSQL database. Docker Compose simplifies the process of running both the application and the database in isolated containers.
By following the instructions above, you can quickly get the service up and running on your local machine.