diff --git a/api/deno.json b/api/deno.json index 604b180..b93b346 100644 --- a/api/deno.json +++ b/api/deno.json @@ -4,7 +4,7 @@ "@std/http": "jsr:@std/http@^1.0.22" }, "name": "@01edu/api", - "version": "0.1.6", + "version": "0.1.7", "license": "MIT", "exports": { "./context": "./context.ts", diff --git a/api/env.ts b/api/env.ts index 9f78843..4208392 100644 --- a/api/env.ts +++ b/api/env.ts @@ -128,6 +128,31 @@ const forAppEnv = * ``` */ export const PROD: EnvGetter = forAppEnv('prod') + +/** + * + * The root for all relative URLs will be this BASE_URL, so it should start and end with a slash. + * If base URL is not set, it defaults to '/', which means the app is served at the root of the domain. + * If the current application is deployed at https://domain.com/tournament/, with + * will point to https://domain.com/tournament/dashboard + * + * @example + * ```ts + * import { BASE_URL } from '@01edu/api/env'; + * + * // for this example the 'index.html' file's `` tag is dynamically replaced during the build process to adapt to the deployment environment. + * const html = Deno.readTextFileSync(import.meta.dirname + '/dist/index.html') + .replace( + '', + ``, + ) + * ``` + */ +export const BASE_URL: string = PROD('BASE_URL', '/') +if (!BASE_URL.startsWith('/') || !BASE_URL.endsWith('/')) { + throw Error('incorrect BASE_URL: must start and end with /') +} + /** * TEST env getter * diff --git a/api/server.ts b/api/server.ts index 8110e19..78860a8 100644 --- a/api/server.ts +++ b/api/server.ts @@ -40,6 +40,7 @@ import { type RequestContext, runContext } from './context.ts' import { respond, ResponseError } from './response.ts' import { now } from '@01edu/time' import type { Awaitable } from '@01edu/types' +import { BASE_URL } from './env.ts' type Handler = (ctx: RequestContext) => Awaitable /** @@ -99,7 +100,7 @@ export const server = ( setCookie(res.headers, { name: 'trace', value: String(ctx.trace), - path: '/', + path: BASE_URL, secure: true, httpOnly: true, sameSite: 'Lax',