Skip to content

Simple API fetching data from external API, stores them in MongoDB and serves through HTTP server

Notifications You must be signed in to change notification settings

chrobson/catfacts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cat Facts API

This is a simple Cat Facts API written in Go. It fetches cat facts from an external API, stores them in a MongoDB database, and serves them through an HTTP server.

Features

  • Fetches cat facts from https://catfact.ninja/fact
  • Stores cat facts in a MongoDB database
  • Serves all stored cat facts through an HTTP endpoint

Installation

  1. Make sure you have Go installed on your system. You can download it from the official website: https://golang.org/dl/

  2. Install the required dependencies:

go get -u go.mongodb.org/mongo-driver/mongo
go get -u go.mongodb.org/mongo-driver/mongo/options
go get -u go.mongodb.org/mongo-driver/bson
  1. Clone this repository:
git clone https://github.com/chrobson/catfacts-api.git
  1. Change the current directory to the project directory:
cd catfacts-api
  1. Run the project:
npm start

Usage

  1. Ensure you have
  • MongoDB installed and running on your local machine (or docker image). If not, you can follow the instructions here to install it: https://docs.mongodb.com/manual/installation/
  • Same for React, First, make sure you have Node.js and npm installed on your machine. You can download Node.js from https://nodejs.org/.
    • Create a new React application using the following command: npx create-react-app catfacts-frontend
    • Change to the new directory cd catfacts-frontend
    • Install Axios to handle HTTP requests: npm install axios
  1. Start the Cat Facts API:
go run main.go
./catfacts-api
npm start
  1. The API will start fetching cat facts every 15 seconds, store them in the MongoDB database and display on React Frontend page. The HTTP API server will listen on port 8080, frontend will be on port 3000.

  2. To get all cat facts from the API, make a GET request to the /facts endpoint:

curl http://localhost:8080/facts
  1. The API will return a JSON array of cat facts.

API Endpoints

  • GET /facts: Retrieve all stored cat facts.

Configuration

You can customize the MongoDB connection string by editing the following line in the main function:

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))

Replace "mongodb://localhost:27017" with your desired MongoDB connection string.

TODO

  1. Improve build (to build npm into build and serve it with go)
  2. Improve frontend by adding features such as automatic refresh, pagination, or styling.

About

Simple API fetching data from external API, stores them in MongoDB and serves through HTTP server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published