Skip to content

Commit

Permalink
feat: Add DEMO env & CI/CD Pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
riimuru committed Oct 18, 2023
1 parent cbe5fd1 commit 6c75cb0
Show file tree
Hide file tree
Showing 15 changed files with 534 additions and 239 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ REDIS_PORT=Redis port (optional)
REDIS_PASSWORD=Redis password (optional)
NINE_ANIME_HELPER_URL=9anime (optional)
TMDB_KEY=TMDB access token auth (optional)
NODE_ENV=Environment (optional) (DEMO or PROD)
2 changes: 2 additions & 0 deletions .github/scripts/install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "Code BeforeInstall event script ran."
2 changes: 2 additions & 0 deletions .github/scripts/start-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "Code ApplicationStart event script ran."
2 changes: 2 additions & 0 deletions .github/scripts/validate-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "Code ValidateService event script ran."
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ src/scripts
.vscode
yarn.lock
test.ts
dist
dist
package-lock.json
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "12.5.0"
- 13
- 14
- 15
- 16
- 17
script:
- npm run lint
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ RUN mkdir -p /home/nodejs/app/node_modules && chown -R nodejs:nodejs /home/nodej
WORKDIR /home/nodejs/app

# set default node env
ARG NODE_ENV=development
ARG NODE_ENV=DEMO
ARG PORT=3000

# ARG NODE_ENV=production
# to be able to run tests (for example in CI), do not set production as environment
ENV NODE_ENV=${NODE_ENV}
ENV PORT=${PORT}
ENV REDIS_HOST=${REDIS_HOST}
ENV REDIS_PORT=${REDIS_PORT}
ENV REDIS_PASSWORD=${REDIS_PASSWORD}

