-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add Fastify support (#194) * Create Fastify middleware * Add fastify standalone script * add fastify middleware tests * Add Fastify middleware documentation * Fix Vue template... * fix ObjectId * move fastify to dev dependencies Co-authored-by: Vasyl Boroviak <[email protected]> * Fix after merge conflicts * Run prettier for all JS files Co-authored-by: Maxime Chabert <[email protected]>
- Loading branch information
1 parent
b1f921c
commit 816993e
Showing
7 changed files
with
1,172 additions
and
6,518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
const Agenda = require("agenda"); | ||
const agendash = require("../app"); | ||
const Fastify = require("fastify"); | ||
const program = require("commander"); | ||
|
||
program | ||
.option( | ||
"-d, --db <db>", | ||
"[required] Mongo connection string, same as Agenda connection string" | ||
) | ||
.option( | ||
"-c, --collection <collection>", | ||
"[optional] Mongo collection, same as Agenda collection name, default agendaJobs", | ||
"agendaJobs" | ||
) | ||
.option( | ||
"-p, --port <port>", | ||
"[optional] Server port, default 3000", | ||
(n, d) => Number(n) || d, | ||
3000 | ||
) | ||
.option( | ||
"-t, --title <title>", | ||
"[optional] Page title, default Agendash", | ||
"Agendash" | ||
) | ||
.option( | ||
"-p, --path <path>", | ||
"[optional] Path to bind Agendash to, default /", | ||
"/" | ||
) | ||
.parse(process.argv); | ||
|
||
if (!program.db) { | ||
console.error("--db required"); | ||
process.exit(1); | ||
} | ||
|
||
if (!program.path.startsWith("/")) { | ||
console.error("--path must begin with /"); | ||
process.exit(1); | ||
} | ||
|
||
const fastify = Fastify(); | ||
|
||
const agenda = new Agenda().database(program.db, program.collection); | ||
|
||
fastify.register( | ||
agendash(agenda, { | ||
middleware: "fastify", | ||
title: program.title, | ||
}) | ||
); | ||
|
||
fastify.listen(program.port, function () { | ||
console.log( | ||
`Agendash started http://localhost:${program.port}${program.path}` | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const path = require("path"); | ||
|
||
module.exports = (agendash) => (instance, opts, done) => { | ||
const { api, requeueJobs, deleteJobs, createJob } = agendash; | ||
|
||
instance.register(require("fastify-static"), { | ||
root: path.join(__dirname, "../../public"), | ||
}); | ||
|
||
instance.get("/", function (req, reply) { | ||
return reply.sendFile("index.html"); | ||
}); | ||
|
||
instance.get("/api", async (request, response) => { | ||
try { | ||
const { | ||
job, | ||
state, | ||
skip, | ||
limit, | ||
q, | ||
property, | ||
isObjectId, | ||
} = request.query; | ||
const apiResponse = await api(job, state, { | ||
query: q, | ||
property, | ||
isObjectId, | ||
skip, | ||
limit, | ||
}); | ||
response.send(apiResponse); | ||
} catch (error) { | ||
response.status(400).send(error); | ||
} | ||
}); | ||
|
||
instance.post("/api/jobs/requeue", async (request, response) => { | ||
try { | ||
const newJobs = await requeueJobs(request.body.jobIds); | ||
response.send(newJobs); | ||
} catch (error) { | ||
response.status(404).send(error); | ||
} | ||
}); | ||
|
||
instance.post("/api/jobs/delete", async (request, response) => { | ||
try { | ||
const deleted = await deleteJobs(request.body.jobIds); | ||
if (deleted) { | ||
response.send({ deleted: true }); | ||
} else { | ||
response.send({ message: "Jobs not deleted" }); | ||
} | ||
} catch (error) { | ||
response.status(404).send(error); | ||
} | ||
}); | ||
|
||
instance.post("/api/jobs/create", async (request, response) => { | ||
try { | ||
await createJob( | ||
request.body.jobName, | ||
request.body.jobSchedule, | ||
request.body.jobRepeatEvery, | ||
request.body.jobData | ||
); | ||
response.send({ created: true }); | ||
} catch (error) { | ||
response.status(400).send(error); | ||
} | ||
}); | ||
|
||
done(); | ||
}; |
Oops, something went wrong.