-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathagendash-standalone-koa.js
executable file
·77 lines (66 loc) · 1.91 KB
/
agendash-standalone-koa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
"use strict";
const { Agenda, DataSource } = require("@cawstudios/agenda");
const agendash = require("../app");
const Koa = require("koa");
const { Command } = require("commander");
const program = new Command();
program
.option("-t, --dataSourceType <type>", "Data source type (mongo or postgres)", /^(mongo|postgres)$/i)
.option("-d, --db <connection>", "DB connection string")
.option("-c, --collection <name>", "Mongo collection name", "agendaJobs")
.option("-p, --port <number>", "Server port", parseInt, 3000)
.option("-t, --title <string>", "Page title", "Agendash")
.option("-p, --path <path>", "Path to bind Agendash to", "/")
.parse(process.argv);
const options = program.opts();
// Validation
if (!options.dataSourceType) {
console.error("--dataSourceType required (mongo or postgres)");
process.exit(1);
}
if (!options.db) {
console.error("--db required");
process.exit(1);
}
if (!options.path.startsWith("/")) {
console.error("--path must begin with /");
process.exit(1);
}
// DB Configuration
const dbConfig = {
[DataSource.POSTGRES]: {
dataSource: DataSource.POSTGRES,
dataSourceOptions: {
db: {
connectionString: options.db
}
}
},
[DataSource.MONGO]: {
dataSource: DataSource.MONGO,
dataSourceOptions: { db: { address: options.db, collection: options.collection } }
}
}[options.dataSourceType];
if (!dbConfig) {
console.error("Unsupported data source");
process.exit(1);
}
const init = async () => {
const agenda = new Agenda();
await agenda.database(dbConfig);
const app = new Koa();
const middlewares = agendash(agenda, {
middleware: "koa",
});
for (const middleware of middlewares) {
app.use(middleware);
}
await app.listen(program.port);
console.log("Server running on port %s", program.port);
};
process.on("unhandledRejection", (error) => {
console.log(error);
process.exit(1);
});
init();