mirror of
https://github.com/DeveloLongScript/MHSF.git
synced 2026-05-07 13:54:59 -05:00
fix: i cant ditch inngest :(
This commit is contained in:
parent
435e80165d
commit
9aa2546e1b
@ -27,6 +27,7 @@ First, you must supply the following services with API keys:
|
||||
|
||||
- [Clerk](https://clerk.com): Create an app and put the respective keys in `.env.local`
|
||||
- MongoDB: Create a database, can be anywhere, and put the location to connect in `.env.local` for the key `MONGO_DB` (this isn't required by any means, but if you want to store any short term or historical data, use this.)
|
||||
- Inngest: Inngest is a smaller library, but runs the `cron` jobs which will make servers automaticly get added to the database.
|
||||
|
||||
_This project uses `yarn` as the main package manager. If `package-lock.json` is present, your pull request will get denied._
|
||||
Second, run `yarn` and `yarn build`. To start the app, run `yarn start`.
|
||||
|
||||
124
src/pages/api/inngest.ts
Normal file
124
src/pages/api/inngest.ts
Normal file
@ -0,0 +1,124 @@
|
||||
// inngest in mh-stats provides information periodicly. like every 30 minutes for historical data.
|
||||
// its fully automatic
|
||||
|
||||
import Favorites from "@/app/account/favorites/page";
|
||||
import { OnlineServer } from "@/lib/types/server";
|
||||
import { Inngest } from "inngest";
|
||||
import { serve } from "inngest/next";
|
||||
import { MongoClient } from "mongodb";
|
||||
import { Noto_Sans_Mahajani } from "next/font/google";
|
||||
|
||||
// Create a client to send and receive events
|
||||
export const inngest = new Inngest({ id: "my-app" });
|
||||
|
||||
// Create an API that serves zero functions
|
||||
export default serve({
|
||||
client: inngest,
|
||||
functions: [
|
||||
inngest.createFunction(
|
||||
{ id: "every-30-min" },
|
||||
[{ cron: "*/30 * * * *" }, { event: "test/30-min" }],
|
||||
async ({ event, step }) => {
|
||||
const mongo = new MongoClient(process.env.MONGO_DB as string);
|
||||
try {
|
||||
const mh = await (
|
||||
await fetch("https://api.minehut.com/servers", {
|
||||
headers: {
|
||||
accept: "*/*",
|
||||
"accept-language": Math.random().toString(),
|
||||
priority: "u=1, i",
|
||||
"sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": '"macOS"',
|
||||
"sec-fetch-dest": "empty",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-site": "cross-site",
|
||||
Referer: "http://localhost:3000/",
|
||||
"Referrer-Policy": "strict-origin-when-cross-origin",
|
||||
},
|
||||
body: null,
|
||||
method: "GET",
|
||||
})
|
||||
).json();
|
||||
|
||||
const mha = mongo.db("mhsf").collection("mh");
|
||||
const meta = mongo.db("mhsf").collection("meta");
|
||||
const dbl = mongo.db("mhsf").collection("history");
|
||||
mha.insertOne({
|
||||
total_players: mh.total_players,
|
||||
total_servers: mh.total_servers,
|
||||
unix: Date.now(),
|
||||
});
|
||||
mh.servers.forEach(async (server: OnlineServer, i: number) => {
|
||||
const favorites = (async () => {
|
||||
const result = await meta.find({ server: server.name }).toArray();
|
||||
if (result.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return result[0].favorites;
|
||||
})();
|
||||
const result = await favorites;
|
||||
|
||||
dbl.insertOne({
|
||||
player_count: server.playerData.playerCount,
|
||||
favorites: result,
|
||||
server: server.name,
|
||||
time: Date.now(),
|
||||
});
|
||||
|
||||
if (i == mh.servers.length) {
|
||||
mongo.close();
|
||||
|
||||
return {
|
||||
event,
|
||||
body: "Finished adding " + mh.servers.length + " servers.",
|
||||
};
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
mongo.close();
|
||||
return { event, body: "Cloudflare.. aborting " + e };
|
||||
}
|
||||
}
|
||||
),
|
||||
inngest.createFunction(
|
||||
{ id: "every-two-months" },
|
||||
[{ cron: "0 0 1 */2 *" }],
|
||||
async ({ event, step }) => {
|
||||
const mongo = new MongoClient(process.env.MONGO_DB as string);
|
||||
const meta = mongo.db("mhsf").collection("history");
|
||||
const historical = mongo.db("mhsf").collection("historical");
|
||||
|
||||
const array = await meta.find().toArray();
|
||||
const result: any = {};
|
||||
|
||||
array.forEach((c) => {
|
||||
if (result[c.server] == undefined) {
|
||||
result[c.server] = {
|
||||
server: c.server,
|
||||
player_count: [c.player_count],
|
||||
favorites: [c.favorites],
|
||||
time: Date.now(),
|
||||
};
|
||||
} else {
|
||||
result[c.server] = {
|
||||
server: c.server,
|
||||
player_count: [...result[c.server]?.player_count, c.player_count],
|
||||
favorites: [...result[c.server]?.favorites, c.favorites],
|
||||
time: c.time,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
historical.insertMany(Object.values(result));
|
||||
meta.drop();
|
||||
mongo.close();
|
||||
|
||||
return {
|
||||
event,
|
||||
body: "Dropped database. ",
|
||||
};
|
||||
}
|
||||
),
|
||||
],
|
||||
});
|
||||
@ -1,74 +0,0 @@
|
||||
import { OnlineServer } from "@/lib/types/server";
|
||||
import { MongoClient } from "mongodb";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
if (req.headers.Authorization !== `Bearer ${process.env.CRON_SECRET}`) {
|
||||
return res.status(401).end("Unauthorized");
|
||||
}
|
||||
|
||||
const mongo = new MongoClient(process.env.MONGO_DB as string);
|
||||
try {
|
||||
const mh = await (
|
||||
await fetch("https://api.minehut.com/servers", {
|
||||
headers: {
|
||||
accept: "*/*",
|
||||
"accept-language": Math.random().toString(),
|
||||
priority: "u=1, i",
|
||||
"sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": '"macOS"',
|
||||
"sec-fetch-dest": "empty",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-site": "cross-site",
|
||||
Referer: "http://localhost:3000/",
|
||||
"Referrer-Policy": "strict-origin-when-cross-origin",
|
||||
},
|
||||
body: null,
|
||||
method: "GET",
|
||||
})
|
||||
).json();
|
||||
|
||||
const mha = mongo.db("mhsf").collection("mh");
|
||||
const meta = mongo.db("mhsf").collection("meta");
|
||||
const dbl = mongo.db("mhsf").collection("history");
|
||||
mha.insertOne({
|
||||
total_players: mh.total_players,
|
||||
total_servers: mh.total_servers,
|
||||
unix: Date.now(),
|
||||
});
|
||||
mh.servers.forEach(async (server: OnlineServer, i: number) => {
|
||||
const favorites = (async () => {
|
||||
const result = await meta.find({ server: server.name }).toArray();
|
||||
if (result.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return result[0].favorites;
|
||||
})();
|
||||
const result = await favorites;
|
||||
|
||||
dbl.insertOne({
|
||||
player_count: server.playerData.playerCount,
|
||||
favorites: result,
|
||||
server: server.name,
|
||||
time: Date.now(),
|
||||
});
|
||||
|
||||
if (i == mh.servers.length) {
|
||||
mongo.close();
|
||||
|
||||
return res.send({
|
||||
body: "Finished adding " + mh.servers.length + " servers.",
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
mongo.close();
|
||||
return res.send({
|
||||
body: "Cloudflare interferred",
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,3 @@
|
||||
{
|
||||
"crons": [
|
||||
{
|
||||
"path": "/api/v1/cron/periodic-st",
|
||||
"schedule": "*/30 * * * *"
|
||||
},
|
||||
{ "path": "/api/v1/cron/data-cleanup", "schedule": "0 0 1 */2 *" }
|
||||
]
|
||||
"crons": [{ "path": "/api/v1/cron/data-cleanup", "schedule": "0 0 1 */2 *" }]
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user