-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add drizzle studio to admin area #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a1b254a
refactor: run sqlite migrations at startup
toddeTV 73c0e9b
feat: add drizzle studio proxy
toddeTV ade600e
feat: add admin database page
toddeTV 222bab0
docs: note admin database route
toddeTV bf89023
docs: record drizzle studio proxy
toddeTV 547d0fd
docs: sync env example defaults
toddeTV 98e4156
fix: harden drizzle studio asset proxy fetch
toddeTV 9ff0137
fix: harden drizzle studio html sanitization
toddeTV ce6bd5b
fix: harden drizzle studio shell fetch
toddeTV 238bf09
fix: bound drizzle studio rpc proxy fetch
toddeTV 442afed
fix: bound drizzle studio startup polling
toddeTV e1dcfb0
fix: validate drizzle studio asset paths
toddeTV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,51 @@ | ||
| # --- Server-side secrets (not exposed to client) | ||
| # --- Server-side runtime config (not exposed to client) | ||
|
|
||
| # Admin Credentials (defaults to admin / 123) | ||
| NUXT_ADMIN_USERNAME= | ||
| NUXT_ADMIN_PASSWORD= | ||
| # Admin login username. | ||
| # Default from nuxt.config.ts: admin | ||
| NUXT_ADMIN_USERNAME=admin | ||
|
|
||
| # JWT Secret for Admin Authentication (defaults to tryUJ0zQbstPbTOrezme+Fv+KndzDNRx5lmSeelr2ial2/2yV8HqLeQ2felJafqf) | ||
| NUXT_JWT_SECRET= | ||
| # Admin login password. | ||
| # Default from nuxt.config.ts: 123 | ||
| NUXT_ADMIN_PASSWORD=123 | ||
|
|
||
| # --- Public configuration (available on both client and server) | ||
| # Private internal port for the embedded Drizzle Studio worker. | ||
| # Default from nuxt.config.ts: 64983 | ||
| NUXT_DRIZZLE_STUDIO_INTERNAL_PORT=64983 | ||
|
|
||
| # WebSocket URL for client connections (defaults to current host) | ||
| # NUXT_PUBLIC_WS_URL=ws://localhost:3000 | ||
| # JWT secret used for admin authentication. | ||
| # Default from nuxt.config.ts: tryUJ0zQbstPbTOrezme+Fv+KndzDNRx5lmSeelr2ial2/2yV8HqLeQ2felJafqf | ||
| NUXT_JWT_SECRET=tryUJ0zQbstPbTOrezme+Fv+KndzDNRx5lmSeelr2ial2/2yV8HqLeQ2felJafqf | ||
|
|
||
| # API URL for client requests (defaults to current host) | ||
| # NUXT_PUBLIC_API_URL=http://localhost:3000 | ||
| # --- Public runtime config (available on client and server) | ||
|
|
||
| # Server Configuration | ||
| # NUXT_PUBLIC_HOST=0.0.0.0 | ||
| # NUXT_PUBLIC_PORT=3000 | ||
| # Base API URL used by the client. | ||
| # Default from nuxt.config.ts: empty string, which keeps same-origin requests. | ||
| NUXT_PUBLIC_API_URL= | ||
|
|
||
| # Show debug (defaults to false / false) | ||
| # NUXT_PUBLIC_DEBUG_SHOW_WEBSOCKET_CONNECTIONS_IN_FRONTEND=false | ||
| # NUXT_PUBLIC_DEBUG_SHOW_CONSOLE_OUTPUTS=false | ||
| # Toggle console debug output in shared logging helpers. | ||
| # Default from nuxt.config.ts: false | ||
| NUXT_PUBLIC_DEBUG_SHOW_CONSOLE_OUTPUTS=false | ||
|
|
||
| # Emoji-Sent Cooldown in Milliseconds (defaults to 1500) | ||
| NUXT_PUBLIC_EMOJI_COOLDOWN_MS= | ||
| # Show websocket connection debug info in the frontend debug panel. | ||
| # Default from nuxt.config.ts: false | ||
| NUXT_PUBLIC_DEBUG_SHOW_WEBSOCKET_CONNECTIONS_IN_FRONTEND=false | ||
|
|
||
| # Cooldown in milliseconds between emoji submissions. | ||
| # Default from nuxt.config.ts: 1500 | ||
| NUXT_PUBLIC_EMOJI_COOLDOWN_MS=1500 | ||
|
|
||
| # Public host hint used by the app when building URLs. | ||
| # Default from nuxt.config.ts: 0.0.0.0 | ||
| NUXT_PUBLIC_HOST=0.0.0.0 | ||
|
|
||
| # Public port hint used by the app when building URLs. | ||
| # Default from nuxt.config.ts: 3000 | ||
| NUXT_PUBLIC_PORT=3000 | ||
|
|
||
| # Public version string exposed to the client. | ||
| # Default from nuxt.config.ts: current package version. | ||
| NUXT_PUBLIC_VERSION=1.0.0-rc.0 | ||
|
|
||
| # Base websocket URL used by the client. | ||
| # Default from nuxt.config.ts: empty string, which keeps same-origin websocket resolution. | ||
| NUXT_PUBLIC_WS_URL= | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <script setup lang="ts"> | ||
| definePageMeta({ | ||
| layout: 'default', | ||
| middleware: 'auth', | ||
| footer: true, | ||
| background: true, | ||
| localeSwitcher: true, | ||
| }) | ||
|
|
||
| const { t } = useI18n() | ||
| const studioUrl = ref('') | ||
|
|
||
| onMounted(() => { | ||
| const currentUrl = new URL(window.location.href) | ||
| const resolvedPort = currentUrl.port || (currentUrl.protocol === 'https:' ? '443' : '80') | ||
| const query = new URLSearchParams({ | ||
| host: currentUrl.hostname, | ||
| port: resolvedPort, | ||
| }) | ||
|
|
||
| studioUrl.value = `/api/admin/drizzle-studio/app/?${query.toString()}` | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <div class="mx-auto max-w-6xl p-5"> | ||
| <UiPageTitle>{{ t('title') }}</UiPageTitle> | ||
|
|
||
| <p class="mb-5 text-center text-sm text-gray-600"> | ||
| {{ t('description') }} | ||
| </p> | ||
|
|
||
| <div class="mb-5 flex flex-wrap justify-center gap-3"> | ||
| <NuxtLink | ||
| class="block border-[3px] border-black bg-white px-4 py-3 text-sm uppercase transition-all duration-200 | ||
| hover:translate-x-1 hover:shadow-[-5px_5px_0_#000]" | ||
| to="/admin" | ||
| > | ||
| {{ t('back') }} | ||
| </NuxtLink> | ||
|
|
||
| <a | ||
| v-if="studioUrl" | ||
| class="block border-[3px] border-black bg-black px-4 py-3 text-sm uppercase text-white | ||
| transition-all duration-200 hover:translate-x-1 hover:shadow-[-5px_5px_0_#000]" | ||
| :href="studioUrl" | ||
| rel="noopener noreferrer" | ||
| target="_blank" | ||
| > | ||
| {{ t('openNewTab') }} | ||
| </a> | ||
| </div> | ||
|
|
||
| <div class="overflow-hidden border-[3px] border-black bg-white"> | ||
| <iframe | ||
| v-if="studioUrl" | ||
| class="h-[78vh] w-full bg-white" | ||
| :src="studioUrl" | ||
| :title="t('iframeTitle')" | ||
| /> | ||
|
|
||
| <div | ||
| v-else | ||
| class="flex h-[78vh] items-center justify-center text-sm uppercase tracking-wide text-gray-500" | ||
| > | ||
| {{ t('loading') }} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </template> | ||
|
|
||
| <i18n lang="yaml"> | ||
| en: | ||
| title: Database | ||
| description: Browse and edit the SQLite database through a server-side Drizzle Studio proxy. | ||
| back: Back to Admin | ||
| openNewTab: Open in New Tab | ||
| iframeTitle: Drizzle Studio | ||
| loading: Loading Studio | ||
| de: | ||
| title: Datenbank | ||
| description: SQLite-Datenbank über einen serverseitigen Drizzle-Studio-Proxy durchsuchen und bearbeiten. | ||
| back: Zurück zur Admin | ||
| openNewTab: In neuem Tab öffnen | ||
| iframeTitle: Drizzle Studio | ||
| loading: Studio wird geladen | ||
| ja: | ||
| title: データベース | ||
| description: サーバー側の Drizzle Studio プロキシ経由で SQLite データベースを参照、編集します。 | ||
| back: 管理画面に戻る | ||
| openNewTab: 新しいタブで開く | ||
| iframeTitle: Drizzle Studio | ||
| loading: Studio を読み込み中 | ||
| </i18n> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { verifyAdmin } from '../../../../utils/auth' | ||
|
|
||
| const DRIZZLE_STUDIO_APP_ORIGIN = 'https://local.drizzle.studio' | ||
| const DRIZZLE_STUDIO_ASSET_FETCH_TIMEOUT_MS = 8000 | ||
| const FORWARDED_HEADERS = [ | ||
| 'cache-control', | ||
| 'content-type', | ||
| 'etag', | ||
| 'last-modified', | ||
| ] | ||
|
|
||
| export default defineEventHandler(async (event) => { | ||
| await verifyAdmin(event) | ||
|
|
||
| const assetPath = event.context.params?.asset | ||
|
|
||
| if (!assetPath) { | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: 'Drizzle Studio asset not found', | ||
| }) | ||
| } | ||
|
|
||
| const pathSegments = assetPath.split('/') | ||
|
|
||
| if (pathSegments.some(segment => segment === '.' || segment === '..')) { | ||
| throw createError({ | ||
| statusCode: 400, | ||
| statusMessage: 'Invalid Drizzle Studio asset path', | ||
| }) | ||
| } | ||
|
|
||
| const requestUrl = getRequestURL(event) | ||
| const upstreamUrl = `${DRIZZLE_STUDIO_APP_ORIGIN}/${assetPath}${requestUrl.search}` | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| let response: Response | ||
|
|
||
| try { | ||
| response = await fetch(upstreamUrl, { | ||
| signal: AbortSignal.timeout(DRIZZLE_STUDIO_ASSET_FETCH_TIMEOUT_MS), | ||
| }) | ||
| } | ||
| catch (error) { | ||
| const isAbort = error instanceof Error && error.name === 'AbortError' | ||
|
|
||
| throw createError({ | ||
| statusCode: isAbort ? 504 : 502, | ||
| statusMessage: isAbort | ||
| ? `Timed out loading Drizzle Studio asset: ${assetPath}` | ||
| : 'Failed to reach Drizzle Studio asset upstream', | ||
| data: error instanceof Error ? { message: error.message } : undefined, | ||
| }) | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| throw createError({ | ||
| statusCode: response.status, | ||
| statusMessage: `Failed to load Drizzle Studio asset: ${assetPath}`, | ||
| }) | ||
| } | ||
|
|
||
| setResponseStatus(event, response.status, response.statusText) | ||
|
|
||
| for (const header of FORWARDED_HEADERS) { | ||
| const value = response.headers.get(header) | ||
|
|
||
| if (value) { | ||
| setHeader(event, header, value) | ||
| } | ||
| } | ||
|
|
||
| return new Uint8Array(await response.arrayBuffer()) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.