Separate API, proc
This commit is contained in:
parent
c1f1742948
commit
6a3f531527
27 changed files with 334 additions and 104 deletions
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}`));
|
||||
})();
|
||||
Loading…
Reference in a new issue