Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert site server to TypeScript #389

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
}
Loading