Add Railway health endpoint and server entrypoint
This commit is contained in:
@@ -4,9 +4,9 @@
|
|||||||
"description": "BlackRoad OS – Public API gateway service",
|
"description": "BlackRoad OS – Public API gateway service",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "ts-node-dev src/index.ts",
|
"dev": "ts-node src/server.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node dist/index.js",
|
"start": "node dist/server.js",
|
||||||
"test": "NODE_ENV=test jest"
|
"test": "NODE_ENV=test jest"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
11
src/index.ts
11
src/index.ts
@@ -1,8 +1,7 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { env } from "./config/env";
|
|
||||||
import { createProxyRouter } from "./routes/proxy";
|
import { createProxyRouter } from "./routes/proxy";
|
||||||
import { serviceClients } from "./lib/httpClient";
|
import { serviceClients } from "./lib/httpClient";
|
||||||
import healthRouter from "./routes/health";
|
import health from "./routes/health";
|
||||||
import infoRouter from "./routes/info";
|
import infoRouter from "./routes/info";
|
||||||
import versionRouter from "./routes/version";
|
import versionRouter from "./routes/version";
|
||||||
import pingRouter from "./routes/v1/ping";
|
import pingRouter from "./routes/v1/ping";
|
||||||
@@ -14,7 +13,7 @@ const app = express();
|
|||||||
app.use(express.json({ limit: "5mb" }));
|
app.use(express.json({ limit: "5mb" }));
|
||||||
|
|
||||||
// API routes
|
// API routes
|
||||||
app.use(healthRouter);
|
app.use(health);
|
||||||
app.use(infoRouter);
|
app.use(infoRouter);
|
||||||
app.use(versionRouter);
|
app.use(versionRouter);
|
||||||
app.use("/v1", pingRouter);
|
app.use("/v1", pingRouter);
|
||||||
@@ -31,10 +30,4 @@ app.use((err: Error, _req: express.Request, res: express.Response, _next: expres
|
|||||||
res.status(502).json({ error: "Upstream request failed" });
|
res.status(502).json({ error: "Upstream request failed" });
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "test") {
|
|
||||||
app.listen(env.PORT, env.HOST, () => {
|
|
||||||
console.log(`Gateway listening on ${env.HOST}:${env.PORT}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import { Router, Request, Response } from "express";
|
import { Router } from "express";
|
||||||
import { SERVICE_ID } from "../config/serviceConfig";
|
|
||||||
|
|
||||||
const router = Router();
|
const api = Router();
|
||||||
|
|
||||||
router.get("/health", (req: Request, res: Response) => {
|
api.get("/api/health", (_req, res) => {
|
||||||
res.status(200).json({
|
res.status(200).json({ status: "ok", service: "blackroad-os-api" });
|
||||||
status: "ok",
|
|
||||||
service: SERVICE_ID,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default api;
|
||||||
|
|||||||
7
src/server.ts
Normal file
7
src/server.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import app from "./index";
|
||||||
|
|
||||||
|
const port = process.env.PORT || 8080;
|
||||||
|
|
||||||
|
app.listen(port, "0.0.0.0", () => {
|
||||||
|
console.log(`[blackroad-os-api] listening on http://0.0.0.0:${port}`);
|
||||||
|
});
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import request from "supertest";
|
import request from "supertest";
|
||||||
import app from "../src/index";
|
import app from "../src/index";
|
||||||
|
|
||||||
describe("GET /health", () => {
|
describe("GET /api/health", () => {
|
||||||
it("returns ok status and service id", async () => {
|
it("returns ok status and service id", async () => {
|
||||||
const response = await request(app).get("/health");
|
const response = await request(app).get("/api/health");
|
||||||
|
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.body).toHaveProperty("status", "ok");
|
expect(response.body).toHaveProperty("status", "ok");
|
||||||
expect(response.body).toHaveProperty("service", "api");
|
expect(response.body).toHaveProperty("service", "blackroad-os-api");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user