create-guren-app 1.15.4 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/api-only/.env.example +3 -25
- package/templates/api-only/.oxlintrc.json +11 -2
- package/templates/api-only/config/env.ts +23 -0
- package/templates/api-only/config/http.ts +15 -0
- package/templates/api-only/package.json +4 -4
- package/templates/api-only/src/app.ts +5 -29
- package/templates/api-only/tests/app.test.ts +7 -12
- package/templates/blog/.guren/routes.gen.ts +10 -0
- package/templates/blog/app/Http/Controllers/PostController.ts +4 -4
- package/templates/blog/src/app.ts +6 -29
- package/templates/blog/tests/PostController.test.ts +3 -4
- package/templates/database/mysql/config/database.ts +14 -2
- package/templates/database/postgres/config/database.ts +14 -2
- package/templates/database/sqlite/config/database.ts +21 -14
- package/templates/default/.env.example +3 -25
- package/templates/default/.oxlintrc.json +11 -2
- package/templates/default/config/env.ts +23 -0
- package/templates/default/config/http.ts +15 -0
- package/templates/default/package.json +4 -4
- package/templates/default/src/app.ts +5 -29
- package/templates/api-only/app/Providers/DatabaseProvider.ts +0 -17
- package/templates/api-only/config/app.ts +0 -42
- package/templates/default/app/Providers/DatabaseProvider.ts +0 -17
- package/templates/default/config/app.ts +0 -38
package/package.json
CHANGED
|
@@ -8,29 +8,7 @@ PORT=3333
|
|
|
8
8
|
HOST=0.0.0.0
|
|
9
9
|
|
|
10
10
|
# bun test automatically sets NODE_ENV=test, which points the app at
|
|
11
|
-
#
|
|
12
|
-
# Override the test DB path itself with TEST_DATABASE_URL if you need to.
|
|
11
|
+
# TEST_DATABASE_URL instead, so this line is only used outside tests.
|
|
13
12
|
DATABASE_URL=./data/guren.db
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
QUEUE_CONNECTION=sync
|
|
18
|
-
# QUEUE_CONNECTION=redis
|
|
19
|
-
|
|
20
|
-
MAIL_MAILER=log
|
|
21
|
-
# MAIL_MAILER=resend
|
|
22
|
-
RESEND_API_KEY=
|
|
23
|
-
MAIL_FROM_ADDRESS=hello@example.com
|
|
24
|
-
MAIL_FROM_NAME="${APP_NAME}"
|
|
25
|
-
|
|
26
|
-
# OAUTH_GITHUB_CLIENT_ID=
|
|
27
|
-
# OAUTH_GITHUB_CLIENT_SECRET=
|
|
28
|
-
# OAUTH_GITHUB_REDIRECT_URI=http://localhost:3333/auth/github/callback
|
|
29
|
-
|
|
30
|
-
# OAUTH_GOOGLE_CLIENT_ID=
|
|
31
|
-
# OAUTH_GOOGLE_CLIENT_SECRET=
|
|
32
|
-
# OAUTH_GOOGLE_REDIRECT_URI=http://localhost:3333/auth/google/callback
|
|
33
|
-
|
|
34
|
-
# OAUTH_DISCORD_CLIENT_ID=
|
|
35
|
-
# OAUTH_DISCORD_CLIENT_SECRET=
|
|
36
|
-
# OAUTH_DISCORD_REDIRECT_URI=http://localhost:3333/auth/discord/callback
|
|
13
|
+
# Blank uses ./data/guren.test.db. Set it to shard parallel CI runs.
|
|
14
|
+
TEST_DATABASE_URL=
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
|
3
3
|
// The guren rules ship with @guren/cli: await-async-assertion (a bare
|
|
4
4
|
// `expect(...).rejects` statement can never fail its test), no-nullish-env-default
|
|
5
|
-
// (`process.env.FOO ?? 'x'` keeps a blank `FOO=` as ''),
|
|
5
|
+
// (`process.env.FOO ?? 'x'` keeps a blank `FOO=` as ''), no-unvalidated-env-read
|
|
6
|
+
// (below), and the comment rules.
|
|
6
7
|
"jsPlugins": ["@guren/cli/oxlint"],
|
|
7
8
|
"ignorePatterns": ["node_modules/**", ".guren/**", "dist/**", "public/build/**"],
|
|
8
9
|
"rules": {
|
|
@@ -16,7 +17,15 @@
|
|
|
16
17
|
"guren/comment-history": "warn",
|
|
17
18
|
"guren/comment-param-restates": "warn",
|
|
18
19
|
"no-unused-vars": ["warn", { "ignoreRestSiblings": true, "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }]
|
|
19
|
-
}
|
|
20
|
+
},
|
|
21
|
+
"overrides": [
|
|
22
|
+
{
|
|
23
|
+
// Application code reads the environment config/env.ts validates, through the
|
|
24
|
+
// `env` a config definition receives. bin/ and drizzle.config.ts run outside the app.
|
|
25
|
+
"files": ["app/**", "config/**", "routes/**", "src/**", "modules/*/**"],
|
|
26
|
+
"rules": { "guren/no-unvalidated-env-read": "error" }
|
|
27
|
+
}
|
|
28
|
+
]
|
|
20
29
|
// Type-aware rules (typescript/no-floating-promises and friends) need
|
|
21
30
|
// `bun add -d oxlint-tsgolint` and `"options": { "typeAware": true }`.
|
|
22
31
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineEnv, Env, type InferEnv } from '@guren/core'
|
|
2
|
+
|
|
3
|
+
const env = defineEnv({
|
|
4
|
+
APP_NAME: Env.string().default('Guren'),
|
|
5
|
+
APP_ENV: Env.string().default('development'),
|
|
6
|
+
APP_KEY: Env.string().secret().requiredInProduction()
|
|
7
|
+
.describe('Signs cookies and encrypts session payloads.'),
|
|
8
|
+
APP_DEBUG: Env.boolean().default(false),
|
|
9
|
+
APP_URL: Env.url().requiredInProduction()
|
|
10
|
+
.describe('Public base URL. Production host authorization answers only to its hostname.'),
|
|
11
|
+
PORT: Env.port().default(3333),
|
|
12
|
+
HOST: Env.string().default('0.0.0.0'),
|
|
13
|
+
// config/database.ts carries the fallbacks, which differ per driver.
|
|
14
|
+
DATABASE_URL: Env.string().optional(),
|
|
15
|
+
TEST_DATABASE_URL: Env.string().optional()
|
|
16
|
+
.describe('Read under NODE_ENV=test by the SQLite driver only.'),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export default env
|
|
20
|
+
|
|
21
|
+
declare module '@guren/core' {
|
|
22
|
+
interface AppEnv extends InferEnv<typeof env> {}
|
|
23
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineHttpConfig } from '@guren/core'
|
|
2
|
+
|
|
3
|
+
const exclude = ['/health']
|
|
4
|
+
|
|
5
|
+
// The Host header is client-controlled, so production answers only to the host
|
|
6
|
+
// this app is deployed as, which APP_URL carries. Emailed links do not depend
|
|
7
|
+
// on this — app/Auth/AppUrl.ts resolves those per request and fails closed there.
|
|
8
|
+
export default defineHttpConfig((env) => ({
|
|
9
|
+
hostAuthorization: process.env.NODE_ENV !== 'production'
|
|
10
|
+
? { allowedHosts: ['localhost:*', '127.0.0.1:*'], exclude }
|
|
11
|
+
// Unreachable in production, where `.requiredInProduction()` failed the boot; it narrows the type.
|
|
12
|
+
: env.APP_URL
|
|
13
|
+
? { allowedHosts: [`${new URL(env.APP_URL).hostname}:*`], exclude }
|
|
14
|
+
: false,
|
|
15
|
+
}))
|
|
@@ -20,13 +20,13 @@
|
|
|
20
20
|
"test": "bun test"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@guren/cli": "^2.
|
|
24
|
-
"@guren/core": "^1.
|
|
25
|
-
"@guren/orm": "^2.
|
|
23
|
+
"@guren/cli": "^2.25.0",
|
|
24
|
+
"@guren/core": "^1.20.0",
|
|
25
|
+
"@guren/orm": "^2.11.0",
|
|
26
26
|
"drizzle-orm": "1.0.0-rc.4"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@guren/testing": "^1.
|
|
29
|
+
"@guren/testing": "^1.12.0",
|
|
30
30
|
"bun-types": "^1.3.1",
|
|
31
31
|
"drizzle-kit": "1.0.0-rc.4",
|
|
32
32
|
"oxlint": "~1.81.0",
|
|
@@ -1,37 +1,13 @@
|
|
|
1
1
|
import { createApp } from '@guren/core'
|
|
2
|
-
import
|
|
2
|
+
import database from '../config/database.js'
|
|
3
|
+
import env from '../config/env.js'
|
|
4
|
+
import http from '../config/http.js'
|
|
3
5
|
import { registerApiRoutes } from '../routes/api.js'
|
|
4
6
|
|
|
5
|
-
// The Host header is client-controlled, so production should answer only to the
|
|
6
|
-
// host this app is deployed as, which APP_URL carries. Emailed links do not
|
|
7
|
-
// depend on this — app/Auth/AppUrl.ts resolves those per request and fails
|
|
8
|
-
// closed there.
|
|
9
|
-
function hostAuthorization() {
|
|
10
|
-
const exclude = ['/health']
|
|
11
|
-
|
|
12
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
13
|
-
return { allowedHosts: ['localhost:*', '127.0.0.1:*'], exclude }
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const appUrl = process.env.APP_URL?.trim()
|
|
17
|
-
// Read at module scope, where not every platform has populated process.env yet
|
|
18
|
-
// (the Cloudflare worker imports this module before wrangler `vars` land), so a
|
|
19
|
-
// missing value warns and leaves the check off rather than throwing and
|
|
20
|
-
// stopping the app from booting at all.
|
|
21
|
-
if (!appUrl) {
|
|
22
|
-
console.warn('[app] APP_URL is not set — host authorization is disabled. Set it to the public base URL of this app.')
|
|
23
|
-
return false
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// `hostname:*` rather than the bare host: the hostname is the security
|
|
27
|
-
// boundary, and a proxy may or may not include the default port in `Host`.
|
|
28
|
-
return { allowedHosts: [`${new URL(appUrl).hostname}:*`], exclude }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
7
|
const app = createApp({
|
|
8
|
+
env,
|
|
9
|
+
config: [database, http],
|
|
32
10
|
routes: registerApiRoutes,
|
|
33
|
-
providers: [DatabaseProvider],
|
|
34
|
-
hostAuthorization: hostAuthorization(),
|
|
35
11
|
})
|
|
36
12
|
|
|
37
13
|
export default app
|
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
import { describe, it } from 'bun:test'
|
|
2
2
|
import { TestApp } from '@guren/testing'
|
|
3
|
-
import
|
|
4
|
-
import { registerApiRoutes } from '../routes/api.js'
|
|
3
|
+
import app from '../src/app.js'
|
|
5
4
|
|
|
5
|
+
// Built from the app entry, so the test exercises the config definitions
|
|
6
|
+
// createApp() wires rather than a second app that never sees them.
|
|
6
7
|
describe('api', () => {
|
|
7
8
|
it('answers the health check', async () => {
|
|
8
|
-
const
|
|
9
|
-
routes: registerApiRoutes,
|
|
10
|
-
providers: [DatabaseProvider],
|
|
11
|
-
})
|
|
9
|
+
const http = await TestApp.fromApp(app)
|
|
12
10
|
|
|
13
|
-
await
|
|
11
|
+
await http.get('/health').assertOk()
|
|
14
12
|
})
|
|
15
13
|
|
|
16
14
|
it('serves the API root', async () => {
|
|
17
|
-
const
|
|
18
|
-
routes: registerApiRoutes,
|
|
19
|
-
providers: [DatabaseProvider],
|
|
20
|
-
})
|
|
15
|
+
const http = await TestApp.fromApp(app)
|
|
21
16
|
|
|
22
|
-
await
|
|
17
|
+
await http.get('/api/v1').assertOk()
|
|
23
18
|
})
|
|
24
19
|
})
|
|
@@ -143,3 +143,13 @@ function appendQueryString(path: string, query?: RouteQuery): string {
|
|
|
143
143
|
const serialized = search.toString()
|
|
144
144
|
return serialized ? `${path}?${serialized}` : path
|
|
145
145
|
}
|
|
146
|
+
|
|
147
|
+
declare module '@guren/core' {
|
|
148
|
+
interface GurenRouteContracts {
|
|
149
|
+
routes: {
|
|
150
|
+
'posts.search': { body: { keywords: string[]; limit: number } }
|
|
151
|
+
'posts.store': { body: { title: string; excerpt: string; body: string } }
|
|
152
|
+
'posts.update': { body: { title: string; excerpt: string; body: string } }
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -3,7 +3,7 @@ import { pages } from '@/.guren/pages.gen'
|
|
|
3
3
|
import { Post } from '../../Models/Post.js'
|
|
4
4
|
import type { UserRecord } from '../../Models/User.js'
|
|
5
5
|
import { PostResource, type PostResourceData } from '../Resources/PostResource.js'
|
|
6
|
-
import { PostIdParamSchema,
|
|
6
|
+
import { PostIdParamSchema, ListPostsQuerySchema } from '../Validators/PostValidator.js'
|
|
7
7
|
|
|
8
8
|
type PostsIndexProps = PaginatedPageProps<PostResourceData>
|
|
9
9
|
|
|
@@ -30,7 +30,7 @@ export default class PostController extends Controller {
|
|
|
30
30
|
// excerpt; `%` and `_` in a keyword act as SQL LIKE wildcards, which this
|
|
31
31
|
// starter treats as a feature.
|
|
32
32
|
async search(): Promise<Response> {
|
|
33
|
-
const { keywords, limit } =
|
|
33
|
+
const { keywords, limit } = this.validated('posts.search').body
|
|
34
34
|
|
|
35
35
|
const posts = await Post.newQuery()
|
|
36
36
|
.with('author')
|
|
@@ -67,7 +67,7 @@ export default class PostController extends Controller {
|
|
|
67
67
|
async store(): Promise<Response> {
|
|
68
68
|
await this.authorize('create', Post)
|
|
69
69
|
const author = await this.auth.userOrFail<UserRecord>()
|
|
70
|
-
const data =
|
|
70
|
+
const { body: data } = this.validated('posts.store')
|
|
71
71
|
// forceCreate, because `authorId` is deliberately absent from Post.fillable:
|
|
72
72
|
// it comes from the session, and a request must never be able to set it.
|
|
73
73
|
const post = await Post.forceCreate({ ...data, authorId: author.id })
|
|
@@ -90,7 +90,7 @@ export default class PostController extends Controller {
|
|
|
90
90
|
const post = await Post.findOrFail(id)
|
|
91
91
|
await this.authorize('update', [Post, post])
|
|
92
92
|
|
|
93
|
-
const data =
|
|
93
|
+
const { body: data } = this.validated('posts.update')
|
|
94
94
|
await Post.update({ id: post.id }, data)
|
|
95
95
|
|
|
96
96
|
return this.redirect(`/posts/${post.id}`)
|
|
@@ -6,37 +6,13 @@
|
|
|
6
6
|
// fallback), so keep .refine()/.transform() free of side effects.
|
|
7
7
|
import 'zod/compile'
|
|
8
8
|
import { createApp } from '@guren/core'
|
|
9
|
-
import DatabaseProvider from '../app/Providers/DatabaseProvider.js'
|
|
10
9
|
import AuthProvider from '../app/Providers/AuthProvider.js'
|
|
11
10
|
import AuthorizationProvider from '../app/Providers/AuthorizationProvider.js'
|
|
11
|
+
import database from '../config/database.js'
|
|
12
|
+
import env from '../config/env.js'
|
|
13
|
+
import http from '../config/http.js'
|
|
12
14
|
import { registerWebRoutes } from '../routes/web.js'
|
|
13
15
|
|
|
14
|
-
// The Host header is client-controlled, so production should answer only to the
|
|
15
|
-
// host this app is deployed as, which APP_URL carries. Emailed links do not
|
|
16
|
-
// depend on this — app/Auth/AppUrl.ts resolves those per request and fails
|
|
17
|
-
// closed there.
|
|
18
|
-
function hostAuthorization() {
|
|
19
|
-
const exclude = ['/health']
|
|
20
|
-
|
|
21
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
22
|
-
return { allowedHosts: ['localhost:*', '127.0.0.1:*'], exclude }
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const appUrl = process.env.APP_URL?.trim()
|
|
26
|
-
// Read at module scope, where not every platform has populated process.env yet
|
|
27
|
-
// (the Cloudflare worker imports this module before wrangler `vars` land), so a
|
|
28
|
-
// missing value warns and leaves the check off rather than throwing and
|
|
29
|
-
// stopping the app from booting at all.
|
|
30
|
-
if (!appUrl) {
|
|
31
|
-
console.warn('[app] APP_URL is not set — host authorization is disabled. Set it to the public base URL of this app.')
|
|
32
|
-
return false
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// `hostname:*` rather than the bare host: the hostname is the security
|
|
36
|
-
// boundary, and a proxy may or may not include the default port in `Host`.
|
|
37
|
-
return { allowedHosts: [`${new URL(appUrl).hostname}:*`], exclude }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
16
|
const app = createApp({
|
|
41
17
|
// Rendered into every server-rendered document. Replace public/favicon.svg
|
|
42
18
|
// with your own artwork, or add more tags here (Open Graph, apple-touch-icon).
|
|
@@ -45,14 +21,15 @@ const app = createApp({
|
|
|
45
21
|
head: '<link rel="icon" type="image/svg+xml" href="/favicon.svg" />',
|
|
46
22
|
},
|
|
47
23
|
},
|
|
24
|
+
env,
|
|
25
|
+
config: [database, http],
|
|
48
26
|
auth: {},
|
|
49
27
|
routes: registerWebRoutes,
|
|
50
|
-
providers: [
|
|
28
|
+
providers: [AuthProvider, AuthorizationProvider],
|
|
51
29
|
// Translations live in lang/<locale>/*.json. Add locales to `supported`
|
|
52
30
|
// and the request locale is detected from ?locale=, a locale cookie, or
|
|
53
31
|
// Accept-Language. `guren codegen` types the keys for t()/useTranslation().
|
|
54
32
|
i18n: { supported: ['en'] },
|
|
55
|
-
hostAuthorization: hostAuthorization(),
|
|
56
33
|
})
|
|
57
34
|
|
|
58
35
|
export default app
|
|
@@ -2,10 +2,9 @@ import { beforeAll, describe, it } from 'bun:test'
|
|
|
2
2
|
import { TestApp } from '@guren/testing'
|
|
3
3
|
import app from '../src/app.js'
|
|
4
4
|
|
|
5
|
-
// posts.search is an HTTP QUERY route (RFC 10008).
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
// controller so the request stream is read once.
|
|
5
|
+
// posts.search is an HTTP QUERY route (RFC 10008). The body schema bound to the
|
|
6
|
+
// route is validated before the controller runs, and the same schema feeds
|
|
7
|
+
// codegen and `guren audit`.
|
|
9
8
|
|
|
10
9
|
// So an invalid payload gets a 422 without the database ever being touched,
|
|
11
10
|
// which keeps this starter test green without migrations or fixtures. Cover the
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import { createMySqlDatabase, type MySqlSeederContext } from '@guren/
|
|
1
|
+
import { createMySqlDatabase, defineDatabaseConfig, type MySqlSeederContext } from '@guren/core'
|
|
2
|
+
import env from './env.js'
|
|
2
3
|
|
|
3
4
|
const database = createMySqlDatabase({
|
|
4
5
|
migrationsFolder: new URL('../db/migrations', import.meta.url),
|
|
5
6
|
seedersFolder: new URL('../db/seeders', import.meta.url),
|
|
6
|
-
|
|
7
|
+
// `context` is the application's validated environment. `guren db:*` runs this
|
|
8
|
+
// outside one, where report mode reads the schema without requiring the keys
|
|
9
|
+
// only the web process needs (APP_KEY and APP_URL in production).
|
|
10
|
+
connectionString: (context) => (context?.env ?? env.parse(undefined, { mode: 'report' }).values).DATABASE_URL
|
|
11
|
+
?? 'mysql://guren:guren@localhost:33306/guren',
|
|
7
12
|
})
|
|
8
13
|
|
|
9
14
|
export const { getDatabase, migrateDatabase, closeDatabase, configureOrm, seedDatabase, resetDatabase, migrationStatus } = database
|
|
10
15
|
|
|
11
16
|
/** Annotate seeders with this: `defineSeeder(async ({ db }: AppSeederContext) => {})`. */
|
|
12
17
|
export type AppSeederContext = MySqlSeederContext
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Seeding is one-shot provisioning, not part of booting: production boots
|
|
21
|
+
* repeatedly on serverless cold starts, and a bundle has no resolver for the
|
|
22
|
+
* raw db/seeders/*.ts files. Run `bunx guren db:seed` explicitly instead.
|
|
23
|
+
*/
|
|
24
|
+
export default defineDatabaseConfig(database, { seedOnBoot: process.env.NODE_ENV !== 'production' })
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import { createPostgresDatabase, type PostgresSeederContext } from '@guren/
|
|
1
|
+
import { createPostgresDatabase, defineDatabaseConfig, type PostgresSeederContext } from '@guren/core'
|
|
2
|
+
import env from './env.js'
|
|
2
3
|
|
|
3
4
|
const database = createPostgresDatabase({
|
|
4
5
|
migrationsFolder: new URL('../db/migrations', import.meta.url),
|
|
5
6
|
seedersFolder: new URL('../db/seeders', import.meta.url),
|
|
6
|
-
|
|
7
|
+
// `context` is the application's validated environment. `guren db:*` runs this
|
|
8
|
+
// outside one, where report mode reads the schema without requiring the keys
|
|
9
|
+
// only the web process needs (APP_KEY and APP_URL in production).
|
|
10
|
+
connectionString: (context) => (context?.env ?? env.parse(undefined, { mode: 'report' }).values).DATABASE_URL
|
|
11
|
+
?? 'postgres://guren:guren@localhost:54322/guren',
|
|
7
12
|
})
|
|
8
13
|
|
|
9
14
|
export const { getDatabase, migrateDatabase, closeDatabase, configureOrm, seedDatabase, resetDatabase, migrationStatus } = database
|
|
10
15
|
|
|
11
16
|
/** Annotate seeders with this: `defineSeeder(async ({ db }: AppSeederContext) => {})`. */
|
|
12
17
|
export type AppSeederContext = PostgresSeederContext
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Seeding is one-shot provisioning, not part of booting: production boots
|
|
21
|
+
* repeatedly on serverless cold starts, and a bundle has no resolver for the
|
|
22
|
+
* raw db/seeders/*.ts files. Run `bunx guren db:seed` explicitly instead.
|
|
23
|
+
*/
|
|
24
|
+
export default defineDatabaseConfig(database, { seedOnBoot: process.env.NODE_ENV !== 'production' })
|
|
@@ -1,24 +1,31 @@
|
|
|
1
|
-
import { createSqliteDatabase, type SqliteSeederContext } from '@guren/
|
|
2
|
-
|
|
3
|
-
// `bun test` sets NODE_ENV=test automatically, so the test suite reads and
|
|
4
|
-
// writes a separate SQLite file and never touches the development database —
|
|
5
|
-
// this takes priority over DATABASE_URL, which .env sets unconditionally.
|
|
6
|
-
// Override the test DB path itself with TEST_DATABASE_URL if needed (e.g. to
|
|
7
|
-
// shard parallel CI runs); DATABASE_URL is still authoritative outside tests.
|
|
8
|
-
function resolveDatabaseFilename(): string {
|
|
9
|
-
if (process.env.NODE_ENV === 'test') {
|
|
10
|
-
return process.env.TEST_DATABASE_URL || './data/guren.test.db'
|
|
11
|
-
}
|
|
12
|
-
return process.env.DATABASE_URL || './data/guren.db'
|
|
13
|
-
}
|
|
1
|
+
import { createSqliteDatabase, defineDatabaseConfig, type SqliteSeederContext } from '@guren/core'
|
|
2
|
+
import env from './env.js'
|
|
14
3
|
|
|
15
4
|
const database = createSqliteDatabase({
|
|
16
5
|
migrationsFolder: new URL('../db/migrations', import.meta.url),
|
|
17
6
|
seedersFolder: new URL('../db/seeders', import.meta.url),
|
|
18
|
-
|
|
7
|
+
// `context` is the application's validated environment. `guren db:*` runs this
|
|
8
|
+
// outside one, where report mode reads the schema without requiring the keys
|
|
9
|
+
// only the web process needs (APP_KEY and APP_URL in production).
|
|
10
|
+
filename: (context) => {
|
|
11
|
+
const values = context?.env ?? env.parse(undefined, { mode: 'report' }).values
|
|
12
|
+
// `bun test` sets NODE_ENV=test automatically, so the test suite reads and
|
|
13
|
+
// writes a separate SQLite file and never touches the development database.
|
|
14
|
+
// This takes priority over DATABASE_URL, which .env sets unconditionally.
|
|
15
|
+
return process.env.NODE_ENV === 'test'
|
|
16
|
+
? values.TEST_DATABASE_URL ?? './data/guren.test.db'
|
|
17
|
+
: values.DATABASE_URL ?? './data/guren.db'
|
|
18
|
+
},
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
export const { getDatabase, migrateDatabase, closeDatabase, configureOrm, seedDatabase, resetDatabase, migrationStatus } = database
|
|
22
22
|
|
|
23
23
|
/** Annotate seeders with this: `defineSeeder(async ({ db }: AppSeederContext) => {})`. */
|
|
24
24
|
export type AppSeederContext = SqliteSeederContext
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Seeding is one-shot provisioning, not part of booting: production boots
|
|
28
|
+
* repeatedly on serverless cold starts, and a bundle has no resolver for the
|
|
29
|
+
* raw db/seeders/*.ts files. Run `bunx guren db:seed` explicitly instead.
|
|
30
|
+
*/
|
|
31
|
+
export default defineDatabaseConfig(database, { seedOnBoot: process.env.NODE_ENV !== 'production' })
|
|
@@ -8,32 +8,10 @@ PORT=3333
|
|
|
8
8
|
HOST=0.0.0.0
|
|
9
9
|
|
|
10
10
|
# bun test automatically sets NODE_ENV=test, which points the app at
|
|
11
|
-
#
|
|
12
|
-
# Override the test DB path itself with TEST_DATABASE_URL if you need to.
|
|
11
|
+
# TEST_DATABASE_URL instead, so this line is only used outside tests.
|
|
13
12
|
DATABASE_URL=./data/guren.db
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
QUEUE_CONNECTION=sync
|
|
18
|
-
# QUEUE_CONNECTION=redis
|
|
19
|
-
|
|
20
|
-
MAIL_MAILER=log
|
|
21
|
-
# MAIL_MAILER=resend
|
|
22
|
-
RESEND_API_KEY=
|
|
23
|
-
MAIL_FROM_ADDRESS=hello@example.com
|
|
24
|
-
MAIL_FROM_NAME="${APP_NAME}"
|
|
25
|
-
|
|
26
|
-
# OAUTH_GITHUB_CLIENT_ID=
|
|
27
|
-
# OAUTH_GITHUB_CLIENT_SECRET=
|
|
28
|
-
# OAUTH_GITHUB_REDIRECT_URI=http://localhost:3333/auth/github/callback
|
|
29
|
-
|
|
30
|
-
# OAUTH_GOOGLE_CLIENT_ID=
|
|
31
|
-
# OAUTH_GOOGLE_CLIENT_SECRET=
|
|
32
|
-
# OAUTH_GOOGLE_REDIRECT_URI=http://localhost:3333/auth/google/callback
|
|
33
|
-
|
|
34
|
-
# OAUTH_DISCORD_CLIENT_ID=
|
|
35
|
-
# OAUTH_DISCORD_CLIENT_SECRET=
|
|
36
|
-
# OAUTH_DISCORD_REDIRECT_URI=http://localhost:3333/auth/discord/callback
|
|
13
|
+
# Blank uses ./data/guren.test.db. Set it to shard parallel CI runs.
|
|
14
|
+
TEST_DATABASE_URL=
|
|
37
15
|
|
|
38
16
|
# Optional: point to an already-running external Vite dev server.
|
|
39
17
|
# VITE_DEV_SERVER_URL=http://localhost:5173
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
|
3
3
|
// The guren rules ship with @guren/cli: await-async-assertion (a bare
|
|
4
4
|
// `expect(...).rejects` statement can never fail its test), no-nullish-env-default
|
|
5
|
-
// (`process.env.FOO ?? 'x'` keeps a blank `FOO=` as ''),
|
|
5
|
+
// (`process.env.FOO ?? 'x'` keeps a blank `FOO=` as ''), no-unvalidated-env-read
|
|
6
|
+
// (below), and the comment rules.
|
|
6
7
|
"jsPlugins": ["@guren/cli/oxlint"],
|
|
7
8
|
"ignorePatterns": ["node_modules/**", ".guren/**", "dist/**", "public/build/**"],
|
|
8
9
|
"rules": {
|
|
@@ -16,7 +17,15 @@
|
|
|
16
17
|
"guren/comment-history": "warn",
|
|
17
18
|
"guren/comment-param-restates": "warn",
|
|
18
19
|
"no-unused-vars": ["warn", { "ignoreRestSiblings": true, "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }]
|
|
19
|
-
}
|
|
20
|
+
},
|
|
21
|
+
"overrides": [
|
|
22
|
+
{
|
|
23
|
+
// Application code reads the environment config/env.ts validates, through the
|
|
24
|
+
// `env` a config definition receives. bin/ and drizzle.config.ts run outside the app.
|
|
25
|
+
"files": ["app/**", "config/**", "routes/**", "src/**", "modules/*/**"],
|
|
26
|
+
"rules": { "guren/no-unvalidated-env-read": "error" }
|
|
27
|
+
}
|
|
28
|
+
]
|
|
20
29
|
// Type-aware rules (typescript/no-floating-promises and friends) need
|
|
21
30
|
// `bun add -d oxlint-tsgolint` and `"options": { "typeAware": true }`.
|
|
22
31
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineEnv, Env, type InferEnv } from '@guren/core'
|
|
2
|
+
|
|
3
|
+
const env = defineEnv({
|
|
4
|
+
APP_NAME: Env.string().default('Guren'),
|
|
5
|
+
APP_ENV: Env.string().default('development'),
|
|
6
|
+
APP_KEY: Env.string().secret().requiredInProduction()
|
|
7
|
+
.describe('Signs cookies and encrypts session payloads.'),
|
|
8
|
+
APP_DEBUG: Env.boolean().default(false),
|
|
9
|
+
APP_URL: Env.url().requiredInProduction()
|
|
10
|
+
.describe('Public base URL. Production host authorization answers only to its hostname.'),
|
|
11
|
+
PORT: Env.port().default(3333),
|
|
12
|
+
HOST: Env.string().default('0.0.0.0'),
|
|
13
|
+
// config/database.ts carries the fallbacks, which differ per driver.
|
|
14
|
+
DATABASE_URL: Env.string().optional(),
|
|
15
|
+
TEST_DATABASE_URL: Env.string().optional()
|
|
16
|
+
.describe('Read under NODE_ENV=test by the SQLite driver only.'),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export default env
|
|
20
|
+
|
|
21
|
+
declare module '@guren/core' {
|
|
22
|
+
interface AppEnv extends InferEnv<typeof env> {}
|
|
23
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineHttpConfig } from '@guren/core'
|
|
2
|
+
|
|
3
|
+
const exclude = ['/health']
|
|
4
|
+
|
|
5
|
+
// The Host header is client-controlled, so production answers only to the host
|
|
6
|
+
// this app is deployed as, which APP_URL carries. Emailed links do not depend
|
|
7
|
+
// on this — app/Auth/AppUrl.ts resolves those per request and fails closed there.
|
|
8
|
+
export default defineHttpConfig((env) => ({
|
|
9
|
+
hostAuthorization: process.env.NODE_ENV !== 'production'
|
|
10
|
+
? { allowedHosts: ['localhost:*', '127.0.0.1:*'], exclude }
|
|
11
|
+
// Unreachable in production, where `.requiredInProduction()` failed the boot; it narrows the type.
|
|
12
|
+
: env.APP_URL
|
|
13
|
+
? { allowedHosts: [`${new URL(env.APP_URL).hostname}:*`], exclude }
|
|
14
|
+
: false,
|
|
15
|
+
}))
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"db:seed": "bunx guren db:seed"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@guren/cli": "^2.
|
|
25
|
-
"@guren/core": "^1.
|
|
24
|
+
"@guren/cli": "^2.25.0",
|
|
25
|
+
"@guren/core": "^1.20.0",
|
|
26
26
|
"@guren/inertia-client": "^1.2.1",
|
|
27
|
-
"@guren/orm": "^2.
|
|
27
|
+
"@guren/orm": "^2.11.0",
|
|
28
28
|
"@inertiajs/react": "^3.6.1",
|
|
29
29
|
"drizzle-orm": "1.0.0-rc.4",
|
|
30
30
|
"react": "^19.0.0",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"zod": "^4.5.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@guren/testing": "^1.
|
|
35
|
+
"@guren/testing": "^1.12.0",
|
|
36
36
|
"@tailwindcss/vite": "^4.2.2",
|
|
37
37
|
"@types/react": "^19.0.0",
|
|
38
38
|
"@types/react-dom": "^19.0.0",
|
|
@@ -6,35 +6,11 @@
|
|
|
6
6
|
// fallback), so keep .refine()/.transform() free of side effects.
|
|
7
7
|
import 'zod/compile'
|
|
8
8
|
import { createApp } from '@guren/core'
|
|
9
|
-
import
|
|
9
|
+
import database from '../config/database.js'
|
|
10
|
+
import env from '../config/env.js'
|
|
11
|
+
import http from '../config/http.js'
|
|
10
12
|
import { registerWebRoutes } from '../routes/web.js'
|
|
11
13
|
|
|
12
|
-
// The Host header is client-controlled, so production should answer only to the
|
|
13
|
-
// host this app is deployed as, which APP_URL carries. Emailed links do not
|
|
14
|
-
// depend on this — app/Auth/AppUrl.ts resolves those per request and fails
|
|
15
|
-
// closed there.
|
|
16
|
-
function hostAuthorization() {
|
|
17
|
-
const exclude = ['/health']
|
|
18
|
-
|
|
19
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
20
|
-
return { allowedHosts: ['localhost:*', '127.0.0.1:*'], exclude }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const appUrl = process.env.APP_URL?.trim()
|
|
24
|
-
// Read at module scope, where not every platform has populated process.env yet
|
|
25
|
-
// (the Cloudflare worker imports this module before wrangler `vars` land), so a
|
|
26
|
-
// missing value warns and leaves the check off rather than throwing and
|
|
27
|
-
// stopping the app from booting at all.
|
|
28
|
-
if (!appUrl) {
|
|
29
|
-
console.warn('[app] APP_URL is not set — host authorization is disabled. Set it to the public base URL of this app.')
|
|
30
|
-
return false
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// `hostname:*` rather than the bare host: the hostname is the security
|
|
34
|
-
// boundary, and a proxy may or may not include the default port in `Host`.
|
|
35
|
-
return { allowedHosts: [`${new URL(appUrl).hostname}:*`], exclude }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
14
|
const app = createApp({
|
|
39
15
|
// Rendered into every server-rendered document. Replace public/favicon.svg
|
|
40
16
|
// with your own artwork, or add more tags here (Open Graph, apple-touch-icon).
|
|
@@ -43,13 +19,13 @@ const app = createApp({
|
|
|
43
19
|
head: '<link rel="icon" type="image/svg+xml" href="/favicon.svg" />',
|
|
44
20
|
},
|
|
45
21
|
},
|
|
22
|
+
env,
|
|
23
|
+
config: [database, http],
|
|
46
24
|
routes: registerWebRoutes,
|
|
47
|
-
providers: [DatabaseProvider],
|
|
48
25
|
// Translations live in lang/<locale>/*.json. Add locales to `supported`
|
|
49
26
|
// and the request locale is detected from ?locale=, a locale cookie, or
|
|
50
27
|
// Accept-Language. `guren codegen` types the keys for t()/useTranslation().
|
|
51
28
|
i18n: { supported: ['en'] },
|
|
52
|
-
hostAuthorization: hostAuthorization(),
|
|
53
29
|
})
|
|
54
30
|
|
|
55
31
|
export default app
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ServiceProvider } from '@guren/core'
|
|
2
|
-
import { bootModels } from '../../config/app.js'
|
|
3
|
-
|
|
4
|
-
export default class DatabaseProvider extends ServiceProvider {
|
|
5
|
-
private initialized = false
|
|
6
|
-
|
|
7
|
-
register(): void {}
|
|
8
|
-
|
|
9
|
-
async boot(): Promise<void> {
|
|
10
|
-
if (this.initialized) {
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
await bootModels()
|
|
15
|
-
this.initialized = true
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { existsSync, readdirSync } from 'node:fs'
|
|
2
|
-
import { fileURLToPath } from 'node:url'
|
|
3
|
-
import { resolve } from 'node:path'
|
|
4
|
-
import { configureOrm, seedDatabase } from './database.js'
|
|
5
|
-
|
|
6
|
-
let bootstrapped = false
|
|
7
|
-
const MIGRATIONS_FOLDER = fileURLToPath(new URL('../db/migrations', import.meta.url))
|
|
8
|
-
|
|
9
|
-
function hasMigrations(): boolean {
|
|
10
|
-
if (!existsSync(MIGRATIONS_FOLDER)) {
|
|
11
|
-
return false
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const entries = readdirSync(MIGRATIONS_FOLDER, { withFileTypes: true })
|
|
15
|
-
return entries.some(
|
|
16
|
-
(entry) => entry.isDirectory() && existsSync(resolve(MIGRATIONS_FOLDER, entry.name, 'migration.sql')),
|
|
17
|
-
)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Seeding is one-shot provisioning, not part of booting: production boots
|
|
22
|
-
* repeatedly on serverless cold starts, and a bundle has no resolver for the
|
|
23
|
-
* raw db/seeders/*.ts files. Run `bunx guren db:seed` explicitly instead.
|
|
24
|
-
*/
|
|
25
|
-
function shouldSeedOnBoot(): boolean {
|
|
26
|
-
return process.env.NODE_ENV !== 'production'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function bootModels(): Promise<void> {
|
|
30
|
-
if (bootstrapped) return
|
|
31
|
-
|
|
32
|
-
if (!hasMigrations()) {
|
|
33
|
-
bootstrapped = true
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
await configureOrm()
|
|
38
|
-
if (shouldSeedOnBoot()) {
|
|
39
|
-
await seedDatabase()
|
|
40
|
-
}
|
|
41
|
-
bootstrapped = true
|
|
42
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ServiceProvider } from '@guren/core'
|
|
2
|
-
import { bootModels } from '../../config/app.js'
|
|
3
|
-
|
|
4
|
-
export default class DatabaseProvider extends ServiceProvider {
|
|
5
|
-
private initialized = false
|
|
6
|
-
|
|
7
|
-
register(): void {}
|
|
8
|
-
|
|
9
|
-
async boot(): Promise<void> {
|
|
10
|
-
if (this.initialized) {
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
await bootModels()
|
|
15
|
-
this.initialized = true
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { existsSync, readdirSync } from 'node:fs'
|
|
2
|
-
import { fileURLToPath } from 'node:url'
|
|
3
|
-
import { resolve } from 'node:path'
|
|
4
|
-
import { configureOrm, seedDatabase } from './database.js'
|
|
5
|
-
|
|
6
|
-
let bootstrapped = false
|
|
7
|
-
const MIGRATIONS_FOLDER = fileURLToPath(new URL('../db/migrations', import.meta.url))
|
|
8
|
-
|
|
9
|
-
function hasMigrations(): boolean {
|
|
10
|
-
if (!existsSync(MIGRATIONS_FOLDER)) {
|
|
11
|
-
return false
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const entries = readdirSync(MIGRATIONS_FOLDER, { withFileTypes: true })
|
|
15
|
-
return entries.some(
|
|
16
|
-
(entry) => entry.isDirectory() && existsSync(resolve(MIGRATIONS_FOLDER, entry.name, 'migration.sql')),
|
|
17
|
-
)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Seeding is one-shot provisioning, not part of booting: production boots
|
|
22
|
-
* repeatedly on serverless cold starts, and a bundle has no resolver for the
|
|
23
|
-
* raw db/seeders/*.ts files. Run `bunx guren db:seed` explicitly instead.
|
|
24
|
-
*/
|
|
25
|
-
function shouldSeedOnBoot(): boolean {
|
|
26
|
-
return process.env.NODE_ENV !== 'production'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function bootModels(): Promise<void> {
|
|
30
|
-
if (bootstrapped) return
|
|
31
|
-
|
|
32
|
-
// Always initialize ORM (even without migrations, models need a connection).
|
|
33
|
-
await configureOrm()
|
|
34
|
-
if (shouldSeedOnBoot() && hasMigrations()) {
|
|
35
|
-
await seedDatabase()
|
|
36
|
-
}
|
|
37
|
-
bootstrapped = true
|
|
38
|
-
}
|