ENV NPM_CONFIG_LOGLEVEL=warn

Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: yarn build && node dist/main.js
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Consumet scrapes data from various websites and provides APIs for accessing the
- [Showcases](#showcases)
- [Provider Request](#provider-request)
- [Support](#support)
- [Contributors ✨](#contributors-)
- [Related repositories](#related-repositories)

## Installation
Expand Down Expand Up @@ -110,8 +111,14 @@ Make a new [issue](https://github.com/consumet/consumet.ts/issues/new?assignees=
You can contact the maintainers of consumet.ts via [email](mailto:[email protected]), or [join the discord server](https://discord.gg/qTPfvMxzNH) (Recommended).

<a href="https://discord.gg/qTPfvMxzNH">
<img src="https://discordapp.com/api/guilds/987492554486452315/widget.png?style=banner2">
</p>
<img src="https://discordapp.com/api/guilds/987492554486452315/widget.png?style=banner2"/>
</a>


## Contributors ✨
Thanks to the following people for keeping this project alive and thriving.

[![](https://contrib.rocks/image?repo=consumet/consumet.ts)](https://github.com/consumet/consumet.ts/graphs/contributors)

## Related repositories
- [Consumet.ts](https://github.com/consumet/consumet.ts)
Expand Down
19 changes: 19 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 0.0
os: linux
files:
- source: /
destination: /api-consumet-org
hooks:
BeforeInstall:
- location: .github/scripts/install_dependencies.sh
timeout: 300
runas: root

ApplicationStart:
- location: .github/scripts/start_server.sh
timeout: 300
runas: root

ValidateService:
- location: .github/scripts/validate_service.sh
timeout: 300
38 changes: 38 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: 0.2

env:
parameter-store:
REDIS_HOST: /redis/host
REDIS_PORT: /redis/port
REDIS_PASSWORD: /redis/password
PORT: /port
NODE_ENV: /node/env

phases:
install:
runtime-versions:
nodejs: 18
commands:
- echo "📦 installing packages..."
- echo "✅ Packages installed successfully."
pre_build:
commands:
- echo "⚙️ Testing..."
- echo "✅ Tests passed successfully."
- echo $PASSWORD

build:
commands:
- echo "🚧 Starting compiling packages..."
- echo "✅ Build passed successfully."
post_build:
commands:
- echo "🚚 Performing post-build packing and operations..."
- aws deploy push --application-name deploy.api.consumet.org --s3-location s3://deploy--deployment-revisions/development/source.zip --ignore-hidden-files
- aws deploy create-deployment --application-name deploy.api.consumet.org --s3-location bucket=deploy-deployment-revisions,key=development/source.zip,bundleType=zip,eTag=80e501480a8545019660e87ca42a6f00,version=67ZE9Q8CZeo9XszaZ4F.eKmrlW7mnDrm --deployment-group-name DGN --deployment-config-name CodeDeployDefault.AllAtOnce --description "This deployment aims to deploy our code to the eligible EC2 instance(s)."
- echo "✅ Post build successful"

artifacts:
files:
- '**/*'
name: deploy-build-artifacts
91 changes: 91 additions & 0 deletions demo/apidemo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo of Consumet API</title>
<meta name="viewport" content="width=device-width">
<style>
html, body {
margin: 0;
height: 100%;
padding: 3px;
font-family: Arial, sans-serif;
font-size: 16px;
}
* {
-moz-box-sizing: border-box;
box-sizing: border-box;
}
label { display: block; }
input {
display: block;
width: 100%;
padding: 8px 5px;
border: 1px solid #CCC;
}
button {
display: inline-block;
width: 49%;
padding: 8px;
}
textarea {
width: 100%;
height: 100%;
}
#top {
height: 180px;
position: relative;

}
#bottom {
height: 100%;
margin-top: -180px;
padding-top: 180px;
}

#get {
position: absolute;
bottom: 0;
right: 0;
border-radius: 2px;
border: 1px solid #CCC;
background: #fd6868;
}
</style>
</head>
<body>
<div id="top">
This is a demo of <a href="https://github.com/consumet/api.consumet.org">Consumet API</a>. It should be <b>only</b> used for development purposes.
To temporarily unlock access to the API, click the button below and follow the instructions.
<button id="get" >Request temporary access</button>
</div>

<script>
const api_url = 'https://api.consumet.org/';
function requestAccess(options, printResult) {
const x = new XMLHttpRequest();
x.open(options.method, options.url);
x.onload = x.onerror = () => {
if (x.status === 200) window.location.href = "/";
else printResult(x.responseText);

};
x.send(options.data);
}

// Bind event
(function() {
var button = document.getElementById('get');
button.addEventListener('click', () => {
requestAccess({
method: 'POST',
url: api_url + 'apidemo',
}, (result) => {
alert(result);
});
}, false);

})();
</script>
</body>
</html>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"start": "ts-node src/main.ts",
"dev": "nodemon src/main.ts",
"build": "tsc",
"lint": "prettier --write ."
},
"engines": {
Expand Down
117 changes: 108 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import anime from './routes/anime';
import manga from './routes/manga';
import comics from './routes/comics';
import lightnovels from './routes/light-novels';
import fs from 'fs';
import movies from './routes/movies';
import meta from './routes/meta';
import news from './routes/news';
Expand All @@ -25,14 +26,6 @@ export const tmdbApi = process.env.TMDB_KEY && process.env.TMDB_KEY;
(async () => {
const PORT = Number(process.env.PORT) || 3000;

console.log(chalk.green(`Starting server on port ${PORT}... 🚀`));
if (!process.env.REDIS_HOST)
console.warn(chalk.yellowBright('Redis not found. Cache disabled.'));
if (!process.env.TMDB_KEY)
console.warn(
chalk.yellowBright('TMDB api key not found. the TMDB meta route may not work.')
);

const fastify = Fastify({
maxParamLength: 1000,
logger: true,
Expand All @@ -42,6 +35,106 @@ export const tmdbApi = process.env.TMDB_KEY && process.env.TMDB_KEY;
methods: 'GET',
});

if (process.env.NODE_ENV === 'DEMO') {
console.log(chalk.yellowBright('DEMO MODE ENABLED'));

const map = new Map<string, { expiresIn: Date }>();
// session duration in milliseconds (5 hours)
const sessionDuration = 1000 * 60 * 60 * 5;

fastify.addHook('onRequest', async (request, reply) => {
const ip = request.ip;
const session = map.get(ip);

// check if the requester ip has a session (temporary access)
if (session) {
// if session is found, check if the session is expired
const { expiresIn } = session;
const currentTime = new Date();
const sessionTime = new Date(expiresIn);

// check if the session has been expired
if (currentTime.getTime() > sessionTime.getTime()) {
console.log('session expired');
// if expired, delete the session and continue
map.delete(ip);

// redirect to the demo request page
return reply.redirect('/apidemo');
}
console.log('session found. expires in', expiresIn);
if (request.url === '/apidemo') return reply.redirect('/');
return;
}

// if route is not /apidemo, redirect to the demo request page
if (request.url === '/apidemo') return;

console.log('session not found');
reply.redirect('/apidemo');
});

fastify.post('/apidemo', async (request, reply) => {
const { ip } = request;

// check if the requester ip has a session (temporary access)
const session = map.get(ip);

if (session) return reply.redirect('/');

// if no session, create a new session
const expiresIn = new Date(Date.now() + sessionDuration);
map.set(ip, { expiresIn });

// redirect to the demo request page
reply.redirect('/');
});

fastify.get('/apidemo', async (_, reply) => {
try {
const stream = fs.readFileSync(__dirname + '/../demo/apidemo.html');
return reply.type('text/html').send(stream);
} catch (err) {
console.error(err);
return reply.status(500).send({
message: 'Could not load the demo page. Please try again later.',
});
}
});

// set interval to delete expired sessions every 1 hour
setInterval(() => {
const currentTime = new Date();
for (const [ip, session] of map.entries()) {
const { expiresIn } = session;
const sessionTime = new Date(expiresIn);

// check if the session is expired
if (currentTime.getTime() > sessionTime.getTime()) {
console.log('session expired for', ip);
// if expired, delete the session and continue
map.delete(ip);
}
}
}, 1000 * 60 * 60);
}

console.log(chalk.green(`Starting server on port ${PORT}... 🚀`));
if (!process.env.REDIS_HOST)
console.warn(chalk.yellowBright('Redis not found. Cache disabled.'));
if (!process.env.TMDB_KEY)
console.warn(
chalk.yellowBright('TMDB api key not found. the TMDB meta route may not work.')
);

console.log(chalk.green(`Starting server on port ${PORT}... 🚀`));
if (!process.env.REDIS_HOST)
console.warn(chalk.yellowBright('Redis not found. Cache disabled.'));
if (!process.env.TMDB_KEY)
console.warn(
chalk.yellowBright('TMDB api key not found. the TMDB meta route may not work.')
);

await fastify.register(books, { prefix: '/books' });
await fastify.register(anime, { prefix: '/anime' });
await fastify.register(manga, { prefix: '/manga' });
Expand All @@ -55,7 +148,13 @@ export const tmdbApi = process.env.TMDB_KEY && process.env.TMDB_KEY;

try {
fastify.get('/', (_, rp) => {
rp.status(200).send('Welcome to consumet api! 🎉');
rp.status(200).send(
`Welcome to consumet api! 🎉 \n${
process.env.NODE_ENV === 'DEMO'
? 'This is a demo of the api. You should only use this for testing purposes.'
: ''
}`
);
});
fastify.get('*', (request, reply) => {
reply.status(404).send({
Expand Down
Loading

0 comments on commit 6c75cb0

Please sign in to comment.