create-guren-app 1.17.5 → 1.18.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 +0 -2
- package/templates/api-only/README.md +2 -0
- package/templates/api-only/_gitignore +2 -0
- package/templates/api-only/bin/serve.ts +24 -0
- package/templates/api-only/config/env.ts +1 -3
- package/templates/api-only/package.json +4 -3
- package/templates/blog/.guren/translations.gen.ts +27 -0
- package/templates/blog/README.md +1 -0
- package/templates/blog/app/Http/Middleware/AuthThrottle.ts +90 -0
- package/templates/blog/lang/en/auth.json +4 -0
- package/templates/blog/routes/auth.ts +3 -2
- package/templates/default/.env.example +0 -2
- package/templates/default/README.md +1 -0
- package/templates/default/_gitignore +2 -0
- package/templates/default/bin/serve.ts +24 -0
- package/templates/default/config/env.ts +1 -3
- package/templates/default/package.json +4 -3
- package/templates/default-ssr/README.md +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-guren-app",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.18.0",
|
|
4
4
|
"description": "Scaffold a Guren app: a Bun-first fullstack TypeScript framework with Laravel-style conventions, Inertia.js + React, Drizzle ORM, and an agent harness for Claude Code and other coding agents.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"guren",
|
|
@@ -7,6 +7,30 @@ try {
|
|
|
7
7
|
process.exit(1)
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
// HSTS, the Secure session cookie, hidden error details and the APP_URL host
|
|
11
|
+
// allowlist all key on NODE_ENV=production, which `bun bin/serve.ts` alone never sets.
|
|
12
|
+
function deployedHost(appUrl: string | undefined): string | undefined {
|
|
13
|
+
if (!appUrl) return undefined
|
|
14
|
+
let host: string
|
|
15
|
+
try {
|
|
16
|
+
host = new URL(appUrl).hostname
|
|
17
|
+
} catch {
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
const loopback = host === 'localhost' || host.endsWith('.localhost') || host.startsWith('127.')
|
|
21
|
+
|| host === '[::1]' || host === '0.0.0.0'
|
|
22
|
+
return loopback ? undefined : host
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const nodeEnv = process.env.NODE_ENV
|
|
26
|
+
const appHost = deployedHost(process.env.APP_URL)
|
|
27
|
+
if (appHost && nodeEnv !== 'production' && nodeEnv !== 'test') {
|
|
28
|
+
console.warn(
|
|
29
|
+
`APP_URL points at ${appHost} but NODE_ENV is ${nodeEnv ? `"${nodeEnv}"` : 'unset'}, so the production `
|
|
30
|
+
+ 'security defaults are off. Start a deployed app with `bun run start` or set NODE_ENV=production.',
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
10
34
|
// `PORT=0` means "any free port", so this tests for a number, not truthiness.
|
|
11
35
|
const parsedPort = Number.parseInt(process.env.PORT ?? '', 10)
|
|
12
36
|
const port = Number.isInteger(parsedPort) ? parsedPort : 3333
|
|
@@ -2,10 +2,8 @@ import { defineEnv, Env, type InferEnv } from '@guren/core'
|
|
|
2
2
|
|
|
3
3
|
const env = defineEnv({
|
|
4
4
|
APP_NAME: Env.string().default('Guren'),
|
|
5
|
-
APP_ENV: Env.string().default('development'),
|
|
6
5
|
APP_KEY: Env.string().secret().requiredInProduction()
|
|
7
|
-
.describe('
|
|
8
|
-
APP_DEBUG: Env.boolean().default(false),
|
|
6
|
+
.describe('Root secret for signing and encryption; each use derives its own key.'),
|
|
9
7
|
APP_URL: Env.url().requiredInProduction()
|
|
10
8
|
.describe('Public base URL. Production host authorization answers only to its hostname.'),
|
|
11
9
|
PORT: Env.port().default(3333),
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "bun run codegen && GUREN_MCP=1 GUREN_DOCS=1 bun run dev:server",
|
|
8
8
|
"dev:server": "bun --hot bin/serve.ts",
|
|
9
|
+
"start": "NODE_ENV=production bun bin/serve.ts",
|
|
9
10
|
"console": "bun bin/console.ts",
|
|
10
11
|
"codegen": "bunx guren codegen --routes routes/api.ts --force",
|
|
11
12
|
"typecheck": "tsc --noEmit",
|
|
@@ -20,9 +21,9 @@
|
|
|
20
21
|
"test": "bun test"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@guren/cli": "^2.
|
|
24
|
-
"@guren/core": "^1.
|
|
25
|
-
"@guren/orm": "^2.13.
|
|
24
|
+
"@guren/cli": "^2.29.0",
|
|
25
|
+
"@guren/core": "^1.24.0",
|
|
26
|
+
"@guren/orm": "^2.13.1",
|
|
26
27
|
"drizzle-orm": "1.0.0-rc.4"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Generated from lang/ (locales: en) — DO NOT EDIT
|
|
2
|
+
// Run `guren codegen` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Every translation key present in at least one locale. Missing-per-locale
|
|
6
|
+
* keys are reported by `guren check --i18n`, not here.
|
|
7
|
+
*/
|
|
8
|
+
export type TranslationKey =
|
|
9
|
+
| 'auth.throttle'
|
|
10
|
+
| 'auth.too_many_requests'
|
|
11
|
+
| 'messages.welcome'
|
|
12
|
+
|
|
13
|
+
// Type this app's translation helpers via declaration merging:
|
|
14
|
+
// `this.t()` / `this.tc()` in controllers and `useTranslation()` in pages
|
|
15
|
+
// autocomplete and reject unknown keys. Delete lang/ and regenerate to
|
|
16
|
+
// return them to plain strings.
|
|
17
|
+
declare module '@guren/core' {
|
|
18
|
+
interface GurenTranslationKeys {
|
|
19
|
+
keys: TranslationKey
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare module '@guren/inertia-client' {
|
|
24
|
+
interface GurenTranslationKeys {
|
|
25
|
+
keys: TranslationKey
|
|
26
|
+
}
|
|
27
|
+
}
|
package/templates/blog/README.md
CHANGED
|
@@ -88,6 +88,7 @@ deployments (AWS Lambda, for example) serve them from a CDN instead.
|
|
|
88
88
|
| `bun run dev` | Start dev server (Bun + Vite) |
|
|
89
89
|
| `bun run build` | Build for production |
|
|
90
90
|
| `bun run preview` | Start the built app with the Bun production server |
|
|
91
|
+
| `bun run start` | Start the app in production mode (`NODE_ENV=production`); what a deployment runs |
|
|
91
92
|
| `bun run db:make` | Generate a migration from `db/schema.ts` |
|
|
92
93
|
| `bun run db:migrate` | Run database migrations |
|
|
93
94
|
| `bun run db:seed` | Seed the database |
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createRateLimitMiddleware, getRequestTranslator, ValidationException } from '@guren/core'
|
|
2
|
+
import type { Context, Middleware } from '@guren/core'
|
|
3
|
+
|
|
4
|
+
interface ThrottleOptions {
|
|
5
|
+
/** Requests allowed per client and email address within the window. */
|
|
6
|
+
limit: number
|
|
7
|
+
windowMs: number
|
|
8
|
+
/** Translation key; `:seconds` is replaced with the wait. */
|
|
9
|
+
messageKey: string
|
|
10
|
+
fallbackMessage: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface BunServerEnv {
|
|
14
|
+
server?: { requestIP?(request: Request): { address: string } | null }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Behind a proxy or CDN every client shares its address: read the header your
|
|
18
|
+
// proxy sets (and strips from client requests) here instead.
|
|
19
|
+
function clientIp(ctx: Context): string | undefined {
|
|
20
|
+
return (ctx.env as BunServerEnv | undefined)?.server?.requestIP?.(ctx.req.raw)?.address
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Hono caches the parsed body, so the controller still reads it after this.
|
|
24
|
+
async function submittedEmail(ctx: Context): Promise<string | undefined> {
|
|
25
|
+
try {
|
|
26
|
+
const body: unknown = (ctx.req.header('content-type') ?? '').includes('application/json')
|
|
27
|
+
? await ctx.req.json()
|
|
28
|
+
: await ctx.req.parseBody()
|
|
29
|
+
const email = (body as Record<string, unknown> | null)?.email
|
|
30
|
+
return typeof email === 'string' ? email.trim().toLowerCase() : undefined
|
|
31
|
+
} catch {
|
|
32
|
+
return undefined
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function throttleMessage(ctx: Context, options: ThrottleOptions, seconds: number): string {
|
|
37
|
+
const translated = getRequestTranslator(ctx)?.t(options.messageKey, { seconds })
|
|
38
|
+
return translated && translated !== options.messageKey
|
|
39
|
+
? translated
|
|
40
|
+
: options.fallbackMessage.replace(':seconds', String(seconds))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Counts every request, keyed per client IP and submitted email. The default
|
|
45
|
+
* store lives in process memory: pass `store: new RedisRateLimitStore(redis)`
|
|
46
|
+
* (from `@guren/core/redis`) to share counts across instances.
|
|
47
|
+
*/
|
|
48
|
+
function authThrottle(name: string, options: ThrottleOptions): Middleware {
|
|
49
|
+
// Built on the first request: the default store starts a sweep timer, which
|
|
50
|
+
// Cloudflare Workers refuses while a module is being evaluated.
|
|
51
|
+
let limiter: Middleware | undefined
|
|
52
|
+
return (ctx, next) => {
|
|
53
|
+
limiter ??= createRateLimitMiddleware({
|
|
54
|
+
limit: options.limit,
|
|
55
|
+
windowMs: options.windowMs,
|
|
56
|
+
keyPrefix: `auth:${name}:`,
|
|
57
|
+
keyGenerator: async (ctx) => `${clientIp(ctx) ?? 'unknown'}|${(await submittedEmail(ctx)) ?? ''}`,
|
|
58
|
+
onRateLimited: (ctx, retryAfter) => {
|
|
59
|
+
const message = throttleMessage(ctx, options, retryAfter)
|
|
60
|
+
// An Inertia form shows a flashed error; a bare 429 would open its error modal.
|
|
61
|
+
if (ctx.req.header('x-inertia') === 'true') {
|
|
62
|
+
throw ValidationException.withMessages({ message })
|
|
63
|
+
}
|
|
64
|
+
return ctx.json({ message, retryAfter }, 429)
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
return limiter(ctx, next)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const throttleLogin = authThrottle('login', {
|
|
72
|
+
limit: 5,
|
|
73
|
+
windowMs: 60_000,
|
|
74
|
+
messageKey: 'auth.throttle',
|
|
75
|
+
fallbackMessage: 'Too many login attempts. Please try again in :seconds seconds.',
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
export const throttleRegistration = authThrottle('register', {
|
|
79
|
+
limit: 5,
|
|
80
|
+
windowMs: 60_000,
|
|
81
|
+
messageKey: 'auth.too_many_requests',
|
|
82
|
+
fallbackMessage: 'Too many requests. Please try again in :seconds seconds.',
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
export const throttlePasswordResetRequest = authThrottle('forgot-password', {
|
|
86
|
+
limit: 3,
|
|
87
|
+
windowMs: 15 * 60_000,
|
|
88
|
+
messageKey: 'auth.too_many_requests',
|
|
89
|
+
fallbackMessage: 'Too many requests. Please try again in :seconds seconds.',
|
|
90
|
+
})
|
|
@@ -3,14 +3,15 @@ import LoginController from '../app/Http/Controllers/Auth/LoginController.js'
|
|
|
3
3
|
import RegisterController from '../app/Http/Controllers/Auth/RegisterController.js'
|
|
4
4
|
import DashboardController from '../app/Http/Controllers/DashboardController.js'
|
|
5
5
|
import ProfileController from '../app/Http/Controllers/ProfileController.js'
|
|
6
|
+
import { throttleLogin, throttleRegistration } from '../app/Http/Middleware/AuthThrottle.js'
|
|
6
7
|
|
|
7
8
|
// Uses the 'auth' and 'guest' aliases routes/web.ts registers on this router.
|
|
8
9
|
export function registerAuthRoutes(router: Router<'auth' | 'guest'>): void {
|
|
9
10
|
router.middleware('guest').group((guest) => {
|
|
10
11
|
guest.get('/login', [LoginController, 'show']).name('login')
|
|
11
|
-
guest.post('/login', [LoginController, 'store']).name('login.store')
|
|
12
|
+
guest.post('/login', [LoginController, 'store'], throttleLogin).name('login.store')
|
|
12
13
|
guest.get('/register', [RegisterController, 'show']).name('register')
|
|
13
|
-
guest.post('/register', [RegisterController, 'store']).name('register.store')
|
|
14
|
+
guest.post('/register', [RegisterController, 'store'], throttleRegistration).name('register.store')
|
|
14
15
|
})
|
|
15
16
|
|
|
16
17
|
router.middleware('auth').group((authed) => {
|
|
@@ -55,6 +55,7 @@ deployments (AWS Lambda, for example) serve them from a CDN instead.
|
|
|
55
55
|
| `bun run dev` | Start dev server (Bun + Vite) |
|
|
56
56
|
| `bun run build` | Build for production |
|
|
57
57
|
| `bun run preview` | Start the built app with the Bun production server |
|
|
58
|
+
| `bun run start` | Start the app in production mode (`NODE_ENV=production`); what a deployment runs |
|
|
58
59
|
| `bun run db:migrate` | Run database migrations |
|
|
59
60
|
| `bun run db:seed` | Seed the database |
|
|
60
61
|
| `bun run codegen` | Regenerate route/page types |
|
|
@@ -7,6 +7,30 @@ try {
|
|
|
7
7
|
process.exit(1)
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
// HSTS, the Secure session cookie, hidden error details and the APP_URL host
|
|
11
|
+
// allowlist all key on NODE_ENV=production, which `bun bin/serve.ts` alone never sets.
|
|
12
|
+
function deployedHost(appUrl: string | undefined): string | undefined {
|
|
13
|
+
if (!appUrl) return undefined
|
|
14
|
+
let host: string
|
|
15
|
+
try {
|
|
16
|
+
host = new URL(appUrl).hostname
|
|
17
|
+
} catch {
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
const loopback = host === 'localhost' || host.endsWith('.localhost') || host.startsWith('127.')
|
|
21
|
+
|| host === '[::1]' || host === '0.0.0.0'
|
|
22
|
+
return loopback ? undefined : host
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const nodeEnv = process.env.NODE_ENV
|
|
26
|
+
const appHost = deployedHost(process.env.APP_URL)
|
|
27
|
+
if (appHost && nodeEnv !== 'production' && nodeEnv !== 'test') {
|
|
28
|
+
console.warn(
|
|
29
|
+
`APP_URL points at ${appHost} but NODE_ENV is ${nodeEnv ? `"${nodeEnv}"` : 'unset'}, so the production `
|
|
30
|
+
+ 'security defaults are off. Start a deployed app with `bun run start` or set NODE_ENV=production.',
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
10
34
|
// `PORT=0` means "any free port", so this tests for a number, not truthiness.
|
|
11
35
|
const parsedPort = Number.parseInt(process.env.PORT ?? '', 10)
|
|
12
36
|
const port = Number.isInteger(parsedPort) ? parsedPort : 3333
|
|
@@ -2,10 +2,8 @@ import { defineEnv, Env, type InferEnv } from '@guren/core'
|
|
|
2
2
|
|
|
3
3
|
const env = defineEnv({
|
|
4
4
|
APP_NAME: Env.string().default('Guren'),
|
|
5
|
-
APP_ENV: Env.string().default('development'),
|
|
6
5
|
APP_KEY: Env.string().secret().requiredInProduction()
|
|
7
|
-
.describe('
|
|
8
|
-
APP_DEBUG: Env.boolean().default(false),
|
|
6
|
+
.describe('Root secret for signing and encryption; each use derives its own key.'),
|
|
9
7
|
APP_URL: Env.url().requiredInProduction()
|
|
10
8
|
.describe('Public base URL. Production host authorization answers only to its hostname.'),
|
|
11
9
|
PORT: Env.port().default(3333),
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"test": "bun test",
|
|
15
15
|
"build": "bun run codegen && bunx vite build",
|
|
16
16
|
"preview": "NODE_ENV=production bun bin/serve.ts",
|
|
17
|
+
"start": "NODE_ENV=production bun bin/serve.ts",
|
|
17
18
|
"db:make": "bunx guren make:migration",
|
|
18
19
|
"db:migrate": "bunx guren db:migrate",
|
|
19
20
|
"db:reset": "bunx guren db:reset",
|
|
@@ -21,10 +22,10 @@
|
|
|
21
22
|
"db:seed": "bunx guren db:seed"
|
|
22
23
|
},
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"@guren/cli": "^2.
|
|
25
|
-
"@guren/core": "^1.
|
|
25
|
+
"@guren/cli": "^2.29.0",
|
|
26
|
+
"@guren/core": "^1.24.0",
|
|
26
27
|
"@guren/inertia-client": "^1.2.2",
|
|
27
|
-
"@guren/orm": "^2.13.
|
|
28
|
+
"@guren/orm": "^2.13.1",
|
|
28
29
|
"@inertiajs/react": "^3.6.1",
|
|
29
30
|
"drizzle-orm": "1.0.0-rc.4",
|
|
30
31
|
"react": "^19.0.0",
|
|
@@ -20,6 +20,7 @@ Routes are registered through `routes/web.ts`, which exports a registrar consume
|
|
|
20
20
|
- `bun run dev` - start the Bun API server alongside the Vite dev server.
|
|
21
21
|
- `bun run build` - build both the client and SSR bundles with Vite.
|
|
22
22
|
- `bun run preview` - serve the built app through the Bun production server locally.
|
|
23
|
+
- `bun run start` - start the app in production mode (`NODE_ENV=production`); what a deployment runs.
|
|
23
24
|
- `bun run codegen` - regenerate route declarations, route helpers, and page manifests.
|
|
24
25
|
- `bun run db:make` - scaffold a new SQL migration from your schema using drizzle-kit.
|
|
25
26
|
- `bun run db:migrate` - apply database migrations.
|