server.tsx 1.6 KB
Newer Older
A
Asher 已提交
1
import { logger } from "@coder/logger"
A
Asher 已提交
2 3
import * as http from "http"
import * as querystring from "querystring"
A
Asher 已提交
4 5 6
import * as React from "react"
import * as ReactDOMServer from "react-dom/server"
import App from "../../browser/app"
A
Asher 已提交
7
import { Options } from "../../common/util"
A
Asher 已提交
8 9 10 11 12 13
import { HttpProvider, HttpResponse } from "../http"

/**
 * Top-level and fallback HTTP provider.
 */
export class MainHttpProvider extends HttpProvider {
A
Asher 已提交
14 15 16 17 18 19
  public async handleRequest(
    base: string,
    requestPath: string,
    _query: querystring.ParsedUrlQuery,
    request: http.IncomingMessage
  ): Promise<HttpResponse | undefined> {
A
Asher 已提交
20 21
    if (base === "/static") {
      const response = await this.getResource(this.rootPath, requestPath)
A
Asher 已提交
22 23 24
      if (this.options.commit && this.options.commit !== "development") {
        response.cache = true
      }
A
Asher 已提交
25 26 27
      return response
    }

A
Asher 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
    // 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,
    }

A
Asher 已提交
42 43
    const response = await this.getUtf8Resource(this.rootPath, "src/browser/index.html")
    response.content = response.content
A
Asher 已提交
44 45 46
      .replace(/{{COMMIT}}/g, this.options.commit || "development")
      .replace(/"{{OPTIONS}}"/g, `'${JSON.stringify(options)}'`)
      .replace(/{{COMPONENT}}/g, ReactDOMServer.renderToString(<App options={options} />))
A
Asher 已提交
47 48 49 50 51 52 53
    return response
  }

  public async handleWebSocket(): Promise<undefined> {
    return undefined
  }
}