One Netlify function to handle individual tokens
This commit is contained in:
parent
f0742ee107
commit
2363d42c15
7 changed files with 105 additions and 143 deletions
|
@ -6,8 +6,8 @@ const handler: Handler = async () => {
|
|||
const octokit = new Octokit({auth: process.env.API_GITHUB});
|
||||
const github = await octokit.rest.activity.listEventsForAuthenticatedUser({username: "TTTaevas"});
|
||||
|
||||
const publicPush = github.data.find((e) => e.type === "PushEvent" && e.public);
|
||||
const privatePush = github.data.find((e) => e.type === "PushEvent" && !e.public);
|
||||
const publicPush = github.data.find((e) => (e.type === "PushEvent" || e.type === "PullRequestEvent") && e.public);
|
||||
const privatePush = github.data.find((e) => (e.type === "PushEvent" || e.type === "PullRequestEvent") && !e.public);
|
||||
|
||||
const info: GithubInfo = {
|
||||
public: publicPush ? {
|
||||
|
|
|
@ -2,7 +2,7 @@ import {type Handler} from "@netlify/functions";
|
|||
import * as osu from "osu-api-v2-js";
|
||||
import {type OsuInfo} from "#Infos/RhythmGames/Osu.js";
|
||||
import {MongoClient} from "mongodb";
|
||||
import {type Token} from "./osu_token.js";
|
||||
import {type Token} from "./token.js";
|
||||
|
||||
const handler: Handler = async (req) => {
|
||||
const client = new MongoClient(process.env.URL_MONGODB!);
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
/* eslint no-async-promise-executor: 0 */ // Doing promises is needed in order to make multiple requests at once, lowering wait time
|
||||
|
||||
import {type Handler} from "@netlify/functions";
|
||||
import {API} from "osu-api-v2-js";
|
||||
import {MongoClient} from "mongodb";
|
||||
|
||||
export interface Token {
|
||||
access_token: API["access_token"];
|
||||
expires: API["expires"];
|
||||
}
|
||||
|
||||
const handler: Handler = async () => {
|
||||
const client = new MongoClient(process.env.URL_MONGODB!);
|
||||
await client.connect();
|
||||
|
||||
const db = client.db("tokens");
|
||||
const collection = db.collection<Token>("osu");
|
||||
const tokens = await collection.find().toArray();
|
||||
|
||||
const now = new Date();
|
||||
const token = tokens.find((t) => t.expires > now);
|
||||
const expiredTokens = tokens.filter((t) => now > t.expires);
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
if (!token) {
|
||||
promises.push(new Promise(async (resolve) => {
|
||||
console.log("Setting a new token for osu!...");
|
||||
const api = await API.createAsync(11451, process.env.API_OSU!);
|
||||
const insertion = await collection.insertOne({
|
||||
access_token: api.access_token,
|
||||
expires: api.expires,
|
||||
});
|
||||
console.log(`New osu! token in the database: ${insertion.insertedId.toString()}`);
|
||||
resolve();
|
||||
}));
|
||||
}
|
||||
|
||||
if (expiredTokens.length) {
|
||||
promises.push(new Promise(async (resolve) => {
|
||||
console.log("Deleting old tokens for osu!...");
|
||||
await Promise.all(expiredTokens.map(async (t) => {
|
||||
return new Promise<void>(async (resolve) => {
|
||||
const deletion = await collection.deleteOne({_id: t._id});
|
||||
if (deletion.deletedCount) {
|
||||
console.log(`Old osu! token deleted from the database: ${t._id.toString()}`);
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
resolve();
|
||||
}));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
void client.close();
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
};
|
||||
};
|
||||
|
||||
export {handler};
|
100
netlify/functions/token.ts
Normal file
100
netlify/functions/token.ts
Normal file
|
@ -0,0 +1,100 @@
|
|||
/* eslint no-async-promise-executor: 0 */ // Doing promises is needed in order to make multiple requests at once, lowering wait time
|
||||
|
||||
import {type Handler} from "@netlify/functions";
|
||||
import {InsertOneResult, MongoClient} from "mongodb";
|
||||
|
||||
import {API} from "osu-api-v2-js";
|
||||
|
||||
export interface Token {
|
||||
access_token: string;
|
||||
expires: Date;
|
||||
}
|
||||
|
||||
const handler: Handler = async (req) => {
|
||||
const service = req.queryStringParameters?.service;
|
||||
if (!service) {return {statusCode: 400};}
|
||||
|
||||
const client = new MongoClient(process.env.URL_MONGODB!);
|
||||
await client.connect();
|
||||
|
||||
const db = client.db("tokens");
|
||||
const collection = db.collection<Token>(service);
|
||||
const tokens = await collection.find().toArray();
|
||||
|
||||
const now = new Date();
|
||||
const token = tokens.find((t) => t.expires > now);
|
||||
const expiredTokens = tokens.filter((t) => now > t.expires);
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
if (!token) {
|
||||
const collections = await db.listCollections().toArray();
|
||||
if (!collections.find((c) => c.name === service)) {client.close(); return {statusCode: 400};}
|
||||
|
||||
promises.push(new Promise(async (resolve, reject) => {
|
||||
console.log(`Setting a new token for ${service}...`);
|
||||
let insertion: InsertOneResult;
|
||||
|
||||
if (service === "osu") {
|
||||
const api = await API.createAsync(11451, process.env.API_OSU!);
|
||||
insertion = await collection.insertOne({
|
||||
access_token: api.access_token,
|
||||
expires: api.expires,
|
||||
});
|
||||
}
|
||||
|
||||
else if (service === "umami") {
|
||||
const response = await fetch("https://visitors.taevas.xyz/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
},
|
||||
body: `username=${process.env.USERNAME_UMAMI}&password=${process.env.PASSWORD_UMAMI}`
|
||||
});
|
||||
const json: {token: string} = await response.json();
|
||||
|
||||
// Assume it expires in one day
|
||||
const date = new Date();
|
||||
date.setHours(date.getHours() + 24);
|
||||
insertion = await collection.insertOne({
|
||||
access_token: json.token,
|
||||
expires: date,
|
||||
});
|
||||
}
|
||||
|
||||
else {
|
||||
console.error(`Service "${service}" doesn't exist! Unable to set a new token...`);
|
||||
return reject();
|
||||
}
|
||||
|
||||
console.log(`New ${service} token in the database: ${insertion.insertedId.toString()}`);
|
||||
resolve();
|
||||
}));
|
||||
}
|
||||
|
||||
if (expiredTokens.length) {
|
||||
promises.push(new Promise(async (resolve) => {
|
||||
console.log(`Deleting old tokens for ${service}...`);
|
||||
await Promise.all(expiredTokens.map(async (t) => {
|
||||
return new Promise<void>(async (resolve) => {
|
||||
const deletion = await collection.deleteOne({_id: t._id});
|
||||
if (deletion.deletedCount) {
|
||||
console.log(`Old ${service} token deleted from the database: ${t._id.toString()}`);
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
resolve();
|
||||
}));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
void client.close();
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
};
|
||||
};
|
||||
|
||||
export {handler};
|
|
@ -1,74 +0,0 @@
|
|||
/* eslint no-async-promise-executor: 0 */ // Doing promises is needed in order to make multiple requests at once, lowering wait time
|
||||
|
||||
import {type Handler} from "@netlify/functions";
|
||||
import {MongoClient} from "mongodb";
|
||||
|
||||
export interface Token {
|
||||
access_token: string;
|
||||
expires: Date;
|
||||
}
|
||||
|
||||
const handler: Handler = async () => {
|
||||
const client = new MongoClient(process.env.URL_MONGODB!);
|
||||
await client.connect();
|
||||
|
||||
const db = client.db("tokens");
|
||||
const collection = db.collection<Token>("umami");
|
||||
const tokens = await collection.find().toArray();
|
||||
|
||||
const now = new Date();
|
||||
const token = tokens.find((t) => t.expires > now);
|
||||
const expiredTokens = tokens.filter((t) => now > t.expires);
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
if (!token) {
|
||||
promises.push(new Promise(async (resolve) => {
|
||||
console.log("Setting a new token for umami...");
|
||||
const response = await fetch("https://visitors.taevas.xyz/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
},
|
||||
body: `username=${process.env.USERNAME_UMAMI}&password=${process.env.PASSWORD_UMAMI}`
|
||||
});
|
||||
const json: {token: string} = await response.json();
|
||||
|
||||
// Assume it expires in one day
|
||||
const date = new Date();
|
||||
date.setHours(date.getHours() + 24);
|
||||
const insertion = await collection.insertOne({
|
||||
access_token: json.token,
|
||||
expires: date,
|
||||
});
|
||||
console.log(`New umami token in the database: ${insertion.insertedId.toString()}`);
|
||||
resolve();
|
||||
}));
|
||||
}
|
||||
|
||||
if (expiredTokens.length) {
|
||||
promises.push(new Promise(async (resolve) => {
|
||||
console.log("Deleting old tokens for umami...");
|
||||
await Promise.all(expiredTokens.map(async (t) => {
|
||||
return new Promise<void>(async (resolve) => {
|
||||
const deletion = await collection.deleteOne({_id: t._id});
|
||||
if (deletion.deletedCount) {
|
||||
console.log(`Old umami token deleted from the database: ${t._id.toString()}`);
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
resolve();
|
||||
}));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
void client.close();
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
};
|
||||
};
|
||||
|
||||
export {handler};
|
|
@ -10,7 +10,7 @@ export default function RhythmGames() {
|
|||
const [error, setError] = useState(false);
|
||||
|
||||
const getToken = async () => {
|
||||
await fetch("/.netlify/functions/osu_token").then((r) => {
|
||||
await fetch("/.netlify/functions/token?service=osu").then((r) => {
|
||||
if (r.ok) {
|
||||
setToken(true);
|
||||
} else {
|
||||
|
|
|
@ -8,7 +8,7 @@ export default function Website() {
|
|||
const [error, setError] = useState(false);
|
||||
|
||||
const getToken = async () => {
|
||||
await fetch("/.netlify/functions/umami_token").then((r) => {
|
||||
await fetch("/.netlify/functions/token?service=umami").then((r) => {
|
||||
if (r.ok) {
|
||||
setToken(true);
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue