Skip to content

Commit 7b7f5b5

Browse files
committed
Add base path argument
It's only used for the login redirect.
1 parent 92daabc commit 7b7f5b5

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { generateCertificate, generatePassword, open, unpackExecutables } from "
1515
interface Args extends ParsedArgs {
1616
"allow-http"?: boolean;
1717
auth?: boolean;
18+
"base-path"?: string;
1819
cert?: string;
1920
"cert-key"?: string;
2021
"extra-builtin-extensions-dir"?: string;
@@ -54,6 +55,7 @@ while (i--) {
5455
}
5556

5657
options.push({ id: "allow-http", type: "boolean", cat: "o", description: "Allow http connections." });
58+
options.push({ id: "base-path", type: "string", cat: "o", description: "Base path of the URL at which code-server is hosted (used for login redirects)." });
5759
options.push({ id: "cert", type: "string", cat: "o", description: "Path to certificate." });
5860
options.push({ id: "cert-key", type: "string", cat: "o", description: "Path to certificate key." });
5961
options.push({ id: "extra-builtin-extensions-dir", type: "string", cat: "o", description: "Path to extra builtin extension directory." });
@@ -118,6 +120,7 @@ const main = async (): Promise<void> => {
118120
const options = {
119121
allowHttp: args["allow-http"],
120122
auth: typeof args.auth !== "undefined" ? args.auth : true,
123+
basePath: args["base-path"],
121124
cert: args.cert,
122125
certKey: args["cert-key"],
123126
folderUri: extra.length > 1 ? extra[extra.length - 1] : undefined,

src/server.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,30 @@ export class HttpError extends Error {
9696
}
9797

9898
export interface ServerOptions {
99-
readonly port?: number;
100-
readonly host?: string;
101-
readonly socket?: string;
10299
readonly allowHttp?: boolean;
100+
readonly auth?: boolean;
101+
readonly basePath?: string;
103102
readonly cert?: string;
104103
readonly certKey?: string;
105-
readonly auth?: boolean;
106-
readonly password?: string;
107104
readonly folderUri?: string;
105+
readonly host?: string;
106+
readonly password?: string;
107+
readonly port?: number;
108+
readonly socket?: string;
108109
}
109110

110111
export abstract class Server {
111112
protected readonly server: http.Server | https.Server;
112113
protected rootPath = path.resolve(__dirname, "../../../..");
113114
private listenPromise: Promise<string> | undefined;
114115
private readonly protocol: string;
116+
private readonly basePath: string = "";
115117

116118
public constructor(public readonly options: ServerOptions) {
117119
this.protocol = this.options.allowHttp ? "http" : "https";
120+
if (this.options.basePath) {
121+
this.basePath = this.options.basePath.replace(/\/+$/, "");
122+
}
118123
if (this.options.cert && this.options.certKey) {
119124
const httpolyglot = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/httpolyglot/lib/index")) as typeof import("httpolyglot");
120125
this.server = httpolyglot.createServer({
@@ -175,7 +180,7 @@ export abstract class Server {
175180
"Cache-Control": "max-age=86400", // TODO: ETag?
176181
"Content-Type": getMediaMime(payload.filePath),
177182
...(payload.redirect ? {
178-
Location: `${this.protocol}://${request.headers.host}${payload.redirect}`,
183+
Location: `${this.protocol}://${request.headers.host}${this.basePath}${payload.redirect}`,
179184
} : {}),
180185
...payload.headers,
181186
});

0 commit comments

Comments
 (0)