From c9ce46d7bcffba9d6eaa3dea1a97e64f744a5e3f Mon Sep 17 00:00:00 2001 From: C-West8 <125926137+C-West8@users.noreply.github.com> Date: Fri, 23 Oct 2026 06:52:20 -0500 Subject: [PATCH] building out frontend and docker --- .dockerignore | 18 + .env.example | 9 + .gitignore | 2 + Dockerfile | 34 ++ README.md | 22 +- docker-compose.yml | 31 ++ docs/design.md | 68 +++- docs/runbook.md | 84 +++++ src/app.d.ts | 4 +- src/app.html | 6 + src/env.ts | 23 ++ src/hooks.server.ts | 37 ++ src/lib/components/AutoRefresh.svelte | 13 + src/lib/components/Badge.svelte | 7 + src/lib/components/FileTree.svelte | 179 ++++++++++ src/lib/components/ProjectFields.svelte | 62 ++++ src/lib/components/RunNowButton.svelte | 81 +++++ src/lib/components/TimeAgo.svelte | 14 + src/lib/format.ts | 80 +++++ src/lib/now.svelte.ts | 19 ++ .../server/adapters/simulated/fake-drive.ts | 6 +- .../simulated/simulated-backend.spec.ts | 42 +++ .../adapters/simulated/simulated-backend.ts | 4 +- src/lib/server/adapters/sqlite/database.ts | 2 +- .../adapters/sqlite/project-repository.ts | 6 +- .../adapters/sqlite/run-repository.spec.ts | 19 ++ .../server/adapters/sqlite/run-repository.ts | 10 +- src/lib/server/app.ts | 24 ++ src/lib/server/auth/admin-auth.ts | 10 +- src/lib/server/auth/session-cookie.ts | 23 ++ src/lib/server/container.ts | 177 ++++++++++ src/lib/server/forms.ts | 27 ++ src/lib/server/status/status-service.spec.ts | 70 ++++ src/lib/server/status/status-service.ts | 20 +- src/lib/server/sync/core/ports.ts | 2 + src/lib/server/sync/project-service.spec.ts | 96 ++++++ src/lib/server/sync/testing/harness.ts | 45 ++- src/lib/styles/app.css | 316 ++++++++++++++++++ src/routes/+layout.server.ts | 7 + src/routes/+layout.svelte | 93 +++++- src/routes/+page.server.ts | 6 + src/routes/+page.svelte | 85 ++++- src/routes/admin/+page.server.ts | 15 + src/routes/admin/+page.svelte | 55 +++ src/routes/admin/login/+page.server.ts | 23 ++ src/routes/admin/login/+page.svelte | 54 +++ .../admin/projects/[id]/+page.server.ts | 47 +++ src/routes/admin/projects/[id]/+page.svelte | 148 ++++++++ src/routes/admin/projects/new/+page.server.ts | 22 ++ src/routes/admin/projects/new/+page.svelte | 47 +++ src/routes/healthz/+server.ts | 9 + src/routes/projects/[id]/+page.server.ts | 25 ++ src/routes/projects/[id]/+page.svelte | 193 +++++++++++ .../projects/[id]/history/+page.server.ts | 15 + src/routes/projects/[id]/history/+page.svelte | 84 +++++ .../[id]/history/[runId]/+page.server.ts | 15 + .../[id]/history/[runId]/+page.svelte | 72 ++++ vite.config.ts | 8 +- 58 files changed, 2665 insertions(+), 50 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 docs/runbook.md create mode 100644 src/hooks.server.ts create mode 100644 src/lib/components/AutoRefresh.svelte create mode 100644 src/lib/components/Badge.svelte create mode 100644 src/lib/components/FileTree.svelte create mode 100644 src/lib/components/ProjectFields.svelte create mode 100644 src/lib/components/RunNowButton.svelte create mode 100644 src/lib/components/TimeAgo.svelte create mode 100644 src/lib/format.ts create mode 100644 src/lib/now.svelte.ts create mode 100644 src/lib/server/adapters/simulated/simulated-backend.spec.ts create mode 100644 src/lib/server/app.ts create mode 100644 src/lib/server/auth/session-cookie.ts create mode 100644 src/lib/server/container.ts create mode 100644 src/lib/server/forms.ts create mode 100644 src/lib/server/status/status-service.spec.ts create mode 100644 src/lib/server/sync/project-service.spec.ts create mode 100644 src/lib/styles/app.css create mode 100644 src/routes/+layout.server.ts create mode 100644 src/routes/+page.server.ts create mode 100644 src/routes/admin/+page.server.ts create mode 100644 src/routes/admin/+page.svelte create mode 100644 src/routes/admin/login/+page.server.ts create mode 100644 src/routes/admin/login/+page.svelte create mode 100644 src/routes/admin/projects/[id]/+page.server.ts create mode 100644 src/routes/admin/projects/[id]/+page.svelte create mode 100644 src/routes/admin/projects/new/+page.server.ts create mode 100644 src/routes/admin/projects/new/+page.svelte create mode 100644 src/routes/healthz/+server.ts create mode 100644 src/routes/projects/[id]/+page.server.ts create mode 100644 src/routes/projects/[id]/+page.svelte create mode 100644 src/routes/projects/[id]/history/+page.server.ts create mode 100644 src/routes/projects/[id]/history/+page.svelte create mode 100644 src/routes/projects/[id]/history/[runId]/+page.server.ts create mode 100644 src/routes/projects/[id]/history/[runId]/+page.svelte diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8d2f9af --- /dev/null +++ b/.dockerignore @@ -0,0 +1,18 @@ +.git +.svelte-kit +.vscode +build +node_modules +docs + +.env +.env.* +!.env.example + +*.db +*.db-shm +*.db-wal + +Dockerfile +docker-compose.yml +.dockerignore diff --git a/.env.example b/.env.example index c0387dd..43a96dd 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,12 @@ +# "live" (real SharePoint/ACC) or "simulated" (in-memory demo; nothing persists) +SYNC_BACKEND=simulated + +# Admin pages are disabled unless both are set +ADMIN_USERNAME=admin +ADMIN_PASSWORD=change-me +# Optional; without it admin logins end when the server restarts +SESSION_SECRET= + DATABASE_PATH=local.db POLL_INTERVAL_SECONDS=180 MANUAL_RUN_COOLDOWN_SECONDS=30 diff --git a/.gitignore b/.gitignore index 23be534..98732d7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ vite.config.js.timestamp-* vite.config.ts.timestamp-* # SQLite *.db +*.db-shm +*.db-wal diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3e4f620 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +# syntax=docker/dockerfile:1 + +FROM node:24-bookworm-slim AS base +WORKDIR /app + +# Build stage. better-sqlite3 normally downloads a prebuilt binary; the compilers +# are only a fallback for when no prebuilt one matches. +FROM base AS build +RUN apt-get update \ + && apt-get install -y --no-install-recommends python3 make g++ \ + && rm -rf /var/lib/apt/lists/* +COPY package.json package-lock.json .npmrc ./ +RUN npm ci +COPY . . +# The address users open the app at, when served over plain HTTP (see docs/runbook.md). +ARG ORIGIN +RUN npm run build && npm prune --omit=dev + +# Runtime stage: the built server, production dependencies and the migrations. +FROM base +ENV NODE_ENV=production \ + PORT=3000 \ + DATABASE_PATH=/data/sync.db +COPY --from=build /app/package.json ./ +COPY --from=build /app/node_modules ./node_modules +COPY --from=build /app/build ./build +COPY --from=build /app/drizzle ./drizzle +RUN mkdir /data && chown node:node /data +USER node +VOLUME /data +EXPOSE 3000 +HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \ + CMD ["node", "-e", "fetch(`http://127.0.0.1:${process.env.PORT}/healthz`).then((r) => process.exit(r.ok ? 0 : 1), () => process.exit(1))"] +CMD ["node", "build"] diff --git a/README.md b/README.md index 9a88702..030a523 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,30 @@ There is also a small web page that shows each project's file tree, sync status, ## Status -In development. The sync core and database layer are built and tested against simulated SharePoint and ACC. The real SharePoint/ACC connections and the web UI are next. See: +In development. The sync engine, database, scheduler and web UI are built, and you can try them end to end in **simulated mode**. The real SharePoint/ACC connections come next, once credentials arrive. See: - [docs/design.md](docs/design.md): architecture, interfaces, data model and build order - [docs/access-setup.md](docs/access-setup.md): what IT and the ACC admin need to set up +- [docs/runbook.md](docs/runbook.md): deploying with Docker/Portainer, updating, backups, troubleshooting + +## Running it locally + +```sh +npm install +cp .env.example .env # simulated mode, admin login admin / change-me +npm run dev +``` + +In simulated mode, SharePoint and ACC are in-memory stand-ins. A demo project is created at startup, filled with sample files, and edited at random every 45 seconds so there's something to watch. One file (`Corrupt model.rvt`) is always rejected, so the issues list has something in it. Nothing persists: a restart starts over. + +| Command | What it does | +| --------------- | ------------------------------------------------------------------------------------------------------------------- | +| `npm test` | Unit and integration tests (Vitest) | +| `npm run check` | Type check | +| `npm run lint` | Prettier + ESLint | +| `npm run build` | Production build into `build/` (`node build` runs it; run it from the repo root so `drizzle/` migrations are found) | + +To try the Docker image locally: put `ADMIN_USERNAME`, `ADMIN_PASSWORD`, `SESSION_SECRET`, `SYNC_BACKEND=simulated` and `ORIGIN=http://localhost:3000` in `.env`, then run `docker compose up -d --build`. ## Stack diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..09f4c69 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +# Portainer stack (or `docker compose up -d --build`). Set the variables below in +# Portainer's stack environment, or in a .env file next to this one. See docs/runbook.md. +services: + sync: + build: + context: . + args: + # Only when serving plain HTTP: the exact address users open, e.g. http://sync-server:3000 + ORIGIN: ${ORIGIN:-} + image: sharepoint-acc-sync:latest + restart: unless-stopped + ports: + - '${HOST_PORT:-3000}:3000' + environment: + SYNC_BACKEND: ${SYNC_BACKEND:-live} + ADMIN_USERNAME: ${ADMIN_USERNAME:?Set ADMIN_USERNAME} + ADMIN_PASSWORD: ${ADMIN_PASSWORD:?Set ADMIN_PASSWORD} + SESSION_SECRET: ${SESSION_SECRET:?Set SESSION_SECRET to a long random string} + POLL_INTERVAL_SECONDS: ${POLL_INTERVAL_SECONDS:-180} + MANUAL_RUN_COOLDOWN_SECONDS: ${MANUAL_RUN_COOLDOWN_SECONDS:-30} + MAX_ATTEMPTS: ${MAX_ATTEMPTS:-3} + HISTORY_RETENTION_DAYS: ${HISTORY_RETENTION_DAYS:-7} + # Times on the status pages are shown in this zone + TZ: ${TZ:-America/Chicago} + volumes: + - sync-data:/data + # Lets a sync in progress finish when the container is stopped or updated + stop_grace_period: 2m + +volumes: + sync-data: diff --git a/docs/design.md b/docs/design.md index b561737..08ed091 100644 --- a/docs/design.md +++ b/docs/design.md @@ -7,7 +7,7 @@ | Direction | One-way, SharePoint → ACC | | Latency | Poll every ~3 min (configurable); "Run now" for immediate sync | | Operations | Create, update (new ACC version), rename, move, delete | -| Scope | Many projects per server; each project is a 1:1 pair of SharePoint root ↔ ACC root | +| Scope | Many projects per server; each project is a 1:1 pair of SharePoint root and ACC root | | ACC ownership | Only touch ACC items this tool created; hand-added ACC content is left alone | | Ignored files | Temp/lock/system files are skipped silently (not issues) | | Retries | 3 attempts across runs, then surface as an issue | @@ -164,14 +164,42 @@ MAX_ATTEMPTS=3 HISTORY_RETENTION_DAYS=7 ``` -Env vars are declared and validated with `zod` in `src/env.ts` (SvelteKit's `defineEnvVars`); the server refuses to start on bad config. Only the variables in use so far are declared; the Graph, APS and admin ones get added along with the code that reads them. Run exactly **one** replica, because the locks live in-process. +Env vars are declared and validated with `zod` in `src/env.ts` (SvelteKit's `defineEnvVars`); the server refuses to start on bad config. Only the variables in use so far are declared; the Graph and APS ones get added along with the adapters that read them. Run exactly **one** replica, because the locks live in-process. + +`SYNC_BACKEND` picks the SharePoint/ACC connection: + +- **`live`**: the real Graph/APS adapters. Until those exist, a `NotConnectedBackend` stands in. Runs fail as an outage (no attempts charged) and the add-project form explains why. +- **`simulated`**: `FakeDrive`/`FakeAcc` behind a `SimulatedBackend` that resolves any pasted path, seeds sample files and makes random edits. The database is in-memory in this mode, so the fake services and the sync state never disagree. + +`ADMIN_USERNAME`/`ADMIN_PASSWORD` turn the admin pages on; `SESSION_SECRET` keeps logins valid across restarts. + +Two settings live outside `src/env.ts`: + +- **`ORIGIN`** is read at build time (`paths.origin` in `vite.config.ts`). SvelteKit 3 fixes the app's address at build time, and adapter-node otherwise assumes HTTPS. So over plain HTTP, form posts fail the CSRF check, and the session cookie is marked `Secure`, unless `ORIGIN` is set. Behind an HTTPS reverse proxy, leave it unset. +- **`TZ`** is the container's time zone. Times are formatted on the server first, so it should match the users' zone. + +### Deployment + +- `Dockerfile`: a multi-stage build on `node:24-bookworm-slim`. The runtime stage holds `build/`, production `node_modules` (`better-sqlite3`, `zod`; everything else is bundled) and `drizzle/`, runs as `node`, and keeps the database at `/data/sync.db`. +- `docker-compose.yml` is the Portainer stack: one service, the `sync-data` volume, required settings enforced with `${VAR:?}`, and `stop_grace_period: 2m` so a run in progress can finish. +- `GET /healthz` backs the image's `HEALTHCHECK`. +- On startup, runs left without an outcome (the process was killed mid-run) are closed as failed. The next run carries on from the saved item state. +- Operations are covered in [runbook.md](runbook.md). + +### Code conventions + +- **Imports:** never `../`. Anything outside the current folder goes through the `#lib/...` alias (`package.json` `imports`). Every local import carries its `.ts` extension, which SvelteKit 3's tsconfig requires. +- **Internal links:** always built with `resolve()` from `$app/paths` (enforced by lint). ## Source layout ``` src/env.ts # env var declarations + validation +src/hooks.server.ts # init → startApp(config); admin route guard src/lib/server/ - container.ts # (todo) composition root: the only place adapters are chosen + container.ts # composition root: the only place adapters are chosen + app.ts # holds the running services (survives dev reloads) + forms.ts logger.ts sync/ core/ # pure TS, no SvelteKit imports model.ts ports.ts errors.ts @@ -182,16 +210,22 @@ src/lib/server/ tree.ts ignore.ts coordinator.ts # per-project lock + manual cooldown scheduler.ts # non-overlapping interval tasks - testing/ # FakeDrive, FakeAcc, harness (real engine + in-memory SQLite) + project-service.ts # admin operations: create/edit/reset/delete, retry + testing/ # harness: real engine + in-memory SQLite + fakes + status/ # read models for the UI: summaries, issues, file tree + auth/ # admin credentials, signed session tokens, cookie helpers adapters/ sqlite/ # Drizzle schema, migrations runner, repositories - graph/ # (todo) GraphChangeSource, auth (msal-node) - aps/ # (todo) ApsDocumentTarget, auth, upload flow + simulated/ # FakeDrive, FakeAcc, SimulatedBackend (demo + tests) + not-connected.ts # placeholder for live mode until Graph/APS exist + graph/ # (todo) GraphChangeSource + SourceRootResolver, auth (msal-node) + aps/ # (todo) ApsDocumentTarget + TargetRootResolver, upload flow http/ # (todo) shared fetch wrapper: retry, backoff, Retry-After - auth/ # (todo) admin session cookie +src/lib/components/ # Badge, FileTree, RunNowButton, ProjectFields, TimeAgo, AutoRefresh +src/lib/styles/app.css # design tokens + shared styles +src/routes/ # projects, project status + history, admin, healthz drizzle/ # generated SQL migrations (npm run db:generate) -src/hooks.server.ts # (todo) init → container → scheduler; admin route guard -src/routes/ # (todo) UI +Dockerfile docker-compose.yml ``` ## Open questions (resolved by spikes) @@ -205,11 +239,11 @@ src/routes/ # (todo) UI 0. **Access requests:** start now; they're the slowest part. See [access-setup.md](access-setup.md). 1. **Spikes** against a sandbox ACC project and test SharePoint site. Answer the open questions above and record the results here. -2. ✅ **Scaffold:** SvelteKit 3 + adapter-node, TypeScript strict, Drizzle + better-sqlite3, zod, Vitest, ESLint/Prettier. (Dockerfile moves to step 9.) -3. ✅ **Core:** ports, IgnorePolicy, ingest, planner, reconciler and SyncEngine, tested against FakeDrive/FakeAcc. -4. ✅ **SQLite repositories** and migrations. -5. **GraphChangeSource.** -6. **ApsDocumentTarget**, including the fallbacks chosen in step 1. -7. **Wiring:** ✅ coordinator, scheduler. Still to do: composition root, `init` hook, retention job schedule. -8. **UI:** status pages, Run now, admin login, project add/edit/reset. -9. **Ship:** Docker image, Portainer stack file, runbook. +2. **Scaffold (done):** SvelteKit 3 + adapter-node, TypeScript strict, Drizzle + better-sqlite3, zod, Vitest, ESLint/Prettier. (Dockerfile moves to step 9.) +3. **Core (done):** ports, IgnorePolicy, ingest, planner, reconciler and SyncEngine, tested against FakeDrive/FakeAcc. +4. **SQLite repositories and migrations (done).** +5. **GraphChangeSource** + SharePoint URL resolver. +6. **ApsDocumentTarget** + ACC folder URL resolver, including the fallbacks chosen in step 1. +7. **Wiring (done):** coordinator, scheduler, composition root, `init` hook, history pruning, simulated mode. +8. **UI (done):** projects list, project status (title block, issues with retry, file tree), 7-day history with per-run detail, Run now with cooldown, admin login, add/edit/resume/reset/remove. +9. **Ship (done):** Docker image, Portainer stack file, runbook. The image was tested locally. A first Portainer deploy is still to do. diff --git a/docs/runbook.md b/docs/runbook.md new file mode 100644 index 0000000..035ef55 --- /dev/null +++ b/docs/runbook.md @@ -0,0 +1,84 @@ +# Runbook + +How to deploy, update, back up and troubleshoot the sync server. + +## What runs + +- **One container**, built from the `Dockerfile`. Never run more than one copy: the locks that stop two syncs of the same project overlapping live inside the process. +- **One volume**, `sync-data`, mounted at `/data`. It holds `sync.db`, the SQLite database with the projects, the record of which ACC item belongs to which SharePoint item, and the last 7 days of history. +- **Port 3000** inside the container. `HOST_PORT` sets the port on the server (default 3000). +- **Health check:** `GET /healthz` answers `{"status":"ok"}` once the server has started. Docker checks it every 30 seconds, and Portainer shows the result as healthy or unhealthy. +- **Logs:** one JSON object per line on stdout. View them in Portainer under the container's logs. + +## Choose how people will reach it + +Decide this before the first deploy, because it changes how the image is built. + +- **Behind an HTTPS reverse proxy** (recommended). The proxy must pass the original `Host` header on, which most do by default. Leave `ORIGIN` empty. +- **Plain HTTP** on the VPN, for example `http://sync-server:3000`. Set `ORIGIN` to exactly the address people will type, with no trailing slash. `ORIGIN` is fixed when the image is built, so the app only works at that one address: not by IP if `ORIGIN` uses a name, and not on a different port. + +If `ORIGIN` is wrong or missing over plain HTTP, pages still load, but every button that submits something fails with "Cross-site POST form submissions are forbidden". + +## Settings + +Set these in the Portainer stack's **Environment variables**, or in a `.env` file next to `docker-compose.yml` when using the command line. Never put them in the repository. + +| Variable | Required | Default | What it is | +| ----------------------------- | --------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ADMIN_USERNAME` | yes | | Login for the admin pages | +| `ADMIN_PASSWORD` | yes | | Password for the admin pages | +| `SESSION_SECRET` | yes | | Signs admin logins. Any long random string, e.g. the output of `node -e "console.log(crypto.randomBytes(32).toString('hex'))"`. Changing it logs admins out | +| `ORIGIN` | see above | | The app's address when served over plain HTTP. Build time only | +| `HOST_PORT` | no | `3000` | Port on the server | +| `SYNC_BACKEND` | no | `live` | `simulated` runs the in-memory demo instead (nothing is saved) | +| `TZ` | no | `America/Chicago` | Time zone the status pages show times in | +| `POLL_INTERVAL_SECONDS` | no | `180` | How often every project is checked for changes | +| `MANUAL_RUN_COOLDOWN_SECONDS` | no | `30` | Minimum gap between "Run now" presses for one project | +| `MAX_ATTEMPTS` | no | `3` | Failed tries before a file is reported as an issue | +| `HISTORY_RETENTION_DAYS` | no | `7` | How long sync history is kept | + +The Microsoft Graph and APS credentials will be added to this table when the live connections are built. + +## First deploy (Portainer) + +1. **Stacks → Add stack**, give it a name such as `sharepoint-acc-sync`, and choose **Repository**. +2. Enter the repository URL and the branch (`refs/heads/main`). The compose path is `docker-compose.yml`. If the repository is private, turn on authentication and use a read-only access token. +3. Under **Environment variables**, add the settings above. +4. **Deploy the stack.** Portainer builds the image from the `Dockerfile`, which takes a few minutes the first time. +5. Check it's working: + - the container shows as **healthy** in Portainer; + - the logs show `Sync server started` with the expected `backend`; + - the projects page opens at the chosen address, and the admin login works. + +From the command line on the Docker host, the equivalent is `docker compose up -d --build` in a clone of the repository, with the settings in `.env`. + +## Updating + +1. In Portainer, open the stack and use **Pull and redeploy**. Make sure the image is rebuilt, not reused: after the first update, check that the change is actually live. +2. On the command line: `git pull`, then `docker compose up -d --build`. + +When the container stops, a sync in progress gets up to 2 minutes to finish. If it doesn't finish in time, it's marked in the history as stopped by a restart, and the next sync picks up whatever was left. Nothing needs cleaning up by hand. + +## Backups + +Back up `sync.db`. Losing it means the tool forgets which ACC files it created. It would then treat every SharePoint file as new, and could not tidy up the ACC copies it made before. + +To take a copy while the server is running: + +```sh +docker exec node -e "require('better-sqlite3')('/data/sync.db').backup('/data/backup.db').then(() => console.log('backed up'))" +docker cp :/data/backup.db ./sync-backup.db +``` + +To restore one: stop the stack, put the backup in the volume as `sync.db`, delete any `sync.db-wal` and `sync.db-shm` beside it, then start the stack. + +## Troubleshooting + +| Symptom | Likely cause and fix | +| ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- | +| Deploy fails with "required variable … is missing a value" | A required setting isn't set. Add it to the stack's environment variables | +| Buttons fail with "Cross-site POST form submissions are forbidden" | Plain HTTP without the right `ORIGIN`, or the app was opened at a different address. Set `ORIGIN` and rebuild | +| Admin login appears to work but doesn't stick | Plain HTTP without `ORIGIN`: the login cookie is marked HTTPS-only, so the browser drops it. Set `ORIGIN` and rebuild | +| Container is unhealthy or keeps restarting | Check the logs. Bad settings are reported by name at startup | +| Times on the pages are off by some hours | `TZ` doesn't match the office's time zone | +| A project shows **Paused** | Its SharePoint folder was deleted or couldn't be found. Nothing was deleted in ACC. Fix the folder, then **Resume** it on the admin page | diff --git a/src/app.d.ts b/src/app.d.ts index da08e6d..2e37e53 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -3,7 +3,9 @@ declare global { namespace App { // interface Error {} - // interface Locals {} + interface Locals { + isAdmin: boolean; + } // interface PageData {} // interface PageState {} // interface Platform {} diff --git a/src/app.html b/src/app.html index 6a2bb58..ae5cd7b 100644 --- a/src/app.html +++ b/src/app.html @@ -4,6 +4,12 @@ + + + %sveltekit.head% diff --git a/src/env.ts b/src/env.ts index 5d51c08..3cd66d7 100644 --- a/src/env.ts +++ b/src/env.ts @@ -4,7 +4,30 @@ import { z } from 'zod'; const positiveInt = (fallback: number) => z.coerce.number().int().positive().default(fallback); +const optional = z + .string() + .optional() + .transform((value) => value || undefined); + export const variables = defineEnvVars({ + SYNC_BACKEND: { + description: + '"live" for real SharePoint/ACC, or "simulated" for an in-memory demo (nothing is persisted).', + schema: z.enum(['live', 'simulated']).default('live') + }, + ADMIN_USERNAME: { + description: 'Username for the admin pages. Admin pages are disabled when unset.', + schema: optional + }, + ADMIN_PASSWORD: { + description: 'Password for the admin pages.', + schema: optional + }, + SESSION_SECRET: { + description: + 'Signs admin sessions. If unset, a random secret is used and logins end on restart.', + schema: optional + }, DATABASE_PATH: { description: 'Path to the SQLite database file (mount a volume here in Docker).', schema: z.string().min(1).default('local.db') diff --git a/src/hooks.server.ts b/src/hooks.server.ts new file mode 100644 index 0000000..a64cea2 --- /dev/null +++ b/src/hooks.server.ts @@ -0,0 +1,37 @@ +import { redirect } from '@sveltejs/kit'; +import type { Handle, ServerInit } from '@sveltejs/kit/hooks'; +import { building } from '$app/env'; +import * as env from '$app/env/private'; +import { services, startApp, stopApp } from '#lib/server/app.ts'; +import { SESSION_COOKIE } from '#lib/server/auth/admin-auth.ts'; + +export const init: ServerInit = async () => { + if (building) return; + await startApp({ + backend: env.SYNC_BACKEND, + databasePath: env.DATABASE_PATH, + pollIntervalSeconds: env.POLL_INTERVAL_SECONDS, + manualRunCooldownSeconds: env.MANUAL_RUN_COOLDOWN_SECONDS, + maxAttempts: env.MAX_ATTEMPTS, + historyRetentionDays: env.HISTORY_RETENTION_DAYS, + admin: { + username: env.ADMIN_USERNAME, + password: env.ADMIN_PASSWORD, + sessionSecret: env.SESSION_SECRET + } + }); + // adapter-node emits this on SIGINT/SIGTERM; let in-flight runs finish first. + process.once('sveltekit:shutdown', () => void stopApp()); +}; + +const PUBLIC_ADMIN_PATHS = new Set(['/admin/login']); + +export const handle: Handle = async ({ event, resolve }) => { + event.locals.isAdmin = services().auth.verifyToken(event.cookies.get(SESSION_COOKIE)); + + const { pathname, search } = event.url; + if (pathname.startsWith('/admin') && !PUBLIC_ADMIN_PATHS.has(pathname) && !event.locals.isAdmin) { + redirect(303, `/admin/login?next=${encodeURIComponent(pathname + search)}`); + } + return resolve(event); +}; diff --git a/src/lib/components/AutoRefresh.svelte b/src/lib/components/AutoRefresh.svelte new file mode 100644 index 0000000..7b2c759 --- /dev/null +++ b/src/lib/components/AutoRefresh.svelte @@ -0,0 +1,13 @@ + diff --git a/src/lib/components/Badge.svelte b/src/lib/components/Badge.svelte new file mode 100644 index 0000000..660c8fc --- /dev/null +++ b/src/lib/components/Badge.svelte @@ -0,0 +1,7 @@ + + +{label} diff --git a/src/lib/components/FileTree.svelte b/src/lib/components/FileTree.svelte new file mode 100644 index 0000000..15d2152 --- /dev/null +++ b/src/lib/components/FileTree.svelte @@ -0,0 +1,179 @@ + + +
+ +
+ +{#if rows.length === 0} +

+ {attentionOnly + ? 'Everything is synced.' + : 'No files yet. They appear here after the first sync.'} +

+{:else} +
+ + + + + + + + + + + {#each rows as { node, depth } (node.sourceId)} + {@const status = statusOf(node)} + + + + + + + {/each} + +
NameStatusChanged in SharePointLast synced
+ {#if node.isFolder} + + + {:else} + + + {/if} + {node.name} + {#if node.problems > 0} + {node.problems} failed inside + {/if} +
+
+{/if} + + diff --git a/src/lib/components/ProjectFields.svelte b/src/lib/components/ProjectFields.svelte new file mode 100644 index 0000000..f9042a6 --- /dev/null +++ b/src/lib/components/ProjectFields.svelte @@ -0,0 +1,62 @@ + + +
+ + +
+ +{#if rootsLocked && current} +
+

From SharePoint: {current.source}

+

To ACC: {current.target}

+

+ These folders are locked because the project has already synced. Reset the project to change + them. +

+
+{:else} +
+ + + + Open the folder in SharePoint and paste the address from your browser. Everything inside it is + synced. + {#if current}Leave blank to keep the current folder.{/if} + +
+
+ + + + Open the destination folder in ACC Docs and paste the address from your browser. + {#if current}Leave blank to keep the current folder.{/if} + +
+{/if} diff --git a/src/lib/components/RunNowButton.svelte b/src/lib/components/RunNowButton.svelte new file mode 100644 index 0000000..1d7659f --- /dev/null +++ b/src/lib/components/RunNowButton.svelte @@ -0,0 +1,81 @@ + + +
+ async ({ result, update }) => { + if (result.type === 'success' && result.data?.run) { + const run = result.data.run as RunRequestResult; + message = MESSAGES[run.status](run); + if (run.status === 'started') startCooldown(cooldownSeconds); + if (run.status === 'cooldown') startCooldown(run.retryAfterSeconds); + } + await update({ reset: false }); + }} +> + +

{unavailable ?? message ?? ''}

+
+ + diff --git a/src/lib/components/TimeAgo.svelte b/src/lib/components/TimeAgo.svelte new file mode 100644 index 0000000..5ca4d34 --- /dev/null +++ b/src/lib/components/TimeAgo.svelte @@ -0,0 +1,14 @@ + + +{#if date} + +{:else} + {empty} +{/if} diff --git a/src/lib/format.ts b/src/lib/format.ts new file mode 100644 index 0000000..c3a78f4 --- /dev/null +++ b/src/lib/format.ts @@ -0,0 +1,80 @@ +import type { ProjectHealth } from '#lib/server/status/status-service.ts'; +import type { + EventAction, + ItemStatus, + RunCounts, + RunOutcome +} from '#lib/server/sync/core/model.ts'; + +/** Visual tone for a badge; maps to a colour token in app.css. */ +export type Tone = 'ok' | 'info' | 'warn' | 'danger' | 'muted'; + +export const HEALTH: Record = { + healthy: { label: 'Healthy', tone: 'ok' }, + running: { label: 'Syncing…', tone: 'info' }, + pending: { label: 'Changes pending', tone: 'info' }, + issues: { label: 'Needs attention', tone: 'danger' }, + paused: { label: 'Paused', tone: 'warn' }, + disabled: { label: 'Disabled', tone: 'muted' }, + 'never-run': { label: 'Not synced yet', tone: 'muted' } +}; + +export const ITEM_STATUS: Record = { + synced: { label: 'Synced', tone: 'ok' }, + pending: { label: 'Pending', tone: 'info' }, + failed: { label: 'Failed', tone: 'danger' }, + deleted: { label: 'Deleted', tone: 'muted' } +}; + +export const OUTCOME: Record = { + success: { label: 'Success', tone: 'ok' }, + partial: { label: 'Partial', tone: 'warn' }, + failed: { label: 'Failed', tone: 'danger' } +}; + +export const EVENT: Record = { + created: { label: 'Created', tone: 'ok' }, + updated: { label: 'New version', tone: 'ok' }, + renamed: { label: 'Renamed', tone: 'info' }, + moved: { label: 'Moved', tone: 'info' }, + deleted: { label: 'Deleted', tone: 'muted' }, + restored: { label: 'Restored', tone: 'ok' }, + failed: { label: 'Failed', tone: 'danger' } +}; + +/** "3 created, 1 updated" — only non-zero counts, or "No changes". */ +export function describeCounts(counts: RunCounts): string { + const parts = (Object.keys(EVENT) as EventAction[]) + .filter((action) => counts[action] > 0) + .map((action) => `${counts[action]} ${EVENT[action].label.toLowerCase()}`); + return parts.length > 0 ? parts.join(', ') : 'No changes'; +} + +const relative = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' }); +const UNITS: Array<[Intl.RelativeTimeFormatUnit, number]> = [ + ['day', 86_400_000], + ['hour', 3_600_000], + ['minute', 60_000], + ['second', 1000] +]; + +/** "3 minutes ago", "just now". */ +export function timeAgo(date: Date | null, now: Date = new Date()): string { + if (!date) return 'Never'; + const diff = date.getTime() - now.getTime(); + if (Math.abs(diff) < 10_000) return 'just now'; + const [unit, ms] = UNITS.find(([, size]) => Math.abs(diff) >= size) ?? UNITS.at(-1)!; + return relative.format(Math.round(diff / ms), unit); +} + +const dateTime = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' }); + +export function formatDateTime(date: Date | null): string { + return date ? dateTime.format(date) : '—'; +} + +export function formatDuration(start: Date, end: Date | null): string { + if (!end) return 'Running'; + const seconds = Math.max(0, Math.round((end.getTime() - start.getTime()) / 1000)); + return seconds < 60 ? `${seconds}s` : `${Math.floor(seconds / 60)}m ${seconds % 60}s`; +} diff --git a/src/lib/now.svelte.ts b/src/lib/now.svelte.ts new file mode 100644 index 0000000..d80bee2 --- /dev/null +++ b/src/lib/now.svelte.ts @@ -0,0 +1,19 @@ +import { browser } from '$app/env'; + +/** + * A shared "now" for relative times. In the browser it is reactive and ticks + * every few seconds; on the server it is simply the current time. + */ +class Now { + #tick = $state(new Date()); + + constructor(intervalMs: number) { + if (browser) setInterval(() => (this.#tick = new Date()), intervalMs); + } + + get current(): Date { + return browser ? this.#tick : new Date(); + } +} + +export const now = new Now(5000); diff --git a/src/lib/server/adapters/simulated/fake-drive.ts b/src/lib/server/adapters/simulated/fake-drive.ts index fabdea5..5df9745 100644 --- a/src/lib/server/adapters/simulated/fake-drive.ts +++ b/src/lib/server/adapters/simulated/fake-drive.ts @@ -132,7 +132,11 @@ export class FakeDrive implements ChangeSource { /** The node followed by its ancestors up to the drive root. */ private *lineage(node: DriveNode): Generator { - for (let n: DriveNode | undefined = node; n; n = n.parentId ? this.nodes.get(n.parentId) : undefined) { + for ( + let n: DriveNode | undefined = node; + n; + n = n.parentId ? this.nodes.get(n.parentId) : undefined + ) { yield n; } } diff --git a/src/lib/server/adapters/simulated/simulated-backend.spec.ts b/src/lib/server/adapters/simulated/simulated-backend.spec.ts new file mode 100644 index 0000000..1bf9715 --- /dev/null +++ b/src/lib/server/adapters/simulated/simulated-backend.spec.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from 'vitest'; +import { REJECTED_FILE, SimulatedBackend } from './simulated-backend.ts'; + +describe('SimulatedBackend', () => { + it('resolves the last segment of a pasted URL and reuses the folder', async () => { + const backend = new SimulatedBackend(); + const url = 'https://contoso.sharepoint.com/sites/A/Shared%20Documents/Tower%20B?web=1'; + + const first = await backend.resolveSourceRoot(url); + const second = await backend.resolveSourceRoot('Tower B'); + + expect(first.displayPath).toBe('Simulated SharePoint / Tower B'); + expect(second.root).toEqual(first.root); + }); + + it('seeds new SharePoint folders with sample files', async () => { + const backend = new SimulatedBackend(); + const { root } = await backend.resolveSourceRoot('Demo'); + + const names = backend.drive.filesUnder(root.rootItemId).map((id) => backend.drive.nameOf(id)); + + expect(names).toContain('A-101 Floor Plan.pdf'); + expect(names).toContain(REJECTED_FILE); + }); + + it('rejects a blank location', async () => { + await expect(new SimulatedBackend().resolveTargetRoot(' / ')).rejects.toThrow( + 'Enter a folder path or URL.' + ); + }); + + it('simulates activity as new change-feed entries', async () => { + const backend = new SimulatedBackend(() => 0.5); + const { root } = await backend.resolveSourceRoot('Demo'); + const { cursor } = await backend.drive.getChanges(root, null); + + backend.simulateActivity(); + + const { changes } = await backend.drive.getChanges(root, cursor); + expect(changes).toHaveLength(1); + }); +}); diff --git a/src/lib/server/adapters/simulated/simulated-backend.ts b/src/lib/server/adapters/simulated/simulated-backend.ts index 0491d50..11804ea 100644 --- a/src/lib/server/adapters/simulated/simulated-backend.ts +++ b/src/lib/server/adapters/simulated/simulated-backend.ts @@ -65,7 +65,9 @@ export class SimulatedBackend implements SourceRootResolver, TargetRootResolver const roots = [...this.sourceFolders.values()]; if (roots.length === 0) return; const root = this.pick(roots); - const files = this.drive.filesUnder(root).filter((id) => this.drive.nameOf(id) !== REJECTED_FILE); + const files = this.drive + .filesUnder(root) + .filter((id) => this.drive.nameOf(id) !== REJECTED_FILE); const roll = this.random(); if (files.length === 0 || roll < 0.25) { diff --git a/src/lib/server/adapters/sqlite/database.ts b/src/lib/server/adapters/sqlite/database.ts index 8cae6bd..ba1ff17 100644 --- a/src/lib/server/adapters/sqlite/database.ts +++ b/src/lib/server/adapters/sqlite/database.ts @@ -3,7 +3,7 @@ import { drizzle, type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3' import { migrate } from 'drizzle-orm/better-sqlite3/migrator'; import * as schema from './schema.ts'; -export type Db = BetterSQLite3Database; +export type Db = BetterSQLite3Database & { $client: Database.Database }; /** Opens (or creates) the database and brings its schema up to date. */ export function openDatabase(path: string, migrationsFolder = 'drizzle'): Db { diff --git a/src/lib/server/adapters/sqlite/project-repository.ts b/src/lib/server/adapters/sqlite/project-repository.ts index d34fced..2a36be3 100644 --- a/src/lib/server/adapters/sqlite/project-repository.ts +++ b/src/lib/server/adapters/sqlite/project-repository.ts @@ -73,11 +73,7 @@ export class SqliteProjectRepository implements ProjectRepository { private set(id: string, values: Partial): void { const defined = Object.entries(values).filter(([, value]) => value !== undefined); if (defined.length === 0) return; - this.db - .update(projects) - .set(Object.fromEntries(defined)) - .where(eq(projects.id, id)) - .run(); + this.db.update(projects).set(Object.fromEntries(defined)).where(eq(projects.id, id)).run(); } } diff --git a/src/lib/server/adapters/sqlite/run-repository.spec.ts b/src/lib/server/adapters/sqlite/run-repository.spec.ts index 8087697..2b17b28 100644 --- a/src/lib/server/adapters/sqlite/run-repository.spec.ts +++ b/src/lib/server/adapters/sqlite/run-repository.spec.ts @@ -25,4 +25,23 @@ describe('SqliteRunRepository', () => { expect(run).toMatchObject({ runId, outcome: null, finishedAt: null }); }); + + it('fails runs left unfinished, leaving finished ones alone', async () => { + const h = await createHarness(); + const finished = await h.sync(); + const unfinished = await h.runs.start(h.project.id, 'schedule', new Date()); + const at = new Date(); + + expect(await h.runs.failUnfinished(at, 'Interrupted')).toBe(1); + + const byId = new Map( + (await h.runs.listSince(h.project.id, new Date(0))).map((r) => [r.runId, r]) + ); + expect(byId.get(unfinished)).toMatchObject({ + outcome: 'failed', + error: 'Interrupted', + finishedAt: at + }); + expect(byId.get(finished.runId)).toMatchObject({ outcome: 'success', error: null }); + }); }); diff --git a/src/lib/server/adapters/sqlite/run-repository.ts b/src/lib/server/adapters/sqlite/run-repository.ts index 7fe25b0..c1b4d7f 100644 --- a/src/lib/server/adapters/sqlite/run-repository.ts +++ b/src/lib/server/adapters/sqlite/run-repository.ts @@ -1,4 +1,4 @@ -import { and, asc, desc, eq, gte, lt } from 'drizzle-orm'; +import { and, asc, desc, eq, gte, isNull, lt } from 'drizzle-orm'; import type { RunCounts, RunEvent, @@ -71,6 +71,14 @@ export class SqliteRunRepository implements RunRepository { async pruneBefore(cutoff: Date): Promise { return this.db.delete(runs).where(lt(runs.startedAt, cutoff)).run().changes; } + + async failUnfinished(at: Date, error: string): Promise { + return this.db + .update(runs) + .set({ finishedAt: at, outcome: 'failed', error }) + .where(isNull(runs.outcome)) + .run().changes; + } } function toRunRecord(row: RunRow): RunRecord { diff --git a/src/lib/server/app.ts b/src/lib/server/app.ts new file mode 100644 index 0000000..d76367f --- /dev/null +++ b/src/lib/server/app.ts @@ -0,0 +1,24 @@ +import { createServices, type AppConfig, type AppServices } from './container.ts'; + +// Kept on globalThis so a dev-server reload of this module can still find and +// stop the previous instance instead of leaking its timers and DB handle. +const holder = globalThis as typeof globalThis & { __syncApp?: AppServices }; + +export async function startApp(config: AppConfig): Promise { + await stopApp(); + const services = createServices(config); + holder.__syncApp = services; + await services.start(); +} + +export async function stopApp(): Promise { + const current = holder.__syncApp; + holder.__syncApp = undefined; + await current?.stop(); +} + +/** The running app's services. Only valid after `startApp` (the server `init` hook). */ +export function services(): AppServices { + if (!holder.__syncApp) throw new Error('The sync app has not been started.'); + return holder.__syncApp; +} diff --git a/src/lib/server/auth/admin-auth.ts b/src/lib/server/auth/admin-auth.ts index c18f39e..b9c7b98 100644 --- a/src/lib/server/auth/admin-auth.ts +++ b/src/lib/server/auth/admin-auth.ts @@ -4,10 +4,10 @@ import type { Clock } from '#lib/server/sync/core/ports.ts'; export const SESSION_COOKIE = 'admin_session'; export interface AdminAuthConfig { - username: string | undefined; - password: string | undefined; + username?: string; + password?: string; /** Signs session tokens. If omitted, a random one is used and sessions end on restart. */ - sessionSecret: string | undefined; + sessionSecret?: string; sessionTtlMs: number; clock: Clock; } @@ -24,6 +24,10 @@ export class AdminAuth { return Boolean(this.config.username && this.config.password); } + get sessionTtlSeconds(): number { + return Math.floor(this.config.sessionTtlMs / 1000); + } + checkCredentials(username: string, password: string): boolean { if (!this.enabled) return false; // Evaluate both so timing doesn't reveal which one was wrong. diff --git a/src/lib/server/auth/session-cookie.ts b/src/lib/server/auth/session-cookie.ts new file mode 100644 index 0000000..1387d40 --- /dev/null +++ b/src/lib/server/auth/session-cookie.ts @@ -0,0 +1,23 @@ +import type { Cookies } from '@sveltejs/kit'; +import { SESSION_COOKIE, type AdminAuth } from './admin-auth.ts'; + +export function startSession(cookies: Cookies, url: URL, auth: AdminAuth): void { + cookies.set(SESSION_COOKIE, auth.issueToken(), { + path: '/', + httpOnly: true, + sameSite: 'strict', + // Behind the VPN the app may be served over plain HTTP. + secure: url.protocol === 'https:', + maxAge: auth.sessionTtlSeconds + }); +} + +export function endSession(cookies: Cookies): void { + cookies.delete(SESSION_COOKIE, { path: '/' }); +} + +/** The `next` query parameter if it's a same-site path, otherwise the admin home. */ +export function safeNext(url: URL): string { + const next = url.searchParams.get('next'); + return next && next.startsWith('/') && !next.startsWith('//') ? next : '/admin'; +} diff --git a/src/lib/server/container.ts b/src/lib/server/container.ts new file mode 100644 index 0000000..13ea2d9 --- /dev/null +++ b/src/lib/server/container.ts @@ -0,0 +1,177 @@ +import { NotConnectedBackend } from '#lib/server/adapters/not-connected.ts'; +import { SimulatedBackend } from '#lib/server/adapters/simulated/simulated-backend.ts'; +import { openDatabase } from '#lib/server/adapters/sqlite/database.ts'; +import { SqliteItemRepository } from '#lib/server/adapters/sqlite/item-repository.ts'; +import { SqliteProjectRepository } from '#lib/server/adapters/sqlite/project-repository.ts'; +import { SqliteRunRepository } from '#lib/server/adapters/sqlite/run-repository.ts'; +import { AdminAuth } from '#lib/server/auth/admin-auth.ts'; +import { consoleLogger } from '#lib/server/logger.ts'; +import { StatusService } from '#lib/server/status/status-service.ts'; +import { SyncCoordinator } from '#lib/server/sync/coordinator.ts'; +import { SyncEngine } from '#lib/server/sync/core/engine.ts'; +import { PatternIgnorePolicy } from '#lib/server/sync/core/ignore.ts'; +import type { + ChangeSource, + DocumentTarget, + Logger, + SourceRootResolver, + TargetRootResolver +} from '#lib/server/sync/core/ports.ts'; +import { Reconciler } from '#lib/server/sync/core/reconciler.ts'; +import { ProjectService } from '#lib/server/sync/project-service.ts'; +import { IntervalScheduler } from '#lib/server/sync/scheduler.ts'; + +export interface AppConfig { + backend: 'live' | 'simulated'; + databasePath: string; + pollIntervalSeconds: number; + manualRunCooldownSeconds: number; + maxAttempts: number; + historyRetentionDays: number; + admin: { username?: string; password?: string; sessionSecret?: string }; +} + +export interface AppServices { + coordinator: SyncCoordinator; + status: StatusService; + projectService: ProjectService; + auth: AdminAuth; + config: AppConfig; + start(): Promise; + stop(): Promise; +} + +interface Connection { + source: ChangeSource; + target: DocumentTarget; + sourceResolver: SourceRootResolver; + targetResolver: TargetRootResolver; +} + +const DAY_MS = 86_400_000; +const PRUNE_INTERVAL_MS = 6 * 3_600_000; +const SIMULATED_ACTIVITY_MS = 45_000; +const ADMIN_SESSION_TTL_MS = 12 * 3_600_000; +const INTERRUPTED_RUN = + 'The server stopped before this sync finished. Anything left over is picked up by the next sync.'; + +/** The composition root: the only place concrete adapters are chosen and wired. */ +export function createServices(config: AppConfig, logger: Logger = consoleLogger): AppServices { + const clock = () => new Date(); + const simulated = config.backend === 'simulated' ? new SimulatedBackend() : null; + const connection = simulated ? simulatedConnection(simulated) : notConnected(); + + // Simulated SharePoint/ACC live in memory, so their sync state must too. + const db = openDatabase(simulated ? ':memory:' : config.databasePath); + const projects = new SqliteProjectRepository(db, clock); + const items = new SqliteItemRepository(db); + const runs = new SqliteRunRepository(db); + const { maxAttempts } = config; + + const reconciler = new Reconciler({ ...connection, items, runs, clock, maxAttempts }); + const engine = new SyncEngine({ + source: connection.source, + projects, + items, + runs, + reconciler, + ignore: new PatternIgnorePolicy(), + clock, + logger, + maxAttempts + }); + const coordinator = new SyncCoordinator({ + runner: engine, + projects, + clock, + logger, + manualCooldownMs: config.manualRunCooldownSeconds * 1000 + }); + const projectService = new ProjectService({ + projects, + items, + sourceResolver: connection.sourceResolver, + targetResolver: connection.targetResolver, + activity: coordinator, + maxAttempts + }); + const status = new StatusService({ + projects, + items, + runs, + activity: coordinator, + clock, + retentionDays: config.historyRetentionDays + }); + const auth = new AdminAuth({ + ...config.admin, + sessionTtlMs: ADMIN_SESSION_TTL_MS, + clock + }); + const scheduler = new IntervalScheduler(logger); + + return { + coordinator, + status, + projectService, + auth, + config, + + async start() { + const interrupted = await runs.failUnfinished(clock(), INTERRUPTED_RUN); + if (interrupted > 0) logger.warn('Closed syncs interrupted by a restart', { interrupted }); + + if (simulated && (await projects.list()).length === 0) { + await projectService.create({ + name: 'Demo project', + sourceInput: 'Demo Project', + targetInput: 'Demo Project' + }); + } + scheduler.every( + 'sync', + config.pollIntervalSeconds * 1000, + () => coordinator.runScheduledPass(), + { + immediate: true + } + ); + scheduler.every( + 'prune-history', + PRUNE_INTERVAL_MS, + () => runs.pruneBefore(new Date(clock().getTime() - config.historyRetentionDays * DAY_MS)), + { immediate: true } + ); + if (simulated) { + scheduler.every('simulated-activity', SIMULATED_ACTIVITY_MS, async () => + simulated.simulateActivity() + ); + } + logger.info('Sync server started', { + backend: config.backend, + pollIntervalSeconds: config.pollIntervalSeconds, + adminEnabled: auth.enabled + }); + }, + + async stop() { + scheduler.stop(); + await coordinator.drain(); + db.$client.close(); + } + }; +} + +function simulatedConnection(backend: SimulatedBackend): Connection { + return { + source: backend.drive, + target: backend.acc, + sourceResolver: backend, + targetResolver: backend + }; +} + +function notConnected(): Connection { + const backend = new NotConnectedBackend(); + return { source: backend, target: backend, sourceResolver: backend, targetResolver: backend }; +} diff --git a/src/lib/server/forms.ts b/src/lib/server/forms.ts new file mode 100644 index 0000000..82e5fd3 --- /dev/null +++ b/src/lib/server/forms.ts @@ -0,0 +1,27 @@ +import { fail, type ActionFailure } from '@sveltejs/kit'; +import { InvalidRequestError } from '#lib/server/sync/core/errors.ts'; + +/** + * Runs a form action's work, turning user-facing errors into a 400 with + * `{ error, ...echo }` so the form can show the message and keep what was + * typed. Anything else still throws. + */ +export async function attempt( + work: () => Promise, + echo?: E +): Promise> { + try { + return await work(); + } catch (error) { + if (error instanceof InvalidRequestError) { + return fail(400, { error: error.message, ...(echo as E) }); + } + throw error; + } +} + +/** A form field as a string (empty if missing or a file). */ +export function field(data: FormData, name: string): string { + const value = data.get(name); + return typeof value === 'string' ? value : ''; +} diff --git a/src/lib/server/status/status-service.spec.ts b/src/lib/server/status/status-service.spec.ts new file mode 100644 index 0000000..6cb3ca2 --- /dev/null +++ b/src/lib/server/status/status-service.spec.ts @@ -0,0 +1,70 @@ +import { describe, expect, it } from 'vitest'; +import { createHarness } from '#lib/server/sync/testing/harness.ts'; +import type { TreeNode } from './tree-view.ts'; + +const outline = (nodes: TreeNode[], depth = 0): string[] => + nodes.flatMap((node) => [ + `${' '.repeat(depth)}${node.name}${node.isFolder ? '/' : ''} [${node.status}${node.problems ? `, ${node.problems} problem` : ''}]`, + ...outline(node.children, depth + 1) + ]); + +describe('StatusService', () => { + it('reports a never-run project', async () => { + const h = await createHarness(); + const [summary] = await h.status.listProjects(); + expect(summary).toMatchObject({ health: 'never-run', issueCount: 0, lastRun: null }); + }); + + it('builds a sorted tree with folder problem counts and lists item issues', async () => { + const h = await createHarness({ maxAttempts: 1 }); + const docs = h.drive.addFolder(h.rootId, 'Docs'); + h.drive.addFile(docs, 'b.pdf'); + h.drive.addFile(docs, 'a 10.pdf'); + h.drive.addFile(docs, 'a 9.pdf'); + h.drive.addFile(h.rootId, 'readme.txt'); + h.drive.addFile(h.rootId, '~$readme.txt'); + h.acc.failOn('b.pdf', new Error('Rejected')); + await h.sync(); + + const detail = (await h.status.getProject(h.project.id))!; + + expect(outline(detail.tree)).toEqual([ + 'Docs/ [synced, 1 problem]', + ' a 9.pdf [synced]', + ' a 10.pdf [synced]', + ' b.pdf [failed]', + 'readme.txt [synced]' + ]); + expect(detail).toMatchObject({ health: 'issues', issueCount: 1 }); + expect(detail.issues).toEqual([ + expect.objectContaining({ path: 'Docs/b.pdf', message: 'Rejected', attempts: 1 }) + ]); + }); + + it('surfaces a paused project as a project-level issue', async () => { + const h = await createHarness(); + await h.projects.pause(h.project.id, 'Root deleted'); + + const detail = (await h.status.getProject(h.project.id))!; + + expect(detail.health).toBe('paused'); + expect(detail.issues).toEqual([ + expect.objectContaining({ sourceId: null, message: 'Paused: Root deleted' }) + ]); + }); + + it('reports running projects as running', async () => { + const h = await createHarness(); + h.running.add(h.project.id); + expect((await h.status.listProjects())[0]).toMatchObject({ running: true, health: 'running' }); + }); + + it('only shows events for runs of the requested project', async () => { + const h = await createHarness(); + h.drive.addFile(h.rootId, 'a.txt'); + const run = await h.sync(); + + expect(await h.status.runEvents(h.project.id, run.runId)).toHaveLength(1); + expect(await h.status.runEvents('other-project', run.runId)).toEqual([]); + }); +}); diff --git a/src/lib/server/status/status-service.ts b/src/lib/server/status/status-service.ts index 6f6cb7a..b4ddb21 100644 --- a/src/lib/server/status/status-service.ts +++ b/src/lib/server/status/status-service.ts @@ -1,9 +1,4 @@ -import type { - Project, - RunEvent, - RunRecord, - StatusCounts -} from '#lib/server/sync/core/model.ts'; +import type { Project, RunEvent, RunRecord, StatusCounts } from '#lib/server/sync/core/model.ts'; import type { Clock, ItemRepository, @@ -14,13 +9,7 @@ import { ItemTree } from '#lib/server/sync/core/tree.ts'; import { buildTree, type TreeNode } from './tree-view.ts'; export type ProjectHealth = - | 'disabled' - | 'paused' - | 'running' - | 'issues' - | 'pending' - | 'never-run' - | 'healthy'; + 'disabled' | 'paused' | 'running' | 'issues' | 'pending' | 'never-run' | 'healthy'; export interface ProjectSummary { project: Project; @@ -63,6 +52,11 @@ export class StatusService { return Promise.all(projects.map((project) => this.summarize(project))); } + async getSummary(id: string): Promise { + const project = await this.deps.projects.get(id); + return project ? this.summarize(project) : null; + } + async getProject(id: string): Promise { const project = await this.deps.projects.get(id); if (!project) return null; diff --git a/src/lib/server/sync/core/ports.ts b/src/lib/server/sync/core/ports.ts index db42633..82059ab 100644 --- a/src/lib/server/sync/core/ports.ts +++ b/src/lib/server/sync/core/ports.ts @@ -108,6 +108,8 @@ export interface RunRepository { listEvents(runId: string): Promise; /** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */ pruneBefore(cutoff: Date): Promise; + /** Close every run left unfinished (e.g. by a restart) as failed; returns how many were closed. */ + failUnfinished(at: Date, error: string): Promise; } export type Clock = () => Date; diff --git a/src/lib/server/sync/project-service.spec.ts b/src/lib/server/sync/project-service.spec.ts new file mode 100644 index 0000000..40a9dad --- /dev/null +++ b/src/lib/server/sync/project-service.spec.ts @@ -0,0 +1,96 @@ +import { describe, expect, it } from 'vitest'; +import { createHarness } from './testing/harness.ts'; +import { BUSY, ROOTS_LOCKED } from './project-service.ts'; + +describe('ProjectService', () => { + it('creates a project from pasted locations', async () => { + const h = await createHarness(); + + const project = await h.projectService.create({ + name: ' Tower B ', + sourceInput: 'Tower B', + targetInput: 'Tower B Docs' + }); + + expect(project).toMatchObject({ + name: 'Tower B', + enabled: true, + sourceDisplayPath: 'SharePoint/Tower B', + targetDisplayPath: 'ACC/Tower B Docs' + }); + }); + + it('rejects blank names and locations', async () => { + const h = await createHarness(); + await expect( + h.projectService.create({ name: ' ', sourceInput: 'a', targetInput: 'b' }) + ).rejects.toThrow('Name is required.'); + await expect( + h.projectService.create({ name: 'x', sourceInput: '', targetInput: 'b' }) + ).rejects.toThrow('SharePoint folder is required.'); + }); + + it('lets roots change before the first sync but locks them afterwards', async () => { + const h = await createHarness(); + const edit = { name: 'Project X', enabled: true, sourceInput: 'Elsewhere' }; + + await h.projectService.update(h.project.id, edit); + expect((await h.projects.get(h.project.id))!.sourceDisplayPath).toBe('SharePoint/Elsewhere'); + + await h.engine.runProject(h.project.id, 'manual'); + await expect(h.projectService.update(h.project.id, edit)).rejects.toThrow(ROOTS_LOCKED); + + await h.projectService.update(h.project.id, { name: 'Renamed', enabled: false }); + expect(await h.projects.get(h.project.id)).toMatchObject({ name: 'Renamed', enabled: false }); + }); + + it('unlocks roots and forgets sync state on reset', async () => { + const h = await createHarness(); + h.drive.addFile(h.rootId, 'a.txt'); + await h.sync(); + + await h.projectService.reset(h.project.id); + + expect(await h.projects.get(h.project.id)).toMatchObject({ + firstSyncedAt: null, + deltaCursor: null + }); + expect(await h.items.listByProject(h.project.id)).toEqual([]); + }); + + it('refuses to reset, delete or retry while a run is in progress', async () => { + const h = await createHarness(); + h.running.add(h.project.id); + + await expect(h.projectService.reset(h.project.id)).rejects.toThrow(BUSY); + await expect(h.projectService.delete(h.project.id)).rejects.toThrow(BUSY); + await expect(h.projectService.retryItem(h.project.id, 'x')).rejects.toThrow(BUSY); + }); + + it('deletes a project with its items and history', async () => { + const h = await createHarness(); + h.drive.addFile(h.rootId, 'a.txt'); + const run = await h.sync(); + + await h.projectService.delete(h.project.id); + + expect(await h.projects.get(h.project.id)).toBeNull(); + expect(await h.items.listByProject(h.project.id)).toEqual([]); + expect(await h.runs.listEvents(run.runId)).toEqual([]); + }); + + it('gives a failed item fresh attempts on retry', async () => { + const h = await createHarness({ maxAttempts: 1 }); + h.drive.addFile(h.rootId, 'bad.dwg'); + h.acc.failOn('bad.dwg', new Error('nope'), 1); + await h.sync(); + const failed = await h.itemNamed('bad.dwg'); + expect(failed.status).toBe('failed'); + + await h.projectService.retryItem(h.project.id, failed.sourceId); + expect(await h.itemNamed('bad.dwg')).toMatchObject({ status: 'pending', attemptCount: 0 }); + + await h.sync(); + expect((await h.itemNamed('bad.dwg')).status).toBe('synced'); + }); +}); diff --git a/src/lib/server/sync/testing/harness.ts b/src/lib/server/sync/testing/harness.ts index 8d8d75b..30cd203 100644 --- a/src/lib/server/sync/testing/harness.ts +++ b/src/lib/server/sync/testing/harness.ts @@ -4,8 +4,14 @@ import { SqliteProjectRepository } from '#lib/server/adapters/sqlite/project-rep import { SqliteRunRepository } from '#lib/server/adapters/sqlite/run-repository.ts'; import { SyncEngine } from '#lib/server/sync/core/engine.ts'; import { PatternIgnorePolicy } from '#lib/server/sync/core/ignore.ts'; -import type { Logger } from '#lib/server/sync/core/ports.ts'; +import type { + Logger, + SourceRootResolver, + TargetRootResolver +} from '#lib/server/sync/core/ports.ts'; import { Reconciler } from '#lib/server/sync/core/reconciler.ts'; +import { ProjectService } from '#lib/server/sync/project-service.ts'; +import { StatusService } from '#lib/server/status/status-service.ts'; import { FakeAcc } from '#lib/server/adapters/simulated/fake-acc.ts'; import { FakeDrive } from '#lib/server/adapters/simulated/fake-drive.ts'; @@ -53,6 +59,40 @@ export async function createHarness({ maxAttempts = 3 } = {}) { targetDisplayPath: 'Project X/Project Files' }); + /** Stands in for the coordinator: tests add project IDs to mark them as running. */ + const running = new Set(); + const activity = { isRunning: (id: string) => running.has(id) }; + + /** Resolves any input to a new folder of that name on each side. */ + const resolver: SourceRootResolver & TargetRootResolver = { + resolveSourceRoot: async (input) => ({ + root: { + siteId: 'site', + driveId: 'drive', + rootItemId: drive.addFolder(drive.driveRootId, input) + }, + displayPath: `SharePoint/${input}` + }), + resolveTargetRoot: async (input) => ({ + root: { + hubId: 'hub', + projectId: 'acc-project', + rootFolderId: acc.addFolderByHand(acc.rootFolderId, input) + }, + displayPath: `ACC/${input}` + }) + }; + + const projectService = new ProjectService({ + projects, + items, + sourceResolver: resolver, + targetResolver: resolver, + activity, + maxAttempts + }); + const status = new StatusService({ projects, items, runs, activity, clock, retentionDays: 7 }); + return { db, drive, @@ -62,6 +102,9 @@ export async function createHarness({ maxAttempts = 3 } = {}) { items, runs, engine, + projectService, + status, + running, project, sync: () => engine.runProject(project.id, 'schedule'), itemNamed: async (name: string) => diff --git a/src/lib/styles/app.css b/src/lib/styles/app.css new file mode 100644 index 0000000..30b131d --- /dev/null +++ b/src/lib/styles/app.css @@ -0,0 +1,316 @@ +/* + * Drafting-room palette: paper, graphite and rule lines, one marker blue for + * actions, and signal colours reserved for status. + */ +:root { + --paper: #fafaf7; + --sheet: #ffffff; + --graphite: #23272c; + --pencil: #5d6670; + --rule: #d3d7dc; + --rule-strong: #23272c; + --marker: #1d5ba6; + --marker-ink: #ffffff; + --ok: #1f7a4d; + --info: #1d5ba6; + --warn: #a86a12; + --danger: #bf3a30; + --muted: #6b737c; + --tint: 12%; + + --font-ui: 'Barlow', system-ui, sans-serif; + --font-display: 'Barlow Semi Condensed', 'Barlow', system-ui, sans-serif; + --step--1: 0.8125rem; + --step-0: 0.9375rem; + --step-1: 1.125rem; + --step-2: 1.5rem; + --step-3: 2.25rem; + --radius: 3px; + --gutter: clamp(1rem, 3vw, 2rem); + + color-scheme: light; +} + +@media (prefers-color-scheme: dark) { + :root { + --paper: #181b1f; + --sheet: #202429; + --graphite: #e4e7ea; + --pencil: #a3abb4; + --rule: #363c43; + --rule-strong: #8b949e; + --marker: #7fb0ec; + --marker-ink: #10243c; + --ok: #5cc28d; + --info: #7fb0ec; + --warn: #e0a64a; + --danger: #f07c72; + --muted: #8b949e; + --tint: 18%; + color-scheme: dark; + } +} + +*, +*::before, +*::after { + box-sizing: border-box; +} + +html { + -webkit-text-size-adjust: 100%; +} + +body { + margin: 0; + background: var(--paper); + color: var(--graphite); + font-family: var(--font-ui); + font-size: var(--step-0); + line-height: 1.5; +} + +h1, +h2, +h3 { + font-family: var(--font-display); + font-weight: 600; + line-height: 1.15; + margin: 0; +} + +h1 { + font-size: var(--step-3); + letter-spacing: -0.01em; +} + +h2 { + font-size: var(--step-2); +} + +h3 { + font-size: var(--step-1); +} + +p { + margin: 0; +} + +a { + color: var(--marker); + text-underline-offset: 0.2em; +} + +:focus-visible { + outline: 2px solid var(--marker); + outline-offset: 2px; +} + +.page { + max-width: 76rem; + margin: 0 auto; + padding: var(--gutter); + display: grid; + gap: 2rem; +} + +.section { + display: grid; + gap: 0.75rem; +} + +.section-head { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 1rem; + flex-wrap: wrap; +} + +.muted { + color: var(--pencil); +} + +.small { + font-size: var(--step--1); +} + +/* Buttons */ + +.button { + display: inline-flex; + align-items: center; + gap: 0.4rem; + min-height: 2.25rem; + padding: 0.35rem 0.9rem; + border: 1px solid var(--rule-strong); + border-radius: var(--radius); + background: var(--sheet); + color: var(--graphite); + font: 500 var(--step-0) / 1 var(--font-ui); + text-decoration: none; + cursor: pointer; +} + +.button:hover { + background: color-mix(in srgb, var(--graphite) 6%, var(--sheet)); +} + +.button.primary { + background: var(--marker); + border-color: var(--marker); + color: var(--marker-ink); +} + +.button.primary:hover { + background: color-mix(in srgb, var(--marker) 88%, black); +} + +.button.danger { + color: var(--danger); + border-color: var(--danger); +} + +.button.compact { + min-height: 1.75rem; + padding: 0.2rem 0.6rem; + font-size: var(--step--1); +} + +.button:disabled { + opacity: 0.55; + cursor: not-allowed; +} + +/* Status badges: a tinted chip with a solid dot, colour from --tone. */ + +.badge { + --tone: var(--muted); + display: inline-flex; + align-items: center; + gap: 0.4em; + padding: 0.1em 0.55em; + border-radius: 999px; + background: color-mix(in srgb, var(--tone) var(--tint), transparent); + color: var(--tone); + font-size: var(--step--1); + font-weight: 600; + white-space: nowrap; +} + +.badge::before { + content: ''; + width: 0.5em; + height: 0.5em; + border-radius: 50%; + background: currentColor; +} + +.tone-ok { + --tone: var(--ok); +} +.tone-info { + --tone: var(--info); +} +.tone-warn { + --tone: var(--warn); +} +.tone-danger { + --tone: var(--danger); +} +.tone-muted { + --tone: var(--muted); +} + +/* Register tables: the drawing-index look used for lists. */ + +.register { + width: 100%; + border-collapse: collapse; + background: var(--sheet); + border: 1px solid var(--rule); +} + +.register th { + text-align: left; + font-weight: 600; + font-size: var(--step--1); + color: var(--pencil); + padding: 0.5rem 0.75rem; + border-bottom: 1px solid var(--rule-strong); +} + +.register td { + padding: 0.55rem 0.75rem; + border-bottom: 1px solid var(--rule); + vertical-align: top; +} + +.register tr:last-child td { + border-bottom: 0; +} + +.table-wrap { + overflow-x: auto; +} + +/* Forms */ + +.form { + display: grid; + gap: 1.1rem; + max-width: 40rem; +} + +.field { + display: grid; + gap: 0.3rem; +} + +.field label { + font-weight: 600; +} + +.field input[type='text'], +.field input[type='password'], +.field input[type='url'] { + width: 100%; + padding: 0.5rem 0.65rem; + border: 1px solid var(--rule-strong); + border-radius: var(--radius); + background: var(--sheet); + color: var(--graphite); + font: inherit; +} + +.field .hint { + font-size: var(--step--1); + color: var(--pencil); +} + +.check { + display: flex; + align-items: center; + gap: 0.5rem; +} + +.notice { + --tone: var(--info); + padding: 0.65rem 0.9rem; + border-left: 3px solid var(--tone); + background: color-mix(in srgb, var(--tone) var(--tint), var(--sheet)); +} + +.actions { + display: flex; + gap: 0.6rem; + flex-wrap: wrap; + align-items: center; +} + +@media (prefers-reduced-motion: reduce) { + * { + animation: none !important; + transition: none !important; + } +} diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts new file mode 100644 index 0000000..eb5b5fc --- /dev/null +++ b/src/routes/+layout.server.ts @@ -0,0 +1,7 @@ +import { services } from '#lib/server/app.ts'; +import type { LayoutServerLoad } from './$types'; + +export const load: LayoutServerLoad = ({ locals }) => ({ + isAdmin: locals.isAdmin, + simulated: services().config.backend === 'simulated' +}); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 7a8b5d1..5a1d5f9 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,12 +1,101 @@ + SharePoint to ACC sync -{@render children()} +
+ SharePoint to ACC sync + +
+ +{#if data.simulated} +
+ Simulated mode: SharePoint and ACC are in-memory stand-ins with sample files. Nothing here is + real, and everything resets when the server restarts. +
+{/if} + +
+ {@render children()} +
+ + diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts new file mode 100644 index 0000000..93cbd57 --- /dev/null +++ b/src/routes/+page.server.ts @@ -0,0 +1,6 @@ +import { services } from '#lib/server/app.ts'; +import type { PageServerLoad } from './$types'; + +export const load: PageServerLoad = async () => ({ + projects: await services().status.listProjects() +}); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index cc88df0..dbc06ce 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,2 +1,83 @@ -

Welcome to SvelteKit

-

Visit svelte.dev/docs/kit to read the documentation

+ + + + Projects | SharePoint to ACC sync + + + + +
+
+

Projects

+

Each project mirrors one SharePoint folder into one ACC folder.

+
+ + {#if data.projects.length === 0} +
+ No projects yet. An admin can add one from the admin page. +
+ {:else} +
+ + + + + + + + + + + {#each data.projects as summary (summary.project.id)} + {@const { project, counts } = summary} + + + + + + + {/each} + +
ProjectStatusFilesLast sync
+ {project.name} +
{project.sourceDisplayPath}
+
+ + {#if summary.issueCount > 0} +
+ {summary.issueCount} + {summary.issueCount === 1 ? 'issue' : 'issues'} +
+ {/if} +
+ {counts.synced} synced{#if counts.pending}, {counts.pending} pending{/if}{#if counts.failed}, + {counts.failed} failed{/if} +
+
+ {/if} +
+ + diff --git a/src/routes/admin/+page.server.ts b/src/routes/admin/+page.server.ts new file mode 100644 index 0000000..f80645d --- /dev/null +++ b/src/routes/admin/+page.server.ts @@ -0,0 +1,15 @@ +import { redirect } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import { endSession } from '#lib/server/auth/session-cookie.ts'; +import type { Actions, PageServerLoad } from './$types'; + +export const load: PageServerLoad = async () => ({ + projects: await services().status.listProjects() +}); + +export const actions: Actions = { + logout: ({ cookies }) => { + endSession(cookies); + redirect(303, '/'); + } +}; diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte new file mode 100644 index 0000000..2fb6db6 --- /dev/null +++ b/src/routes/admin/+page.svelte @@ -0,0 +1,55 @@ + + + + Admin | SharePoint to ACC sync + + +
+
+

Manage projects

+
+ Add project +
+ +
+
+
+ + {#if data.projects.length === 0} +
+ No projects yet. Add one to start syncing a SharePoint folder into ACC. +
+ {:else} +
+ + + + + + + + + + + {#each data.projects as { project, health } (project.id)} + + + + + + + {/each} + +
ProjectFrom SharePointTo ACCStatus
+ {project.name} + {project.sourceDisplayPath}{project.targetDisplayPath}
+
+ {/if} +
diff --git a/src/routes/admin/login/+page.server.ts b/src/routes/admin/login/+page.server.ts new file mode 100644 index 0000000..20c4a38 --- /dev/null +++ b/src/routes/admin/login/+page.server.ts @@ -0,0 +1,23 @@ +import { fail, redirect } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import { safeNext, startSession } from '#lib/server/auth/session-cookie.ts'; +import { field } from '#lib/server/forms.ts'; +import type { Actions, PageServerLoad } from './$types'; + +export const load: PageServerLoad = ({ locals, url }) => { + if (locals.isAdmin) redirect(303, safeNext(url)); + return { enabled: services().auth.enabled }; +}; + +export const actions: Actions = { + default: async ({ request, cookies, url }) => { + const data = await request.formData(); + const username = field(data, 'username'); + const { auth } = services(); + if (!auth.checkCredentials(username, field(data, 'password'))) { + return fail(400, { error: 'That username and password combination is incorrect.', username }); + } + startSession(cookies, url, auth); + redirect(303, safeNext(url)); + } +}; diff --git a/src/routes/admin/login/+page.svelte b/src/routes/admin/login/+page.svelte new file mode 100644 index 0000000..550a53f --- /dev/null +++ b/src/routes/admin/login/+page.svelte @@ -0,0 +1,54 @@ + + + + Admin login | SharePoint to ACC sync + + +
+
+

Admin login

+

Admins can add projects, change their settings, and reset or remove them.

+
+ + {#if !data.enabled} +
+ Admin login is turned off. Set ADMIN_USERNAME and ADMIN_PASSWORD on the + server to turn it on. +
+ {:else} +
+ {#if form?.error} + + {/if} +
+ + +
+
+ + +
+
+ +
+
+ {/if} +
diff --git a/src/routes/admin/projects/[id]/+page.server.ts b/src/routes/admin/projects/[id]/+page.server.ts new file mode 100644 index 0000000..5f22155 --- /dev/null +++ b/src/routes/admin/projects/[id]/+page.server.ts @@ -0,0 +1,47 @@ +import { error, redirect } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import { attempt, field } from '#lib/server/forms.ts'; +import type { Actions, PageServerLoad } from './$types'; + +export const load: PageServerLoad = async ({ params }) => { + const summary = await services().status.getSummary(params.id); + if (!summary) error(404, 'Project not found'); + return { summary }; +}; + +export const actions: Actions = { + save: async ({ params, request }) => { + const data = await request.formData(); + const values = { + name: field(data, 'name'), + enabled: data.has('enabled'), + sourceInput: field(data, 'sourceInput'), + targetInput: field(data, 'targetInput') + }; + return attempt( + async () => { + await services().projectService.update(params.id, values); + return { saved: true }; + }, + { values } + ); + }, + + resume: ({ params }) => + attempt(async () => { + await services().projectService.resume(params.id); + return { resumed: true }; + }), + + reset: ({ params }) => + attempt(async () => { + await services().projectService.reset(params.id); + return { reset: true }; + }), + + delete: ({ params }) => + attempt(async () => { + await services().projectService.delete(params.id); + redirect(303, '/admin'); + }) +}; diff --git a/src/routes/admin/projects/[id]/+page.svelte b/src/routes/admin/projects/[id]/+page.svelte new file mode 100644 index 0000000..037eddc --- /dev/null +++ b/src/routes/admin/projects/[id]/+page.svelte @@ -0,0 +1,148 @@ + + + + Edit {project.name} | SharePoint to ACC sync + + +
+
+ Back to projects +

{project.name}

+ View sync status +
+ + {#if form?.error} + + {:else if form && 'saved' in form} +
Changes saved.
+ {:else if form && 'reset' in form} +
+ Project reset. The next sync reads the SharePoint folder from scratch. +
+ {:else if form && 'resumed' in form} +
Project resumed. It syncs again on the next run.
+ {/if} + + {#if project.pausedReason} +
+

Paused

+
+

{project.pausedReason}

+

+ Nothing in ACC was changed. If the SharePoint folder is back, resume the project. If it + moved for good, reset the project and point it at the new folder. +

+
+
+ +
+
+ {/if} + +
+

Settings

+
+ ({ update }) => + update({ reset: false })} + > + + +
+ +
+ +
+ +
+

Reset or remove

+
+
+

Reset project

+

+ Forgets what has been synced and unlocks the folder settings. Files already in ACC stay + there; the next sync matches SharePoint again from scratch. +

+
+
+ +
+
+
+
+

Remove project

+

+ Stops syncing and deletes this project's history. Nothing is deleted from SharePoint or + ACC. +

+
+
+ +
+
+
+
+ + diff --git a/src/routes/admin/projects/new/+page.server.ts b/src/routes/admin/projects/new/+page.server.ts new file mode 100644 index 0000000..fdd5219 --- /dev/null +++ b/src/routes/admin/projects/new/+page.server.ts @@ -0,0 +1,22 @@ +import { redirect } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import { attempt, field } from '#lib/server/forms.ts'; +import type { Actions } from './$types'; + +export const actions: Actions = { + default: async ({ request }) => { + const data = await request.formData(); + const values = { + name: field(data, 'name'), + sourceInput: field(data, 'sourceInput'), + targetInput: field(data, 'targetInput') + }; + return attempt( + async () => { + const project = await services().projectService.create(values); + redirect(303, `/projects/${project.id}`); + }, + { values } + ); + } +}; diff --git a/src/routes/admin/projects/new/+page.svelte b/src/routes/admin/projects/new/+page.svelte new file mode 100644 index 0000000..5ea8a00 --- /dev/null +++ b/src/routes/admin/projects/new/+page.svelte @@ -0,0 +1,47 @@ + + + + Add project | SharePoint to ACC sync + + +
+
+ Back to projects +

Add project

+

+ Pick the SharePoint folder to copy from and the ACC folder to copy into. The first sync starts + within a few minutes, or right away with Run now. +

+
+ +
{ + saving = true; + return async ({ update }) => { + await update({ reset: false }); + saving = false; + }; + }} + > + {#if form?.error} + + {/if} + +
+ + Cancel +
+ +
diff --git a/src/routes/healthz/+server.ts b/src/routes/healthz/+server.ts new file mode 100644 index 0000000..d41536b --- /dev/null +++ b/src/routes/healthz/+server.ts @@ -0,0 +1,9 @@ +import { json } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import type { RequestHandler } from './$types'; + +/** Liveness check for Docker's HEALTHCHECK: answers once the sync app has started. */ +export const GET: RequestHandler = () => { + services(); + return json({ status: 'ok' }, { headers: { 'cache-control': 'no-store' } }); +}; diff --git a/src/routes/projects/[id]/+page.server.ts b/src/routes/projects/[id]/+page.server.ts new file mode 100644 index 0000000..1ddeb17 --- /dev/null +++ b/src/routes/projects/[id]/+page.server.ts @@ -0,0 +1,25 @@ +import { error } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import { attempt, field } from '#lib/server/forms.ts'; +import type { Actions, PageServerLoad } from './$types'; + +export const load: PageServerLoad = async ({ params }) => { + const { status, config } = services(); + const detail = await status.getProject(params.id); + if (!detail) error(404, 'Project not found'); + return { detail, cooldownSeconds: config.manualRunCooldownSeconds }; +}; + +export const actions: Actions = { + run: async ({ params }) => ({ + run: await services().coordinator.requestManualRun(params.id) + }), + + retry: async ({ params, request }) => { + const sourceId = field(await request.formData(), 'sourceId'); + return attempt(async () => { + await services().projectService.retryItem(params.id, sourceId); + return { retried: sourceId }; + }); + } +}; diff --git a/src/routes/projects/[id]/+page.svelte b/src/routes/projects/[id]/+page.svelte new file mode 100644 index 0000000..bb0ee8d --- /dev/null +++ b/src/routes/projects/[id]/+page.svelte @@ -0,0 +1,193 @@ + + + + {project.name} | SharePoint to ACC sync + + + + +
+
+
+
+

{project.name}

+ +
+ +
+
+
+
From SharePoint
+
{project.sourceDisplayPath}
+
+
+
To ACC
+
{project.targetDisplayPath}
+
+
+
Last sync
+
+ {#if detail.lastRun} + + {#if detail.lastRun.outcome} + + {/if} +
{describeCounts(detail.lastRun.counts)}
+ {:else} + Not synced yet + {/if} + See the last 7 days +
+
+
+
Files
+
+ {detail.counts.synced} synced +
+ {detail.counts.pending} pending, {detail.counts.failed} failed +
+
+
+
+
+ + {#if detail.issues.length > 0} +
+

Issues

+ {#if form?.error} + + {/if} +
    + {#each detail.issues as issue (issue.sourceId ?? issue.message)} +
  • +
    + {issue.path} +

    {issue.message}

    + {#if issue.sourceId} +

    + Tried {issue.attempts} + {issue.attempts === 1 ? 'time' : 'times'}, last . It won't be tried again until it changes in SharePoint or you retry it. +

    + {/if} +
    + {#if issue.sourceId} +
    + + +
    + {/if} +
  • + {/each} +
+
+ {/if} + +
+

Files

+ +
+
+ + diff --git a/src/routes/projects/[id]/history/+page.server.ts b/src/routes/projects/[id]/history/+page.server.ts new file mode 100644 index 0000000..fe2a2ac --- /dev/null +++ b/src/routes/projects/[id]/history/+page.server.ts @@ -0,0 +1,15 @@ +import { error } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import type { PageServerLoad } from './$types'; + +export const load: PageServerLoad = async ({ params }) => { + const { status, config } = services(); + const summary = await status.getSummary(params.id); + if (!summary) error(404, 'Project not found'); + return { + project: summary.project, + running: summary.running, + runs: await status.history(params.id), + retentionDays: config.historyRetentionDays + }; +}; diff --git a/src/routes/projects/[id]/history/+page.svelte b/src/routes/projects/[id]/history/+page.svelte new file mode 100644 index 0000000..18c991f --- /dev/null +++ b/src/routes/projects/[id]/history/+page.svelte @@ -0,0 +1,84 @@ + + + + History for {data.project.name} | SharePoint to ACC sync + + + + +
+
+ + Back to {data.project.name} + +

Sync history

+

+ Every sync of {data.project.name} in the last {data.retentionDays} days. Older runs are removed + automatically. +

+
+ + {#if data.runs.length === 0} +
No syncs in the last {data.retentionDays} days.
+ {:else} +
+ + + + + + + + + + + + {#each data.runs as run (run.runId)} + + + + + + + + {/each} + +
StartedTriggered byResultChangesTook
+ + {formatDateTime(run.startedAt)} + +
+
{run.trigger === 'manual' ? 'Run now' : 'Schedule'} + {#if run.outcome} + + {:else} + + {/if} + {#if run.error} +
{run.error}
+ {/if} +
{describeCounts(run.counts)}{formatDuration(run.startedAt, run.finishedAt)}
+
+ {/if} +
+ + diff --git a/src/routes/projects/[id]/history/[runId]/+page.server.ts b/src/routes/projects/[id]/history/[runId]/+page.server.ts new file mode 100644 index 0000000..481ef01 --- /dev/null +++ b/src/routes/projects/[id]/history/[runId]/+page.server.ts @@ -0,0 +1,15 @@ +import { error } from '@sveltejs/kit'; +import { services } from '#lib/server/app.ts'; +import type { PageServerLoad } from './$types'; + +export const load: PageServerLoad = async ({ params }) => { + const { status } = services(); + const [summary, runs, events] = await Promise.all([ + status.getSummary(params.id), + status.history(params.id), + status.runEvents(params.id, params.runId) + ]); + const run = runs.find((r) => r.runId === params.runId); + if (!summary || !run) error(404, 'Sync run not found. It may be older than the history window.'); + return { project: summary.project, run, events }; +}; diff --git a/src/routes/projects/[id]/history/[runId]/+page.svelte b/src/routes/projects/[id]/history/[runId]/+page.svelte new file mode 100644 index 0000000..e59f23a --- /dev/null +++ b/src/routes/projects/[id]/history/[runId]/+page.svelte @@ -0,0 +1,72 @@ + + + + Sync on {formatDateTime(data.run.startedAt)} | {data.project.name} + + +
+
+ + Back to sync history + +

Sync on {formatDateTime(data.run.startedAt)}

+
+ {#if data.run.outcome} + + {/if} + + {data.run.trigger === 'manual' ? 'Started with Run now' : 'Started by the schedule'}, took + {formatDuration(data.run.startedAt, data.run.finishedAt)}. {describeCounts( + data.run.counts + )}. + +
+ {#if data.run.error} +
{data.run.error}
+ {/if} +
+ + {#if data.events.length === 0} +
Nothing needed to change in ACC during this sync.
+ {:else} +
+ + + + + + + + + + {#each data.events as event, i (i)} + + + + + + {/each} + +
TimeWhat happenedFile
{timeOnly.format(event.at)} + {event.path} + {#if event.message} +
{event.message}
+ {/if} +
+
+ {/if} +
+ + diff --git a/vite.config.ts b/vite.config.ts index 51fcb1c..07ffa0f 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -10,7 +10,13 @@ export default defineConfig({ runes: ({ filename }) => filename.split(/[/\\]/).includes('node_modules') ? undefined : true }, - adapter: adapter() + adapter: adapter(), + paths: { + // Kit 3 fixes the site's address at build time. When unset, requests are assumed to + // arrive over HTTPS (e.g. through a reverse proxy). Set ORIGIN (e.g. http://sync:3000) + // when serving plain HTTP, or form posts are rejected as cross-site. + origin: process.env.ORIGIN || undefined + } }) ], test: {