Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion api/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions api/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <base href="/tournament/" />
* <a href="dashboard"> 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 `<base href ="/" />` 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(
'<base href="/" />',
`<base href="${BASE_URL}" />`,
)
* ```
*/
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
*
Expand Down
3 changes: 2 additions & 1 deletion api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>
/**
Expand Down Expand Up @@ -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',
Expand Down