code-server/src/node/app/app.ts

46 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as http from "http"
import { HttpCode, HttpError } from "../../common/http"
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
import { ApiHttpProvider } from "./api"
/**
2020-03-03 01:43:02 +07:00
* App/fallback HTTP provider.
*/
2020-03-03 01:43:02 +07:00
export class AppHttpProvider extends HttpProvider {
public constructor(options: HttpProviderOptions, private readonly api: ApiHttpProvider) {
super(options)
}
2020-03-03 01:43:02 +07:00
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
if (!this.authenticated(request)) {
return { redirect: "/login", query: { to: route.fullPath } }
}
// Run an existing app, but if it doesn't exist go ahead and start it.
let app = this.api.getRunningApplication(route.base)
let sessionId = app && app.sessionId
if (!app) {
app = (await this.api.installedApplications()).find((a) => a.path === route.base)
2020-03-03 01:43:02 +07:00
if (app && app.exec) {
sessionId = await this.api.createSession(app)
}
}
if (sessionId) {
return this.getAppRoot(route, (app && app.name) || "", sessionId)
}
2020-03-03 01:43:02 +07:00
throw new HttpError("Application not found", HttpCode.NotFound)
}
public async getAppRoot(route: Route, name: string, sessionId: string): Promise<HttpResponse> {
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/app.html")
response.content = response.content.replace(/{{APP_NAME}}/, name)
return this.replaceTemplates(route, response, sessionId)
}
2020-03-03 01:43:02 +07:00
public async handleWebSocket(): Promise<true> {
throw new HttpError("Not found", HttpCode.NotFound)
2020-02-15 04:57:51 +07:00
}
}