Skip to content

Commit e6ae10b

Browse files
committed
Start integrating async lifecycle managers
1 parent 34d68cf commit e6ae10b

File tree

3 files changed

+117
-114
lines changed

3 files changed

+117
-114
lines changed

server.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,48 @@ const dotenv = require("dotenv");
22
// Load environment variables from .env file
33
dotenv.config();
44

5+
import { buildAPI } from "./v2";
6+
57
var express = require("express"),
6-
bodyParser = require("body-parser"),
7-
//v1 = require("./v1"),
8-
v2 = require("./v2"),
9-
defs = require("./v2/defs"),
10-
app = express();
8+
bodyParser = require("body-parser");
9+
//defs = require("./v2/defs"),
1110

12-
// parse application/x-www-form-urlencoded
13-
app.use(bodyParser.urlencoded({ extended: false }));
11+
//TODO: update port to designated env.
12+
const listenPort = process.argv[2] ?? process.env.PORT ?? 5000;
1413

15-
// parse application/json
16-
app.use(bodyParser.json());
14+
async function runServer() {
15+
const app = express();
16+
// parse application/x-www-form-urlencoded
17+
app.use(bodyParser.urlencoded({ extended: false }));
1718

18-
// parse application/vnd.api+json as json
19-
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
19+
// parse application/json
20+
app.use(bodyParser.json());
2021

21-
// Load and prefix all routes with /api and appropriate version
22-
app.use("/v2", v2);
22+
// parse application/vnd.api+json as json
23+
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
2324

24-
app.route("/v1*").get(function (req, res, next) {
25-
res.status(410).send({
26-
error:
27-
"Macrostrat's v1 API has been retired. Please update your usage to newer endpoints.",
28-
});
29-
});
25+
const v2 = await buildAPI();
3026

31-
// If no version specified, fall back to more current
32-
app.use("/", v2);
27+
// Load and prefix all routes with /api and appropriate version
28+
app.use("/v2", v2);
3329

34-
app.set("json spaces", 2);
30+
app.route("/v1*").get(function (req, res, next) {
31+
res.status(410).send({
32+
error:
33+
"Macrostrat's v1 API has been retired. Please update your usage to newer endpoints.",
34+
});
35+
});
3536

36-
//TODO: update port to designated env.
37-
app.port = process.argv[2] ?? process.env.PORT ?? 5000;
37+
// If no version specified, fall back to more current
38+
app.use("/", v2);
3839

39-
app.start = function () {
40-
app.listen(app.port, function () {
41-
console.log("Listening on port " + app.port);
42-
});
43-
};
40+
app.set("json spaces", 2);
4441

45-
if (!module.parent) {
46-
app.start();
42+
app.listen(listenPort, function () {
43+
console.log("Listening on port " + listenPort);
44+
});
4745
}
4846

49-
module.exports = app;
47+
runServer().catch((error) => {
48+
console.error("Failed to start server:", error);
49+
});

v2/index.ts

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,106 @@
1-
var api = require("./api"),
2-
larkin = require("./larkin");
1+
const api = require("./api");
2+
const larkin = require("./larkin");
33

4-
larkin.checkCapabilities(api).then((capabilities) => {
5-
if (capabilities.size > 0) {
6-
let msg = "Progressive enhancements enabled:\n";
7-
capabilities.forEach((cap) => {
8-
msg += `- ${cap}\n`;
9-
});
10-
larkin.log("info", msg);
11-
} else {
12-
console.log("No enhancements available.");
13-
}
14-
});
4+
export async function buildAPI() {
5+
await larkin.checkCapabilities(api);
156

16-
// Load route categories
17-
api.use("/carto", require("./carto"));
18-
api.use("/defs", require("./definitions"));
19-
api.use("/grids", require("./grids"));
20-
api.use("/mobile", require("./mobile"));
7+
// Load route categories
8+
api.use("/carto", require("./carto"));
9+
api.use("/defs", require("./definitions"));
10+
api.use("/grids", require("./grids"));
11+
api.use("/mobile", require("./mobile"));
2112

22-
api.route("/").get(require("./root"));
13+
api.route("/").get(require("./root"));
2314

24-
api.route("/meta").get(require("./meta"));
15+
api.route("/meta").get(require("./meta"));
2516

26-
api.route("/changes").get(function (req, res, next) {
27-
res.sendFile(__dirname + "/changes.html");
28-
});
17+
api.route("/changes").get(function (req, res, next) {
18+
res.sendFile(__dirname + "/changes.html");
19+
});
2920

30-
api.route("/columns/refresh-cache").get(require("./column-cache-refresh"));
21+
api.route("/columns/refresh-cache").get(require("./column-cache-refresh"));
3122

32-
api.route("/columns").get(function (req, res, next) {
33-
require("./columns")(req, res, next);
34-
});
23+
api.route("/columns").get(function (req, res, next) {
24+
require("./columns")(req, res, next);
25+
});
3526

36-
api.route("/sections").get(require("./sections"));
27+
api.route("/sections").get(require("./sections"));
3728

38-
api.route("/units").get(function (req, res, next) {
39-
require("./units")(req, res, next);
40-
});
29+
api.route("/units").get(function (req, res, next) {
30+
require("./units")(req, res, next);
31+
});
4132

42-
api.route("/fossils").get(require("./fossils"));
33+
api.route("/fossils").get(require("./fossils"));
4334

44-
api.route("/stats").get(require("./stats"));
35+
api.route("/stats").get(require("./stats"));
4536

46-
api.route("/paleogeography").get(require("./paleogeography"));
37+
api.route("/paleogeography").get(require("./paleogeography"));
4738

48-
api.route("/geologic_units/gmna").get(require("./geologic_units_gmna"));
39+
api.route("/geologic_units/gmna").get(require("./geologic_units_gmna"));
4940

50-
api.route("/geologic_units/gmus").get(require("./geologic_units_gmus"));
41+
api.route("/geologic_units/gmus").get(require("./geologic_units_gmus"));
5142

52-
api.route("/geologic_units/burwell").get(function (req, res, next) {
53-
require("./geologic_units_burwell")(req, res, next);
54-
});
43+
api.route("/geologic_units/burwell").get(function (req, res, next) {
44+
require("./geologic_units_burwell")(req, res, next);
45+
});
5546

56-
api.route("/geologic_units/map").get(function (req, res, next) {
57-
require("./geologic_units_burwell")(req, res, next);
58-
});
47+
api.route("/geologic_units/map").get(function (req, res, next) {
48+
require("./geologic_units_burwell")(req, res, next);
49+
});
5950

60-
api
61-
.route("/geologic_units/burwell/nearby")
62-
.get(require("./geologic_units_burwell_nearby"));
51+
api
52+
.route("/geologic_units/burwell/nearby")
53+
.get(require("./geologic_units_burwell_nearby"));
6354

64-
api
65-
.route("/geologic_units/map/nearby")
66-
.get(require("./geologic_units_burwell_nearby"));
55+
api
56+
.route("/geologic_units/map/nearby")
57+
.get(require("./geologic_units_burwell_nearby"));
6758

68-
api
69-
.route("/geologic_units/burwell/points")
70-
.get(require("./geologic_units_burwell_points"));
59+
api
60+
.route("/geologic_units/burwell/points")
61+
.get(require("./geologic_units_burwell_points"));
7162

72-
api
73-
.route("/geologic_units/map/points")
74-
.get(require("./geologic_units_burwell_points"));
63+
api
64+
.route("/geologic_units/map/points")
65+
.get(require("./geologic_units_burwell_points"));
7566

76-
api.route("/geologic_units/map/legend").get(function (req, res, next) {
77-
require("./geologic_units_burwell_legend")(req, res, next);
78-
});
67+
api.route("/geologic_units/map/legend").get(function (req, res, next) {
68+
require("./geologic_units_burwell_legend")(req, res, next);
69+
});
7970

80-
api.route("/elevation").get(function (req, res, next) {
81-
require("./elevation")(req, res, next);
82-
});
71+
api.route("/elevation").get(function (req, res, next) {
72+
require("./elevation")(req, res, next);
73+
});
8374

84-
api.route("/places").get(function (req, res, next) {
85-
require("./places")(req, res, next);
86-
});
75+
api.route("/places").get(function (req, res, next) {
76+
require("./places")(req, res, next);
77+
});
8778

88-
api.route("/measurements").get(require("./measurements"));
79+
api.route("/measurements").get(require("./measurements"));
8980

90-
api.route("/age_model").get(require("./age_model"));
81+
api.route("/age_model").get(require("./age_model"));
9182

92-
api.route("/eodp").get(require("./eodp"));
93-
//api.route("/hillshade")
94-
// .get(require("./hillshade"));
83+
api.route("/eodp").get(require("./eodp"));
84+
//api.route("/hillshade")
85+
// .get(require("./hillshade"));
9586

96-
api.route("/boundaries").get(require("./boundaries"));
87+
api.route("/boundaries").get(require("./boundaries"));
9788

98-
api.route("/hex-summary").get(require("./hex_summary"));
89+
api.route("/hex-summary").get(require("./hex_summary"));
9990

100-
api.route("/hex-summary/max/:zoom").get(require("./hex_summary_max"));
91+
api.route("/hex-summary/max/:zoom").get(require("./hex_summary_max"));
10192

102-
api.route("*").get(require("./catchall"));
93+
api.route("*").get(require("./catchall"));
10394

104-
api.use(function (err, req, res, next) {
105-
if (err.status !== 404) {
106-
return next();
107-
} else if (err.status === 404) {
108-
larkin.error(req, res, next, "404: Page not found", 404);
109-
} else {
110-
larkin.error(req, res, next, "500: Internal Server Error", 500);
111-
}
112-
});
95+
api.use(function (err, req, res, next) {
96+
if (err.status !== 404) {
97+
return next();
98+
} else if (err.status === 404) {
99+
larkin.error(req, res, next, "404: Page not found", 404);
100+
} else {
101+
larkin.error(req, res, next, "500: Internal Server Error", 500);
102+
}
103+
});
113104

114-
module.exports = api;
105+
return api;
106+
}

v2/larkin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,17 @@ enum APICapability {
696696
} catch (e) {
697697
console.log("Composite projects not supported");
698698
}
699+
700+
if (larkin.capabilities.size > 0) {
701+
let msg = "Progressive enhancements enabled:\n";
702+
larkin.capabilities.forEach((cap) => {
703+
msg += `- ${cap}\n`;
704+
});
705+
larkin.log("info", msg);
706+
} else {
707+
larkin.log("info", "No enhancements available.");
708+
}
709+
699710
return larkin.capabilities;
700711
};
701712

0 commit comments

Comments
 (0)