Skip to content

Commit

Permalink
tidy up server
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Jan 21, 2025
1 parent d08f421 commit ffed70c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/db/tileDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as sqlite3 from "sqlite3";
import sqlite3 from "sqlite3";
import { open, Database } from "sqlite";
import { TileDataResultRow } from "../types/db";

Expand Down
62 changes: 30 additions & 32 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import compression from "compression";
import cors from "cors";
import express from "express";
import url from "url";
import * as path from "node:path";
import { ConfigReader } from "./server/configReader";
import { GroutConfig } from "./types/app";
Expand All @@ -10,41 +11,38 @@ import { discoverTileDatasets } from "./server/discover";
import { handleError } from "./errors/handleError";
import { buildMetadata } from "./server/buildMetadata";

//// TODO: we might be able to take this out once we configure modules
import url from 'url'
// Set __dirname for ESM
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Wrap the main server set-up functionality in a non-top-level method so we can use async - we can revert this in
// https://mrc-ide.myjetbrains.com/youtrack/issue/mrc-6134/Add-Vite-build-and-related-tidy-up
const app = express();
//const main = async () => {
initialiseLogging(app);
app.use(cors());
app.use(compression({ level: 9 })); // Use best compression

const rootDir = path.resolve(path.join(__dirname, ".."));
const configReader = new ConfigReader(path.join(rootDir, "config"));
const { port } = configReader.readConfigFile(
"grout.config.json"
) as GroutConfig;

const tileDatasets = await discoverTileDatasets(
path.resolve(path.join(rootDir, "data"))
);

const metadata = buildMetadata(tileDatasets);

Object.assign(app.locals, {
tileDatasets,
metadata
});
Object.freeze(app.locals); // We don't expect anything else to modify app.locals

app.use("/", registerRoutes());
app.use(handleError);
app.listen(port, () => {
console.log(`Grout is running on port ${port}`);
});

initialiseLogging(app);
app.use(cors());
app.use(compression({ level: 9 })); // Use best compression

const rootDir = path.resolve(path.join(__dirname, ".."));
const configReader = new ConfigReader(path.join(rootDir, "config"));
const { port } = configReader.readConfigFile(
"grout.config.json"
) as GroutConfig;

const tileDatasets = await discoverTileDatasets(
path.resolve(path.join(rootDir, "data"))
);

const metadata = buildMetadata(tileDatasets);

Object.assign(app.locals, {
tileDatasets,
metadata
});
Object.freeze(app.locals); // We don't expect anything else to modify app.locals

app.use("/", registerRoutes());
app.use(handleError);
app.listen(port, () => {
console.log(`Grout is running on port ${port}`);
});

export const viteNodeApp = app;
6 changes: 1 addition & 5 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@ export default defineConfig({
initAppOnBoot: true,
outputFormat: "esm"
})
],
server: {
port: 5000,
open: false // do not open browser on dev run
}
]
});

0 comments on commit ffed70c

Please sign in to comment.