Skip to content

Commit

Permalink
Added Cafe API to Project
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Aug 13, 2024
1 parent 843d0ef commit d7107ed
Show file tree
Hide file tree
Showing 329 changed files with 7,477 additions and 134 deletions.
1 change: 1 addition & 0 deletions api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
51 changes: 51 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Environmente File
.env

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Gradle
/build/
/.gradle/
/out/
gradle.properties
/node_modules/

# IntelliJ IDEA
/.idea/
*.iml
*.ipr
*.iws

# Eclipse
.classpath
.project
.settings/
/bin/

# Logging Stuff
/logs/

# Mac Stuff
.DS_Store

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
6 changes: 6 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:16-slim
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm", "start"]
109 changes: 109 additions & 0 deletions api/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// - [ ] Changed Port
// - [ ] Changed MySQL Info and Port
// - [ ] Changed Token

// Load Our App Server Using Express
const express = require('express');
const app = express();
const morgan = require('morgan');
const body_parser = require('body-parser');
const checkType = require('./middleware/check-type');
const server_port = 5100;

// Body Parser - Extracts JSON Data
app.use(body_parser.urlencoded({extended: false}));
app.use(body_parser.json());

// Now serving everything in the public directory.
app.use(express.static('./public'));

// app.use(morgan('combined'));
app.use(morgan('short'));

// Base Directory
app.get("/", (request, response, next) => {
console.log("Responding to Route");
response.send("Hello from beanbeanjuice!");
});

// Preventing CORS Errors
app.use((request, response, next) => {
response.header('Access-Control-Allow-Origin', '*');
response.header('Access-Control-Allow-Headers',
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);

if (request.method === 'OPTIONS') {
response.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return response.status(200).json({});
}
next();
});

const cafe_api_url = "/cafe/api/v1"
const cafe_user_router = require('./routes/cafe/user.js');
app.use(cafe_api_url, cafe_user_router);

const cafe_bot_api_url = "/cafeBot/api/v1";
const beanCoins_router = require('./routes/cafeBot/beanCoins.js');
const birthdays_router = require('./routes/cafeBot/birthdays.js');
const cafe_router = require('./routes/cafeBot/cafe.js');
const cafeBot_router = require('./routes/cafeBot/cafeBot.js');
const codes_router = require('./routes/cafeBot/codes.js');
const counting_router = require('./routes/cafeBot/counting.js');
const guild_twitch_router = require('./routes/cafeBot/guild_twitch.js');
const guilds_router = require('./routes/cafeBot/guilds.js');
const interactions_router = require('./routes/cafeBot/interactions.js');
const minigames_router = require('./routes/cafeBot/minigames.js');
const polls_router = require('./routes/cafeBot/polls.js');
const raffles_router = require('./routes/cafeBot/raffles.js');
const voice_binds_router = require('./routes/cafeBot/voice_binds.js');
const welcomes_router = require('./routes/cafeBot/welcomes.js');
const goodbyes_router = require('./routes/cafeBot/goodbyes.js');
const words_router = require('./routes/cafeBot/words.js');
app.use(cafe_bot_api_url, beanCoins_router);
app.use(cafe_bot_api_url, birthdays_router);
app.use(cafe_bot_api_url, cafe_router);
app.use(cafe_bot_api_url, cafeBot_router);
app.use(cafe_bot_api_url, codes_router);
app.use(cafe_bot_api_url, counting_router);
app.use(cafe_bot_api_url, guild_twitch_router);
app.use(cafe_bot_api_url, guilds_router);
app.use(cafe_bot_api_url, interactions_router);
app.use(cafe_bot_api_url, minigames_router);
app.use(cafe_bot_api_url, polls_router);
app.use(cafe_bot_api_url, raffles_router);
app.use(cafe_bot_api_url, voice_binds_router);
app.use(cafe_bot_api_url, welcomes_router);
app.use(cafe_bot_api_url, goodbyes_router);
app.use(cafe_bot_api_url, words_router);

const interaction_pictures_router = require('./routes/cafeBot/interaction_pictures/interaction_pictures.js');
app.use(cafe_bot_api_url, interaction_pictures_router);

const boggle_api_url = "/boggle/api/v1"
const boggle_game_router = require('./routes/boggle/game.js');
const boggle_user_settings_router = require('./routes/boggle/settings.js');
app.use(boggle_api_url, boggle_game_router);
app.use(boggle_api_url, boggle_user_settings_router);

// Catches Errors that Have No Routes
app.use((request, response, next) => {
console.log("There has been an error. Route not found.");
const error = new Error("Route Not Found");
error.status = 404;
next(error);
});

// Catches ALL Errors
app.use((error, request, response, next) => {
response.status(error.status || 500);
response.json({
message: error.message
});
});

// Ping @ localhost:5101
app.listen(server_port, () => {
console.log("Server is up on " + checkType.RELEASE_TYPE + " and listening on port: " + server_port);
});
17 changes: 17 additions & 0 deletions api/middleware/check-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (request, response, next) => {
try {
user_type = request.user_data.user_type;

if (user_type === 'ADMIN') {
next();
} else {
response.status(401).json({
message: "Authentication Failed"
});
}
} catch (error) {
return response.status(401).json({
message: "Authentication Failed"
});
}
};
17 changes: 17 additions & 0 deletions api/middleware/check-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const jwt = require('jsonwebtoken');
const checkType = require('../middleware/check-type');

const TOKEN = checkType.TOKEN;

module.exports = (request, response, next) => {
try {
const token = request.headers.authorization;
const decoded_token = jwt.verify(token, TOKEN);
request.user_data = decoded_token;
next(); // Called if successfully authenticated.
} catch (error) {
return response.status(401).json({
message: "Authentication Failed"
});
}
};
24 changes: 24 additions & 0 deletions api/middleware/check-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('dotenv').config();

const RELEASE_TYPE = process.env.API_REQUEST_LOCATION;

if (RELEASE_TYPE === undefined) {
throw new Error('RELEASE_TYPE should be `BETA` or `RELEASE`.');
}

const TOKEN = process.env.JWT_TOKEN;

function getMySQLData() {
return {
"url": process.env.MYSQL_URL,
"port": process.env.MYSQL_PORT,
"user": process.env.MYSQL_USER,
"password": process.env.MYSQL_PASSWORD
}
}

module.exports = {
getMySQLData,
RELEASE_TYPE,
TOKEN
}
Loading

0 comments on commit d7107ed

Please sign in to comment.