entry.ts 3.3 KB
Newer Older
A
Asher 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import { logger } from "@coder/logger"
import { ApiHttpProvider } from "./api/server"
import { MainHttpProvider } from "./app/server"
import { AuthType, HttpServer } from "./http"
import { generateCertificate, generatePassword, hash, open } from "./util"
import { VscodeHttpProvider } from "./vscode/server"
import { ipcMain, wrap } from "./wrapper"

export interface Args {
  auth?: AuthType
  "base-path"?: string
  cert?: string
  "cert-key"?: string
  format?: string
  host?: string
  open?: boolean
  port?: string
  socket?: string
  _?: string[]
}

const main = async (args: Args = {}): Promise<void> => {
A
Asher 已提交
23 24 25
  const auth = args.auth || AuthType.Password
  const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))

A
Asher 已提交
26 27 28 29 30 31 32
  let commit = "development"
  try {
    commit = require("../../package.json").commit
  } catch (error) {
    logger.warn(error.message)
  }

A
Asher 已提交
33 34
  // Spawn the main HTTP server.
  const options = {
A
Asher 已提交
35
    auth,
A
Asher 已提交
36 37 38
    basePath: args["base-path"],
    cert: args.cert,
    certKey: args["cert-key"],
A
Asher 已提交
39
    commit,
A
Asher 已提交
40
    host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
A
Asher 已提交
41
    password: originalPassword ? hash(originalPassword) : undefined,
A
Asher 已提交
42 43 44 45 46 47 48 49 50
    port: typeof args.port !== "undefined" ? parseInt(args.port, 10) : 8080,
    socket: args.socket,
  }
  if (!options.cert && typeof options.cert !== "undefined") {
    const { cert, certKey } = await generateCertificate()
    options.cert = cert
    options.certKey = certKey
  }

A
Asher 已提交
51
  const httpServer = new HttpServer(options)
A
Asher 已提交
52 53 54
  httpServer.registerHttpProvider("/", MainHttpProvider)
  httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer)
  httpServer.registerHttpProvider("/vscode-embed", VscodeHttpProvider, [])
A
Asher 已提交
55

56
  ipcMain().onDispose(() => httpServer.dispose())
A
Asher 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

  const serverAddress = await httpServer.listen()
  logger.info(`Server listening on ${serverAddress}`)

  if (auth === AuthType.Password && !process.env.PASSWORD) {
    logger.info(`  - Password is ${originalPassword}`)
    logger.info("    - To use your own password, set the PASSWORD environment variable")
    if (!args.auth) {
      logger.info("    - To disable use `--auth none`")
    }
  } else if (auth === AuthType.Password) {
    logger.info("  - Using custom password for authentication")
  } else {
    logger.info("  - No authentication")
  }

  if (httpServer.protocol === "https") {
    logger.info(
      args.cert
        ? `  - Using provided certificate${args["cert-key"] ? " and key" : ""} for HTTPS`
        : `  - Using generated certificate and key for HTTPS`
    )
  } else {
    logger.info("  - Not serving HTTPS")
  }

  if (serverAddress && !options.socket && args.open) {
    // The web socket doesn't seem to work if browsing with 0.0.0.0.
    const openAddress = serverAddress.replace(/:\/\/0.0.0.0/, "://localhost")
    await open(openAddress).catch(console.error)
    logger.info(`  - Opened ${openAddress}`)
  }
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
// TODO: Implement CLI parser.
if (process.argv.includes("--version")) {
  const version = require("../../package.json").version
  if (process.argv.includes("--json")) {
    console.log({
      codeServer: version,
      vscode: require("../../lib/vscode/package.json").version,
    })
  } else {
    console.log(version)
  }
  process.exit(0)
} else {
  wrap(main)
}