Skip to content

set default base_path hono /companion + allow to customize it #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ COPY ./config/ /app/config/
COPY --from=builder /tini /tini

ENV PORT=8282 \
HOST=0.0.0.0
HOST=0.0.0.0 \
SERVER_BASE_URL=/companion

ENV THC_PORT=${PORT} \
THC_PATH=/healthz
THC_PATH=${SERVER_BASE_URL}/healthz

# Copy passwd file for the non-privileged user from the user-stage
COPY --from=user-stage /etc/passwd /etc/passwd
Expand Down
1 change: 1 addition & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# [server]
# port = 8282 # env variable: PORT
# host = "127.0.0.1" # env variable: HOST
# base_path = "/companion" # env variable: SERVER_BASE_PATH
# # secret key needs to be 16 characters long or more
# secret_key = "CHANGE_ME" # env variable: SERVER_SECRET_KEY
# verify_requests = false # env variable: SERVER_VERIFY_REQUESTS
Expand Down
3 changes: 3 additions & 0 deletions src/lib/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const ConfigSchema = z.object({
server: z.object({
port: z.number().default(Number(Deno.env.get("PORT")) || 8282),
host: z.string().default(Deno.env.get("HOST") || "127.0.0.1"),
base_path: z.string().default(
Deno.env.get("SERVER_BASE_PATH") || "/companion",
),
Comment on lines +8 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
base_path: z.string().default(
Deno.env.get("SERVER_BASE_PATH") || "/companion",
),
base_path: z.string().regex("^/").default(
Deno.env.get("SERVER_BASE_PATH") || "/companion",
),

Might be worth considering adding a quick regex check here to ensure it's prefixed with a forward-slash. Without it (i.e. base_path = "companion" in the toml), companion starts up fine but doesn't work in a non-obvious way:

image

The suggestion is untested.

secret_key: z.string().length(16).default(
Deno.env.get("SERVER_SECRET_KEY") || "",
),
Expand Down
13 changes: 11 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const { getFetchClient } = await import(getFetchClientLocation);
declare module "hono" {
interface ContextVariableMap extends HonoVariables {}
}
const app = new Hono();
const app = new Hono().basePath(config.server.base_path);
const metrics = config.server.enable_metrics ? new Metrics() : undefined;

let tokenMinter: TokenMinter;
Expand Down Expand Up @@ -125,7 +125,16 @@ routes(app, config);

export function run(signal: AbortSignal, port: number, hostname: string) {
return Deno.serve(
{ signal: signal, port: port, hostname: hostname },
{
onListen() {
console.log(
`[INFO] Server successfully started at http://${config.server.host}:${config.server.port}${config.server.base_path}`,
);
},
signal: signal,
port: port,
hostname: hostname,
},
app.fetch,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/invidious_routes/captions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ captionsHandler.get("/:videoId", async (c) => {
invidiousAvailableCaptionsArr.push({
label: caption_track.name.text || "",
languageCode: caption_track.language_code,
url: `/api/v1/captions/${videoId}?label=${
url: `${config.server.base_path}/api/v1/captions/${videoId}?label=${
encodeURIComponent(caption_track.name.text || "")
}`,
});
Expand Down
5 changes: 3 additions & 2 deletions src/routes/invidious_routes/dashManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ dashManifest.get("/:videoId", async (c) => {
queryParams.set("enc", "true");
queryParams.set("data", encryptedParams);
}
dashUrl = (dashUrl.pathname + "?" +
queryParams.toString()) as unknown as URL;
dashUrl =
(config.server.base_path + dashUrl.pathname + "?" +
queryParams.toString()) as unknown as URL;
return dashUrl;
} else {
return dashUrl;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/invidious_routes/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function getDownloadHandler(app: Hono) {

if ("label" in downloadWidgetData) {
return await app.request(
`/api/v1/captions/${videoId}?label=${
`${config.server.base_path}/api/v1/captions/${videoId}?label=${
encodeURIComponent(downloadWidgetData.label)
}`,
);
Expand All @@ -84,7 +84,7 @@ export default function getDownloadHandler(app: Hono) {
urlQueriesForLatestVersion.set("local", "true");

return await app.request(
`/latest_version?${urlQueriesForLatestVersion.toString()}`,
`${config.server.base_path}/latest_version?${urlQueriesForLatestVersion.toString()}`,
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/routes/invidious_routes/latestVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ latestVersion.get("/", async (c) => {
queryParams.set("enc", "true");
queryParams.set("data", encryptedParams);
}
urlToRedirect = itagUrlParsed.pathname + "?" +
urlToRedirect = config.server.base_path + itagUrlParsed.pathname +
"?" +
queryParams.toString();
console.log(urlToRedirect);
}

if (title) urlToRedirect += `&title=${encodeURIComponent(title)}`;
Expand Down
11 changes: 7 additions & 4 deletions src/tests/main_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Deno.env.set("SERVER_SECRET_KEY", "aaaaaaaaaaaaaaaa");
const { run } = await import("../main.ts");

const { parseConfig } = await import("../lib/helpers/config.ts");
const config = await parseConfig();

import { dashManifest } from "./dashManifest.ts";
import { youtubePlayer } from "./youtubePlayer.ts";
import { latestVersion } from "./latestVersion.ts";
Expand All @@ -9,14 +12,14 @@ Deno.test({
name: "Checking if Invidious companion works",
async fn(t) {
const controller = new AbortController();
const port = 8282;
const baseUrl = `http://localhost:${port.toString()}`;
const baseUrl =
`http://${config.server.host}:${config.server.port.toString()}${config.server.base_path}`;
const headers = { Authorization: "Bearer aaaaaaaaaaaaaaaa" };

await run(
controller.signal,
port,
"localhost",
config.server.port,
config.server.host,
);

await t.step(
Expand Down