-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert site server to TypeScript (#389)
Demo implementation: vivid-planet/comet#2595 and vivid-planet/comet#2617
- Loading branch information
Showing
7 changed files
with
71 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,3 +51,5 @@ public/sitemap.xml | |
public/robots.txt | ||
|
||
tsconfig.tsbuildinfo | ||
|
||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,5 @@ public/sitemap.xml | |
public/robots.txt | ||
|
||
tsconfig.tsbuildinfo | ||
|
||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |