This commit is contained in:
1 parent
e0aba40321
commit
0315387fe8
1 file changed
+86
-186
+86
-186
@@ -1,170 +1,99 @@
|
||||
# shared packages
|
||||
# shared
|
||||
|
||||
This folder is the single source of truth for reusable web UI across the projects in this workspace.
|
||||
The single source of truth for code reused across every project in this workspace: a Svelte design system and application shell, backend helpers (Postgres access, config, auth/session persistence, an AuthGuard client, log shipping) in four languages, and the Go media-backend/media-webui powering the whole `MEDIA_SERVICE`-family of projects.
|
||||
|
||||
The first packages are:
|
||||
---
|
||||
|
||||
- `@gabriel20xx/design-tokens` — colors, typography, spacing, radii, shadows, and light/dark theme variables.
|
||||
- `@gabriel20xx/web-ui` — Svelte components for the application shell, header/navigation, authentication, dialogs, profile/settings/about windows, buttons, cards, badges, and page headers.
|
||||
- `repos.gfranz.ch/Gabriel20xx/shared/backend` — Go helpers for environment configuration, `pgx` PostgreSQL pools, transactions, common auth persistence/passwords/tokens, and embedded Goose migrations.
|
||||
- `@gabriel20xx/node-backend` — Node.js `pg` pool/configuration, transactions, migrations, and auth/session persistence.
|
||||
- `gabriel20xx-shared-backend` — Python `asyncpg` pool/configuration and auth/session persistence.
|
||||
## Tech Stack
|
||||
|
||||
The components do not import `$app/state`, `$lib` stores, or a project-specific API. Applications provide those things through props and callbacks.
|
||||
| Layer | Technology |
|
||||
|-------|------------|
|
||||
| Web UI | Svelte, TypeScript |
|
||||
| Backend helpers | Go, Node.js, Python, Java |
|
||||
| Database | PostgreSQL (via `pgx`, `pg`, or `asyncpg`) |
|
||||
| Package registry | Gitea npm / Go / PyPI registries |
|
||||
|
||||
## Build and check the packages
|
||||
---
|
||||
|
||||
Run these commands from this directory:
|
||||
## Packages
|
||||
|
||||
| Package | Language | Purpose |
|
||||
|---|---|---|
|
||||
| `@gabriel20xx/design-tokens` | CSS | Colors, typography, spacing, radii, shadows, light/dark theme variables. |
|
||||
| `@gabriel20xx/web-ui` | Svelte | Application shell, header/navigation, auth screens, dialogs, buttons, cards, badges. |
|
||||
| `@gabriel20xx/media-shared-assets` | Browser JS/CSS | Shared client-side assets for the media-backend/`MEDIA_SERVICE` family. |
|
||||
| `repos.gfranz.ch/Gabriel20xx/shared/backend` | Go | Env config, `pgx` pools/transactions, auth persistence, an AuthGuard client (`authclient`), log shipping (`logshipper`), embedded Goose migrations. |
|
||||
| `@gabriel20xx/node-backend` | Node.js | `pg` pool/config/transactions/migrations, auth/session persistence, an AuthGuard client. |
|
||||
| `gabriel20xx-shared-backend` | Python | `asyncpg` pool/config, auth/session persistence, an AuthGuard client. |
|
||||
| `com.gabriel20xx.shared.auth` | Java | An AuthGuard client, mirroring the Go/Node/Python ones. |
|
||||
| `media-backend` / `media-webui` | Go / Svelte | The Go binary and web UI powering the `MEDIA_SERVICE`-family projects (see below). |
|
||||
|
||||
The `web-ui` components don't import `$app/state`, `$lib` stores, or a project-specific API — applications provide those through props and callbacks.
|
||||
|
||||
---
|
||||
|
||||
## How consumers actually use this
|
||||
|
||||
There are two different distribution paths in this workspace, used for different things:
|
||||
|
||||
### Vendored snapshot (the one every project actually uses)
|
||||
|
||||
Every consumer project vendors a full copy of this repo's tracked files into its own `shared/` (or, for the `media-backend` family, `engine/`) directory, committed directly rather than installed from a registry. This exists because none of these projects are meant to resolve a private registry dependency in a fresh CI checkout without extra credentials, and because a git submodule pointing at this (private) repo breaks the same way — see `scripts/sync-consumers.ps1`'s own comments for the full history.
|
||||
|
||||
To pull the latest snapshot into every registered consumer:
|
||||
|
||||
```powershell
|
||||
# from this repo
|
||||
.\scripts\sync-consumers.ps1 -Commit -Push
|
||||
```
|
||||
|
||||
The script copies every tracked file in this repo into each consumer's destination folder, regenerates any `package-lock.json` that depends on a local `file:` package, and commits/pushes only if something actually changed ("Sync shared/engine snapshot from `<short-sha>`"). Consumers are registered in the script's `$consumerProjects` list, each with a destination folder (`shared` by default, `engine` for the media-backend family). A `post-commit` hook (installed via `scripts/install-sync-hook.ps1`) runs this automatically — with `-Commit` only, never `-Push` — after every commit to this repo, so pushing the sync commits it creates to each consumer is still a manual follow-up step.
|
||||
|
||||
### Published registry packages (for standalone/external use)
|
||||
|
||||
The npm, Go, and Python packages are also published to this Gitea instance's package registries, for a project that genuinely wants a version-pinned dependency rather than a vendored snapshot. `.gitea/workflows/publish-*.yml` publish on a `v*` tag push or a matching Gitea release; `PACKAGE_REGISTRY_USERNAME`/`PACKAGE_REGISTRY_TOKEN` must be configured as Actions secrets. See "Publishing" below for the details. No project in this workspace currently consumes a package this way — it exists for future/external use.
|
||||
|
||||
---
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
npm install
|
||||
npm run check
|
||||
npm run build
|
||||
npm run build # builds packages/web-ui/dist (gitignored, not committed)
|
||||
npm run test:node-backend
|
||||
```
|
||||
|
||||
`npm run build` creates the distributable package in `packages/web-ui/dist`. The generated `dist` directory is intentionally ignored by the package configuration and should not be committed.
|
||||
|
||||
Validate the Go backend package from `shared/backend`:
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
go test ./...
|
||||
go vet ./...
|
||||
```
|
||||
|
||||
## Publish the individual packages
|
||||
|
||||
The package names use the `@gabriel20xx` npm scope. The `publish-npm.yml` and
|
||||
`publish-python.yml` Gitea workflows publish the packages when a `v*` tag is pushed or a
|
||||
matching Gitea release is published. Configure `PACKAGE_REGISTRY_USERNAME` and
|
||||
`PACKAGE_REGISTRY_TOKEN` as user Actions secrets before publishing.
|
||||
|
||||
The npm workflow publishes `design-tokens` first because `web-ui` depends on it. It also
|
||||
skips an npm version that is already present, so retrying a partially completed release is safe.
|
||||
The Python workflow uses Twine's `--skip-existing` option for the same reason.
|
||||
|
||||
## Install individual packages from a registry
|
||||
|
||||
From a project's frontend directory, install the published UI package from Gitea:
|
||||
|
||||
```bash
|
||||
npm config set @gabriel20xx:registry https://repos.gfranz.ch/api/packages/Gabriel20xx/npm/
|
||||
npm config set //repos.gfranz.ch/api/packages/Gabriel20xx/npm/:_authToken "$PACKAGE_REGISTRY_TOKEN"
|
||||
npm install @gabriel20xx/web-ui@0.1.0
|
||||
cd media-backend
|
||||
go test ./...
|
||||
go vet ./...
|
||||
```
|
||||
|
||||
The design-token package is installed transitively. Install it directly only when an application needs to reference its theme file itself:
|
||||
---
|
||||
|
||||
```bash
|
||||
npm install @gabriel20xx/design-tokens@0.1.0
|
||||
```
|
||||
|
||||
Import the shared stylesheet once from the application's global stylesheet, before project-specific overrides:
|
||||
|
||||
```css
|
||||
@import '@gabriel20xx/web-ui/styles.css';
|
||||
```
|
||||
|
||||
The stylesheet imports the design tokens and uses an `sc-` prefix, so it does not require Tailwind and does not collide with existing project class names. Set `data-theme="light"` or `data-theme="dark"` on the application root to select a theme. Dark is the default.
|
||||
|
||||
## Use the shared Go backend package
|
||||
|
||||
The Go module is published to the private Gitea Go Package Registry. Configure a
|
||||
read-only package token in the consuming workflow as `PACKAGE_REGISTRY_USERNAME`
|
||||
and `PACKAGE_REGISTRY_TOKEN`, then configure Go to use the registry before running
|
||||
`go get`, `go test`, or `go build`:
|
||||
|
||||
```bash
|
||||
export GOPROXY=https://repos.gfranz.ch/api/packages/Gabriel20xx/go,https://proxy.golang.org
|
||||
export GONOSUMDB=repos.gfranz.ch/Gabriel20xx/shared/*
|
||||
```
|
||||
|
||||
From a Go backend directory, add the released module (the module is a subdirectory of the shared repository):
|
||||
|
||||
```bash
|
||||
go get repos.gfranz.ch/Gabriel20xx/shared/backend@v0.1.0
|
||||
```
|
||||
|
||||
Then initialize the pool and shared auth store at startup:
|
||||
|
||||
```go
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/postgres"
|
||||
)
|
||||
|
||||
cfg, err := postgres.FromEnv()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
pool, err := postgres.Open(context.Background(), cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := auth.EnsureSchema(context.Background(), pool, auth.DefaultSchemaOptions()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
authStore := auth.NewStore(pool)
|
||||
```
|
||||
|
||||
Use `auth.SchemaOptions{SingleUser: false, APITokens: true}` for a multi-user service that needs API tokens. The shared package owns persistence primitives and password/token utilities; each application still owns authorization rules, cookies, HTTP handlers, roles, and domain tables.
|
||||
|
||||
For embedded SQL migrations, use `migrate.Up(ctx, databaseURL, embeddedMigrations, "migrations")`. The package does not contain application tables, so migrations remain versioned with the service that owns those tables.
|
||||
|
||||
The Go module lives under `backend`, so its release tag must use the submodule prefix (`backend/v0.1.0`). The consuming `go.mod` uses the normal module version (`v0.1.0`). To publish a release, push a tag such as `backend/v0.1.0`; the Gitea workflow packages the module and uploads it to the private registry. The workflow also listens for Gitea's `release.published` event, so a release can be published from the Gitea Releases page to manually retry a failed upload for an existing tag. Never reuse a package version that was already uploaded successfully.
|
||||
|
||||
For Node services, install the shared package after publishing it:
|
||||
|
||||
```bash
|
||||
npm install @gabriel20xx/node-backend@0.1.0
|
||||
```
|
||||
|
||||
For Python services, configure the Gitea index and install the published wheel:
|
||||
|
||||
```bash
|
||||
python -m pip install \
|
||||
--index-url "https://$PACKAGE_REGISTRY_USERNAME:$PACKAGE_REGISTRY_TOKEN@repos.gfranz.ch/api/packages/Gabriel20xx/pypi/simple" \
|
||||
gabriel20xx-shared-backend==0.1.0
|
||||
```
|
||||
|
||||
The Node and Python packages intentionally expose the same infrastructure concepts but use their native database drivers. This is the safe way to share behavior across the polyglot services; a Go package cannot be imported by the Python or Node applications.
|
||||
|
||||
## Existing MediaCollection shared package
|
||||
|
||||
`Original/MediaCollection/MediaShared` is an existing, larger media-specific package. It remains in place for media workflows and startup integration, while the MediaCollection WebUI authentication surface and admin-reset code use this shared package. Its remaining generic responsibilities can be migrated independently without introducing another authentication screen.
|
||||
|
||||
## Typical application wiring
|
||||
## Typical `web-ui` wiring
|
||||
|
||||
```svelte
|
||||
<script lang="ts">
|
||||
import {
|
||||
AboutDialog,
|
||||
AppShell,
|
||||
ProfileDialog,
|
||||
SettingsDialog
|
||||
} from '@gabriel20xx/web-ui';
|
||||
import { AboutDialog, AppShell, ProfileDialog, SettingsDialog } from '@gabriel20xx/web-ui';
|
||||
import { page } from '$app/state';
|
||||
|
||||
let aboutOpen = $state(false);
|
||||
let profileOpen = $state(false);
|
||||
let settingsOpen = $state(false);
|
||||
|
||||
const navItems = [
|
||||
{ href: '/', label: 'Dashboard' },
|
||||
{ href: '/about', label: 'About' }
|
||||
];
|
||||
</script>
|
||||
|
||||
<AppShell
|
||||
brand="My application"
|
||||
currentPath={page.url.pathname}
|
||||
navItems={navItems}
|
||||
navItems={[{ href: '/', label: 'Dashboard' }]}
|
||||
onLogout={() => auth.logout()}
|
||||
onProfile={() => (profileOpen = true)}
|
||||
onSettings={() => (settingsOpen = true)}
|
||||
@@ -174,73 +103,44 @@ The Node and Python packages intentionally expose the same infrastructure concep
|
||||
<slot />
|
||||
{/snippet}
|
||||
</AppShell>
|
||||
|
||||
<AboutDialog
|
||||
bind:open={aboutOpen}
|
||||
brand="My application"
|
||||
description="A short description supplied by this project."
|
||||
version="1.0.0"
|
||||
/>
|
||||
|
||||
<SettingsDialog bind:open={settingsOpen}>
|
||||
{#snippet children()}
|
||||
<!-- Project-specific settings controls go here. -->
|
||||
{/snippet}
|
||||
</SettingsDialog>
|
||||
|
||||
<ProfileDialog
|
||||
bind:open={profileOpen}
|
||||
currentEmail={auth.user?.email ?? ''}
|
||||
currentUsername={auth.user?.username ?? ''}
|
||||
onSave={(profile) => auth.updateProfile(profile)}
|
||||
/>
|
||||
```
|
||||
|
||||
The shared profile menu exposes Profile, Settings, About, and Log out. It uses the `About` navigation item's href automatically; pass `aboutHref` or `onAbout` to `AppShell` when a project uses a custom About destination or dialog.
|
||||
Import the stylesheet once, before project-specific overrides: `@import '@gabriel20xx/web-ui/styles.css';`. It uses an `sc-` class prefix, so it doesn't require Tailwind and doesn't collide with existing project classes. Set `data-theme="light"` or `data-theme="dark"` on the application root; dark is the default.
|
||||
|
||||
For authentication, use the same shell with `AuthPage` and connect the callbacks to the application's existing auth service:
|
||||
For authentication, use `AuthPage` and wire its callbacks to the project's auth service — every project in this workspace either talks to AuthGuard directly or proxies through its own backend to AuthGuard. See `Central/AuthGuard/README.md` for the identity service this whole authentication surface is built to work with.
|
||||
|
||||
```svelte
|
||||
<AuthPage
|
||||
brand="My application"
|
||||
faviconHref="/favicon.svg"
|
||||
onLogin={(credentials) => auth.login(credentials.identifier, credentials.password)}
|
||||
onSetup={(credentials) => auth.setup(credentials)}
|
||||
onRequestReset={() => auth.requestAdminReset()}
|
||||
onConfirmReset={(code) => auth.confirmAdminReset(code)}
|
||||
setupRequired={setupRequired}
|
||||
tagline="Your project tagline"
|
||||
/>
|
||||
---
|
||||
|
||||
## Using the Go backend package
|
||||
|
||||
```go
|
||||
import (
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/postgres"
|
||||
)
|
||||
|
||||
pool, _ := postgres.Open(ctx, postgres.FromEnv())
|
||||
authStore := auth.NewStore(pool) // local Postgres-backed accounts
|
||||
client := authclient.New(authGuardURL) // or proxy to AuthGuard instead
|
||||
```
|
||||
|
||||
Applications that support MFA can enable the shared optional authenticator-code field with
|
||||
`showTotp` and forward `credentials.totpToken` to their login endpoint. The base layout and reset
|
||||
controls remain the same as every other shared auth screen.
|
||||
Use `auth.SchemaOptions{SingleUser: false, APITokens: true}` for a multi-user service that needs API tokens. This package owns persistence primitives and password/token utilities; each application still owns authorization rules, cookies, HTTP handlers, roles, and domain tables. For embedded SQL migrations, use `migrate.Up(ctx, databaseURL, embeddedMigrations, "migrations")` — this package has no application tables of its own, so migrations stay versioned with the service that owns those tables.
|
||||
|
||||
The shared reset controls request a short-lived code printed by the server-side auth package and consume it once after confirmation. The package handles form state, validation attributes, loading state, and error presentation. The application remains responsible for deciding where to navigate after a successful action and how errors are returned.
|
||||
---
|
||||
|
||||
## Local package development
|
||||
|
||||
Before publishing, build the packages and install them from the local workspace in a frontend project:
|
||||
## Publishing
|
||||
|
||||
```bash
|
||||
# From Web/shared
|
||||
npm run build
|
||||
|
||||
# From a project frontend directory; do not commit this file dependency for CI
|
||||
npm install ../../../shared/packages/design-tokens ../../../shared/packages/web-ui
|
||||
npm run changeset # describe the change
|
||||
npm run version-packages # bump versions, commit the changelog
|
||||
npm run release # publishes npm packages in dependency order (design-tokens first)
|
||||
```
|
||||
|
||||
For CI and normal development, use the published version instead of a `file:` dependency. A local file dependency only works when the checkout also contains the sibling `shared` directory.
|
||||
The Go module lives under `backend`, so its release tag uses the submodule prefix (`backend/v0.1.0`); push that tag and the Gitea workflow packages and uploads the module. The npm and Python workflows both skip a version already present in the registry, so retrying a partially completed release is safe.
|
||||
|
||||
## Updating the design system
|
||||
---
|
||||
|
||||
When a component or token changes:
|
||||
## CI/CD
|
||||
|
||||
1. Run `npm run changeset` and describe the change.
|
||||
2. Run `npm run version-packages` and commit the generated version/changelog files.
|
||||
3. Run `npm install`, `npm run check`, `npm run build`, and `npm run test:node-backend`.
|
||||
4. Run `npm run release`; Changesets publishes the npm packages in dependency order.
|
||||
5. Update each project with `npm install @gabriel20xx/web-ui@<new-version>` and commit its lockfile.
|
||||
|
||||
The package boundary is intentional: application-specific screens stay in their project, while repeated visual language and account/layout infrastructure stays here.
|
||||
`lint.yml` and `tests.yml` each run three jobs — Node/Svelte, Go, and Python — plus dedicated `media-backend-lint.yml`/`media-backend-tests.yml` for the `media-backend` Go module. `publish-npm.yml`, `publish-go.yml`, and `publish-python.yml` handle registry publishing as described above.
|
||||
Reference in new issue
Block a user