Skip to content

Commit

Permalink
env
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Aug 18, 2023
1 parent 4d2d37e commit abb3e05
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions env.example.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { resolve } from 'node:path'
import { defineBackendConfig, env, safeEnv } from './src/server/env'

/**
* recommend: edit this config with typescript supported editor, you will get immediate type check.
* if you don't have the editor, you can run command `yarn check-backend-config` instead.
*
* you can use env('key') to read from environment variables. It will raise error if env not found.
* you can use env('key') to read from environment variables. It will raise an error if env not found.
*
* you can also use safeEnv('key'). it will not raise any error, but instead returns undefined.
* for a required field this will raise an compiler error.
Expand All @@ -14,18 +15,19 @@ import { defineBackendConfig, env, safeEnv } from './src/server/env'
export default defineBackendConfig({

article: {
location: 'article',
location: resolve('articles'), // must be a resolved path. Put absolute path or use 'resolve()' here.
},

sessionStore: 'redis',
leaderboardSource: 'redis',

redisURL: safeEnv('REDIS_URL') || 'redis://localhost',
redisURL: safeEnv('REDIS_URL') ?? 'redis://localhost',
dsn: env('DB_DSN'),

avatar: {
// must be a resolved path. Put absolute path or use 'resolve()' here.
location: '/path/to/avatars/folder',
domain: '//a.dev.ppy.sb',
domain: 'a.dev.ppy.sb',
},

api: {
Expand Down
4 changes: 3 additions & 1 deletion src/server/backend/$base/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { resolve } from 'node:path'
import { discriminatedUnion, literal, object, string } from 'zod'
import { zodPath } from '~/server/trpc/shapes'
import env from '~~/env'

Check failure on line 4 in src/server/backend/$base/env.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

Cannot find module '~~/env' or its corresponding type declarations.

export const redisURL = string().url()
Expand All @@ -10,7 +12,7 @@ export const validator = discriminatedUnion('sessionStore', [
object({ redisURL, sessionStore: redis }),
]).and(object({
article: object({
location: string().default('articles'),
location: zodPath.default(resolve('articles')),
}),
}))

Expand Down
6 changes: 3 additions & 3 deletions src/server/backend/bancho.py/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { discriminatedUnion, literal, object, string } from 'zod'
import { zodFQDN, zodPath } from '~/server/trpc/shapes'
import { zodFQDN as FQDN, zodPath as path } from '~/server/trpc/shapes'
import { validator as base, redis, redisURL } from '$base/env'
import env from '~~/env'

Check failure on line 4 in src/server/backend/bancho.py/env.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

Cannot find module '~~/env' or its corresponding type declarations.

Expand All @@ -18,8 +18,8 @@ export const leaderboard = discriminatedUnion('leaderboardSource', [
])

export const avatar = object({
location: zodPath,
domain: zodFQDN,
location: path,
domain: FQDN,
})

export const apiEndpoint = object({
Expand Down
2 changes: 1 addition & 1 deletion src/server/trpc/shapes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const zodPath = string().trim().superRefine((val, ctx) => {
if (!existsSync(val)) {
ctx.addIssue({
code: ZodIssueCode.custom,
message: 'invalid path: Guccho cannot access the path you provided',
message: `invalid path: Guccho cannot access the path you provided: ${val}`,
})
}
})

0 comments on commit abb3e05

Please sign in to comment.