Archived
1
0

Improve routing

This commit is contained in:
Asher
2020-02-04 18:16:45 -06:00
parent dbc5c065f8
commit 8cc11d1688
26 changed files with 289 additions and 267 deletions

View File

@ -1,33 +1,49 @@
import { logger } from "@coder/logger"
import * as http from "http"
import * as querystring from "querystring"
import * as React from "react"
import * as ReactDOMServer from "react-dom/server"
import * as ReactRouterDOM from "react-router-dom"
import App from "../../browser/app"
import { Options } from "../../common/util"
import { HttpProvider, HttpResponse } from "../http"
/**
* Top-level and fallback HTTP provider.
*/
export class MainHttpProvider extends HttpProvider {
public async handleRequest(base: string, requestPath: string): Promise<HttpResponse | undefined> {
public async handleRequest(
base: string,
requestPath: string,
_query: querystring.ParsedUrlQuery,
request: http.IncomingMessage
): Promise<HttpResponse | undefined> {
if (base === "/static") {
const response = await this.getResource(this.rootPath, requestPath)
response.cache = true
if (this.options.commit && this.options.commit !== "development") {
response.cache = true
}
return response
}
// TEMP: Auto-load VS Code for now. In future versions we'll need to check
// the URL for the appropriate application to load, if any.
const app = {
name: "VS Code",
path: "/",
embedPath: "/vscode-embed",
}
const options: Options = {
app,
authed: !!this.authenticated(request),
logLevel: logger.level,
}
const response = await this.getUtf8Resource(this.rootPath, "src/browser/index.html")
response.content = response.content
.replace(/{{COMMIT}}/g, "") // TODO
.replace(/"{{OPTIONS}}"/g, `'${JSON.stringify({ logLevel: logger.level })}'`)
.replace(
/{{COMPONENT}}/g,
ReactDOMServer.renderToString(
<ReactRouterDOM.StaticRouter location={base}>
<App />
</ReactRouterDOM.StaticRouter>
)
)
.replace(/{{COMMIT}}/g, this.options.commit || "development")
.replace(/"{{OPTIONS}}"/g, `'${JSON.stringify(options)}'`)
.replace(/{{COMPONENT}}/g, ReactDOMServer.renderToString(<App options={options} />))
return response
}