Separate API, proc
This commit is contained in:
parent
c1f1742948
commit
6a3f531527
27 changed files with 334 additions and 104 deletions
3
backend/.envrc
Normal file
3
backend/.envrc
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export SECRET=krRkGF2rthrT3DScCwXY4Jc_z9ldSQFw32OJt1iKFh0=
|
||||
export MONGO_URI=localhost
|
||||
export MONGO_PORT=27017
|
||||
29
backend/.gitignore
vendored
Normal file
29
backend/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
/dist
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
yarn.lock
|
||||
|
||||
.vscode/
|
||||
.vscode/*
|
||||
27
backend/Dockerfile
Normal file
27
backend/Dockerfile
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
FROM node:18 AS builder
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
COPY package*.json ./
|
||||
RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npm run ts:build
|
||||
|
||||
FROM node:18-alpine AS server
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
RUN chown -R node:node /usr/src/app
|
||||
|
||||
USER node
|
||||
EXPOSE 5000
|
||||
|
||||
COPY package*.json ./
|
||||
RUN npm install --production
|
||||
|
||||
COPY --from=0 /usr/src/app/dist dist
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
CMD ["node", "dist/server.js"]
|
||||
28
backend/package.json
Normal file
28
backend/package.json
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"name": "ctmmm-api",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.20.0",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.1",
|
||||
"fernet-nodejs": "^1.0.4",
|
||||
"moment": "^2.29.3",
|
||||
"mongoose": "^7.0.0",
|
||||
"shortid": "^2.2.16"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.19.2",
|
||||
"@types/cors": "^2.8.12",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/shortid": "^0.0.29",
|
||||
"typescript": "^4.7.4"
|
||||
},
|
||||
"scripts": {
|
||||
"start:live": "yarn run ts:build; concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"yarn run ts:watch\" \"yarn run start:serve\"",
|
||||
"start:serve": "nodemon --inspect dist/server.js",
|
||||
"ts:build": "tsc",
|
||||
"ts:watch": "tsc -w"
|
||||
}
|
||||
}
|
||||
16
backend/src/app.ts
Normal file
16
backend/src/app.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { json, urlencoded } from "body-parser";
|
||||
import cors from "cors";
|
||||
import express from "express";
|
||||
import GetJob from "./app/controllers/job/get";
|
||||
import PutJob from "./app/controllers/job/put";
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(json({ limit: "50mb" }));
|
||||
app.use(urlencoded({ extended: true }));
|
||||
app.use(cors());
|
||||
|
||||
app.post("/api/v1/jobs/:jobId", GetJob);
|
||||
app.put("/api/v1/jobs", PutJob);
|
||||
|
||||
export default app;
|
||||
38
backend/src/app/controllers/job/get.ts
Normal file
38
backend/src/app/controllers/job/get.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { Request, Response } from "express";
|
||||
import Jobs from "../../models/Job";
|
||||
import Status from "../../status";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.params.jobId) {
|
||||
return res.status(403).send("URL Param for Job ID is required");
|
||||
}
|
||||
|
||||
const job = await Jobs.findOne({ job_id: req.params.jobId });
|
||||
if (!job) {
|
||||
return res
|
||||
.status(404)
|
||||
.send(`Job with id ${req.params.jobId} was not found`);
|
||||
}
|
||||
|
||||
// Determine how many items in the queue are ahead of this one
|
||||
const aheadInQueue = await Jobs.find({
|
||||
queue_place: { $lt: job.queuePlace },
|
||||
});
|
||||
|
||||
let text: string = "";
|
||||
if (job.status == Status.ENQUEUED) {
|
||||
text = `Enqueued: ${aheadInQueue.length} Job(s) ahead in queue`;
|
||||
} else if (job.status == Status.PROCESSING) {
|
||||
text = `Currently Processing\n${job.logs}`;
|
||||
} else if (job.status == Status.COMPLETE) {
|
||||
text = job.logs;
|
||||
} else {
|
||||
text = "Could not find job status";
|
||||
}
|
||||
|
||||
return res.status(200).send(text);
|
||||
} catch (ex) {
|
||||
return res.status(500).json(ex);
|
||||
}
|
||||
};
|
||||
49
backend/src/app/controllers/job/put.ts
Normal file
49
backend/src/app/controllers/job/put.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { Request, Response } from "express";
|
||||
import Job from "../../models/Job";
|
||||
import Status from "../../status";
|
||||
import { Fernet } from "fernet-nodejs";
|
||||
|
||||
const getNextQueuePlace = async () => {
|
||||
const allActiveJobs = await Job.find({ status: Status.ENQUEUED }).sort({
|
||||
queue_place: "desc",
|
||||
});
|
||||
if (allActiveJobs.length) {
|
||||
return allActiveJobs[0].queuePlace + 1;
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (
|
||||
!req.body.githubPat ||
|
||||
!req.body.curseforgeSlug ||
|
||||
!req.body.modrinthId
|
||||
) {
|
||||
return res
|
||||
.status(403)
|
||||
.send(
|
||||
[
|
||||
"Missing a required parameter in the JSON body",
|
||||
"Required params: githubPat: string, curseforgeSlug: string, modrinthId: string",
|
||||
].join(" ")
|
||||
);
|
||||
}
|
||||
|
||||
const fern = new Fernet(process.env.SECRET);
|
||||
|
||||
const newJob = new Job({
|
||||
githubPat: fern.encrypt(req.body.githubPat),
|
||||
curseforgeSlug: req.body.curseforgeSlug,
|
||||
modrinthId: req.body.modrinthId,
|
||||
delimiter: req.body.delimiter || "-",
|
||||
queuePlace: await getNextQueuePlace(),
|
||||
});
|
||||
|
||||
await newJob.save();
|
||||
return res.status(200).send(newJob.jobId);
|
||||
} catch (ex) {
|
||||
console.log(ex);
|
||||
return res.status(500).send(ex);
|
||||
}
|
||||
};
|
||||
52
backend/src/app/models/Job.ts
Normal file
52
backend/src/app/models/Job.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import { Document, model, Schema } from "mongoose";
|
||||
import shortid from "shortid";
|
||||
import Status from "../status";
|
||||
|
||||
export type JobType = Document & {
|
||||
jobId: string;
|
||||
githubPat: string;
|
||||
curseforgeSlug: string;
|
||||
modrinthId: string;
|
||||
delimiter: string;
|
||||
logs: string;
|
||||
status: number;
|
||||
queuePlace: number;
|
||||
};
|
||||
|
||||
const JobSchema = new Schema<JobType>({
|
||||
jobId: {
|
||||
type: String,
|
||||
default: shortid.generate,
|
||||
required: true,
|
||||
},
|
||||
githubPat: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
curseforgeSlug: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
modrinthId: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
delimiter: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
logs: {
|
||||
type: String,
|
||||
default: "",
|
||||
},
|
||||
status: {
|
||||
type: Number,
|
||||
default: Status.ENQUEUED,
|
||||
},
|
||||
queuePlace: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
export default model<JobType>("jobs", JobSchema, "jobs");
|
||||
11
backend/src/app/status.ts
Normal file
11
backend/src/app/status.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
enum Status {
|
||||
ENQUEUED = 0,
|
||||
PROCESSING = 1,
|
||||
COMPLETE = 2,
|
||||
SUCCESS = 3,
|
||||
PARTIAL_FAIL = 4,
|
||||
FAIL = 5,
|
||||
NOTFOUND = -1,
|
||||
}
|
||||
|
||||
export default Status;
|
||||
7
backend/src/db.ts
Normal file
7
backend/src/db.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import mongoose from "mongoose";
|
||||
|
||||
export const connectDb = async () => {
|
||||
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/ctmmm`, {
|
||||
autoIndex: true,
|
||||
});
|
||||
};
|
||||
8
backend/src/server.ts
Normal file
8
backend/src/server.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import App from "./app";
|
||||
import { connectDb } from "./db";
|
||||
|
||||
(async () => {
|
||||
await connectDb();
|
||||
const port = process.env.PORT || 3030;
|
||||
App.listen(port, () => console.log(`Listening on http://0.0.0.0:${port}`));
|
||||
})();
|
||||
20
backend/tsconfig.json
Normal file
20
backend/tsconfig.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"target": "es2017",
|
||||
"noImplicitAny": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"*": ["node_modules/*"]
|
||||
},
|
||||
"lib": ["es2019"]
|
||||
},
|
||||
"include": ["src/**/*", "src/types.d.ts"],
|
||||
"exclude": ["test/**/*"]
|
||||
}
|
||||
Loading…
Reference in a new issue