Skip to content

Commit

Permalink
Fix the HTTP GET error, add database success record mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillweston committed Apr 17, 2024
1 parent 3b3e353 commit 350e3ce
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v2
with:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ This project implements a serverless function hosted within a Docker container t
## Running the Application
Run the Docker container with the necessary environment variables:
```bash
docker run -e DOCKER_ENV=true MONGODB_URI='your_mongo_uri' MONGODB_USERNAME='your_mongo_database_username' MONGODB_PASSWORD='your_mongo_database_username' MONGODB_PORT='your_mongo_database_port' MONGODB_DB='your_mongo_database_name' TWITTER_CONSUMER_KEY='your_key' -e TWITTER_CONSUMER_SECRET='your_secret' -p 5000:5000 twitter-auth
docker run -e DOCKER_ENV=true MONGODB_URI='your_mongo_uri' MONGODB_USERNAME='your_mongo_database_username' MONGODB_PASSWORD='your_mongo_database_username' MONGODB_PORT='your_mongo_database_port' MONGODB_DB='your_mongo_database_name' MONGODB_USERDB='your_mongo_database_name_for_user_data' TWITTER_CONSUMER_KEY='your_key' -e TWITTER_CONSUMER_SECRET='your_secret' -p 5000:5000 twitter-auth
```

## How to Acquire Twitter API Keys
Expand All @@ -115,6 +115,10 @@ docker run -e DOCKER_ENV=true MONGODB_URI='your_mongo_uri' MONGODB_USERNAME='you
5. Go to the 'Keys and Tokens' tab.
6. Copy the 'API Key' and 'API Secret Key' and use them as your `TWITTER_CONSUMER_KEY` and `TWITTER_CONSUMER_SECRET`.

## Twitter API Rate Limitations

Refer to the [Twitter API Rate Limiting](https://developer.twitter.com/en/docs/twitter-api/rate-limits) documentation for details on the rate limits for different endpoints.

### User Authentication Settings

1. Click the 'Edit' button in the 'User Authentication Settings' section.
Expand All @@ -137,6 +141,7 @@ The application has the following endpoints:
- `/check-follow`: Checks if a user is being followed by the authenticated user.
- `/check-bookmark`: Checks if a tweet has been bookmarked by the user.


## Milestones
- [x] Implement OAuth with Twitter.
- [x] Basic functionality to interact with Twitter API.
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
environment:
DOCKER_ENV: ${DOCKER_ENV:-true}
MONGODB_DB: ${MONGODB_DB:-twitterLogs}
MONGODB_USERDB: ${MONGODB_USERDB:-twitterUsers}
MONGODB_PORT: ${MONGODB_PORT:-27017}
MONGODB_USERNAME: ${MONGODB_USERNAME:-admin}
MONGODB_PASSWORD: ${MONGODB_PASSWORD}
Expand Down
17 changes: 12 additions & 5 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ if (!process.env.DOCKER_ENV) {
}

// Construct the MongoDB URI using environment variables
const uri = `mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}` +
`@localhost:${process.env.MONGODB_PORT}/${process.env.MONGODB_DB}?authSource=admin`;
const uri = `mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}`
+ `@localhost:${process.env.MONGODB_PORT}/?authSource=admin`;

const client = new MongoClient(uri);
let dbConnection;
let dbConnection = null;
let userDbConnection = null;

module.exports = {
connectToServer() {
return new Promise((resolve, reject) => {
client.connect()
.then(() => {
console.log("Connected successfully to MongoDB server");
dbConnection = client.db(process.env.MONGODB_DB); // This is how you select the database
resolve(dbConnection);
// Connect to the log database
dbConnection = client.db(process.env.MONGODB_DB);
// Connect to the user database
userDbConnection = client.db(process.env.MONGODB_USERDB);
resolve({ dbConnection, userDbConnection });
})
.catch((err) => {
console.error("Failed to connect to MongoDB server:", err);
Expand All @@ -29,6 +33,9 @@ module.exports = {
getDb() {
return dbConnection;
},
getUserDb() {
return userDbConnection;
},
closeConnection() {
console.log("Closing connection to MongoDB server");
client.close();
Expand Down
Loading

0 comments on commit 350e3ce

Please sign in to comment.