Skip to content

Commit

Permalink
Convert site server to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
nsams committed Oct 9, 2024
1 parent 3fed4be commit b504bc8
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 52 deletions.
2 changes: 2 additions & 0 deletions site/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ public/sitemap.xml
public/robots.txt

tsconfig.tsbuildinfo

/dist
2 changes: 2 additions & 0 deletions site/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ public/sitemap.xml
public/robots.txt

tsconfig.tsbuildinfo

/dist/
6 changes: 3 additions & 3 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "starter-site",
"private": true,
"scripts": {
"dev": "npm run intl:compile && run-s gql:types generate-block-types && NODE_OPTIONS='--inspect=localhost:9230' dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- node server.js",
"build": "npm run intl:compile && run-p gql:types generate-block-types && next build",
"serve": "NODE_ENV=production node server.js",
"dev": "npm run intl:compile && run-s gql:types generate-block-types && NODE_OPTIONS='--inspect=localhost:9230' dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- ts-node --project tsconfig.server.json server.ts",
"build": "npm run intl:compile && run-p gql:types generate-block-types && tsc --project tsconfig.server.json && next build",
"serve": "NODE_ENV=production node dist/server.js",
"gql:types": "graphql-codegen",
"gql:watch": "graphql-codegen --watch",
"prelint": "npm run intl:compile && run-p gql:types generate-block-types",
Expand Down
44 changes: 0 additions & 44 deletions site/server.js

This file was deleted.

47 changes: 47 additions & 0 deletions site/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { createServer } from "http";
import next from "next";
import { parse } from "url";

const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = parseInt(process.env.PORT || "3000", 10);
const cdnOriginCheckSecret = process.env.CDN_ORIGIN_CHECK_SECRET;

// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();

app.prepare().then(() => {
if (process.env.TRACING_ENABLED) {
require("./tracing");
}
createServer(async (req, res) => {
try {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const parsedUrl = parse(req.url!, true);

if (cdnOriginCheckSecret) {
if (req.headers["x-cdn-origin-check"] !== cdnOriginCheckSecret) {
res.statusCode = 403;
res.end();
return;
}
}
await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("internal server error");
}
})
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${port}`);
});
});
10 changes: 5 additions & 5 deletions site/tracing.js → site/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { diag, DiagConsoleLogger, DiagLogLevel } = require("@opentelemetry/api");
const opentelemetry = require("@opentelemetry/sdk-node");
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http");
const { IncomingMessage } = require("http");
import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import * as opentelemetry from "@opentelemetry/sdk-node";
import { IncomingMessage } from "http";

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

Expand Down
12 changes: 12 additions & 0 deletions site/tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"lib": ["es2019"],
"target": "es2019",
"isolatedModules": false,
"noEmit": false
},
"include": ["server.ts", "tracing.ts"]
}

0 comments on commit b504bc8

Please sign in to comment.