Compare commits
6 commits
main
...
simplify-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69ad95a7b2 | ||
|
|
cd216cd660 | ||
|
|
b87c4554fb | ||
|
|
4ceb1ff223 | ||
|
|
bbf3bfc48a | ||
|
|
29cd2f046d |
7 changed files with 151 additions and 279 deletions
42
README.md
42
README.md
|
|
@ -54,23 +54,23 @@ myst config init
|
||||||
|
|
||||||
Prompts for:
|
Prompts for:
|
||||||
|
|
||||||
- Public viewer URL (`share.example.com`)
|
- Forgejo API URL — Docker: `http://forgejo:3000`, bare metal: `http://localhost:3000`, cross-host: `https://git.example.com`
|
||||||
- Private admin URL (`admin.example.com`)
|
- Forgejo PAT (with `read:user` and `read:repository` scopes)
|
||||||
- Forgejo base URL (`git.example.com`)
|
|
||||||
- PostgreSQL connection string
|
|
||||||
- Dedicated Forgejo username and PAT
|
|
||||||
- Grant token secret
|
|
||||||
|
|
||||||
### 2. Configure Dedicated Forgejo User
|
### 2. Deploy with Docker Compose
|
||||||
|
|
||||||
Create a dedicated Forgejo user (e.g., `myst-bot`) with a PAT:
|
```bash
|
||||||
|
cd packages/cli/examples
|
||||||
|
cp docker-compose.example.yml docker-compose.yml
|
||||||
|
# Edit .env with your FORGEJO_BASE_URL and FORGEJO_PAT
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
- `read:user`
|
The DATABASE_URL is injected automatically by Docker Compose.
|
||||||
- `read:repository`
|
|
||||||
|
|
||||||
### 3. Deploy
|
### 3. Alternative Deployments
|
||||||
|
|
||||||
Deploy Myst anywhere you can run a normal web app beside Forgejo. Phase 1 should explicitly support both of these examples:
|
For non-Docker deployments, you must supply DATABASE_URL yourself:
|
||||||
|
|
||||||
- VPS deployment: Hostinger or another VPS with Dokploy, Traefik, UFW, Myst, Forgejo, and a shared Postgres instance that contains separate `forgejo` and `myst` databases.
|
- VPS deployment: Hostinger or another VPS with Dokploy, Traefik, UFW, Myst, Forgejo, and a shared Postgres instance that contains separate `forgejo` and `myst` databases.
|
||||||
- Self-hosted server deployment: a home server, mini PC, NAS, or other self-hosted Linux machine running Myst with Docker, Podman, Docker Compose, Coolify, Caddy, Nginx, or another reverse proxy.
|
- Self-hosted server deployment: a home server, mini PC, NAS, or other self-hosted Linux machine running Myst with Docker, Podman, Docker Compose, Coolify, Caddy, Nginx, or another reverse proxy.
|
||||||
|
|
@ -79,16 +79,20 @@ Myst should not assume a VPS-only environment. The requirement is an existing Fo
|
||||||
|
|
||||||
## Example Configuration
|
## Example Configuration
|
||||||
|
|
||||||
|
Create a `.env` file with:
|
||||||
|
|
||||||
```env
|
```env
|
||||||
MYST_PUBLIC_URL="https://share.example.com"
|
# Docker (same Compose network):
|
||||||
MYST_ADMIN_URL="https://admin.example.com"
|
FORGEJO_BASE_URL=http://forgejo:3000
|
||||||
DATABASE_URL="postgresql://myst:secret@localhost:5432/myst"
|
# Bare metal / non-container:
|
||||||
FORGEJO_BASE_URL="https://git.example.com"
|
# FORGEJO_BASE_URL=http://localhost:3000
|
||||||
FORGEJO_BOT_USERNAME="myst-bot"
|
# Cross-host:
|
||||||
FORGEJO_PAT="fgp_xxxxxxxxxxxxxxxxxxxx"
|
# FORGEJO_BASE_URL=https://git.example.com
|
||||||
GRANT_TOKEN_SECRET="your-secret-here"
|
FORGEJO_PAT=forgejo_pat_xxxxxxxxxxxx
|
||||||
```
|
```
|
||||||
|
|
||||||
|
When using the provided Docker Compose example, DATABASE_URL is injected automatically. For other deployment approaches (e.g., bare metal, Kubernetes), you must supply DATABASE_URL yourself.
|
||||||
|
|
||||||
## Phase 1 Progress
|
## Phase 1 Progress
|
||||||
|
|
||||||
- [x] Project renamed from Embargo to Myst
|
- [x] Project renamed from Embargo to Myst
|
||||||
|
|
|
||||||
42
packages/cli/examples/docker-compose.example.yml
Normal file
42
packages/cli/examples/docker-compose.example.yml
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
# Docker Compose example for deploying Myst with PostgreSQL
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# 1. Copy to docker-compose.yml: cp packages/cli/examples/docker-compose.example.yml docker-compose.yml
|
||||||
|
# 2. Edit .env with your Forgejo config
|
||||||
|
# 3. Run: docker compose up -d
|
||||||
|
#
|
||||||
|
# The DATABASE_URL is auto-handled by Docker Compose networking.
|
||||||
|
|
||||||
|
services:
|
||||||
|
myst:
|
||||||
|
image: myst:latest
|
||||||
|
container_name: myst
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
environment:
|
||||||
|
- DATABASE_URL=postgresql://myst:myst@db:5432/myst
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
|
db:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
container_name: myst-db
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
- POSTGRES_USER=myst
|
||||||
|
- POSTGRES_PASSWORD=myst
|
||||||
|
- POSTGRES_DB=myst
|
||||||
|
volumes:
|
||||||
|
- myst-postgres:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U myst -d myst"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
myst-postgres:
|
||||||
|
|
@ -1,6 +1,3 @@
|
||||||
{
|
{
|
||||||
"adminUrl": "https://admin.example.com",
|
"forgejoBaseUrl": "http://forgejo:3000"
|
||||||
"forgejoBaseUrl": "https://git.example.com",
|
|
||||||
"forgejoBotUsername": "myst-bot",
|
|
||||||
"publicUrl": "https://share.example.com"
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,56 +4,32 @@ import path from "node:path";
|
||||||
import * as c from "yoctocolors";
|
import * as c from "yoctocolors";
|
||||||
import * as p from "@clack/prompts";
|
import * as p from "@clack/prompts";
|
||||||
|
|
||||||
import { generateSecret } from "../../utils/crypto.js";
|
import { generateEnvFile, SECRET_ENV_KEYS } from "../../utils/config.js";
|
||||||
import {
|
|
||||||
generateEnvFile,
|
|
||||||
SECRET_ENV_KEYS,
|
|
||||||
} from "../../utils/config.js";
|
|
||||||
import { CommandConfig } from "../../cli-router.js";
|
import { CommandConfig } from "../../cli-router.js";
|
||||||
|
|
||||||
function normalizeHttpUrl(value: string): string {
|
|
||||||
const trimmed = value.trim();
|
|
||||||
const hasProtocol = /^[a-z]+:\/\//i.test(trimmed);
|
|
||||||
const isNotHttp = !/^https?:\/\//i.test(trimmed);
|
|
||||||
|
|
||||||
if (hasProtocol && isNotHttp) {
|
|
||||||
throw new Error("URLs must use http:// or https://");
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasProtocol ? trimmed : `https://${trimmed}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateHttpUrlOrHost(
|
function validateHttpUrlOrHost(
|
||||||
value: string | undefined,
|
value: string | undefined,
|
||||||
label: string,
|
label: string,
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
if (!value?.trim()) return `${label} is required`;
|
const trimmed = value?.trim();
|
||||||
|
if (!trimmed) return `${label} is required`;
|
||||||
|
|
||||||
try {
|
const hasProtocol = /^[a-z]+:\/\//i.test(trimmed);
|
||||||
const normalized = normalizeHttpUrl(value);
|
const isHttp = /^https?:\/\//i.test(trimmed);
|
||||||
const url = new URL(normalized);
|
|
||||||
if (!url.hostname) {
|
if (hasProtocol && !isHttp) {
|
||||||
return `${label} must include a domain or hostname`;
|
return `${label} must use http:// or https://`;
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return `${label} must be a valid URL or hostname`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
if (!hasProtocol) {
|
||||||
}
|
return `${label} must include a scheme — e.g. http://forgejo:3000 or https://git.example.com`;
|
||||||
|
|
||||||
function validatePostgresUrl(value: string | undefined): string | undefined {
|
|
||||||
if (!value?.trim()) {
|
|
||||||
return "Database URL is required";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const url = new URL(value);
|
const url = new URL(trimmed);
|
||||||
if (url.protocol !== "postgres:" && url.protocol !== "postgresql:") {
|
if (!url.hostname) return `${label} must include a domain or hostname`;
|
||||||
return "Database URL must start with postgres:// or postgresql://";
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
return "Database URL must be a valid Postgres connection string";
|
return `${label} must be a valid URL`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
@ -72,7 +48,7 @@ async function init(opts: { output?: string }) {
|
||||||
`- Repo permissions: ${c.cyan("https://forgejo.org/docs/latest/user/repo-permissions/")}`,
|
`- Repo permissions: ${c.cyan("https://forgejo.org/docs/latest/user/repo-permissions/")}`,
|
||||||
`- Admin CLI: ${c.cyan("https://forgejo.org/docs/latest/admin/command-line/")}`,
|
`- Admin CLI: ${c.cyan("https://forgejo.org/docs/latest/admin/command-line/")}`,
|
||||||
"",
|
"",
|
||||||
`1. Create a dedicated Forgejo user such as ${c.bold("myst-bot")} and generate its PAT in Forgejo ${c.bold("Settings -> Applications")}.`,
|
`1. Generate a PAT in Forgejo ${c.bold("Settings -> Applications")}.`,
|
||||||
`2. Generate a PAT with the appropriate scopes for your Forgejo setup.`,
|
`2. Generate a PAT with the appropriate scopes for your Forgejo setup.`,
|
||||||
"",
|
"",
|
||||||
`Recommended PAT scopes:\n - ${c.bold("read:user")}\n - ${c.bold("read:repository")}\n - ${c.bold("read:organization")} (optional)`,
|
`Recommended PAT scopes:\n - ${c.bold("read:user")}\n - ${c.bold("read:repository")}\n - ${c.bold("read:organization")} (optional)`,
|
||||||
|
|
@ -118,7 +94,9 @@ async function init(opts: { output?: string }) {
|
||||||
.map((line) => {
|
.map((line) => {
|
||||||
if (!line.includes("=")) return line;
|
if (!line.includes("=")) return line;
|
||||||
const [key] = line.split("=");
|
const [key] = line.split("=");
|
||||||
return SECRET_ENV_KEYS.includes(key as typeof SECRET_ENV_KEYS[number])
|
return SECRET_ENV_KEYS.includes(
|
||||||
|
key as (typeof SECRET_ENV_KEYS)[number],
|
||||||
|
)
|
||||||
? `${key}=****`
|
? `${key}=****`
|
||||||
: line;
|
: line;
|
||||||
})
|
})
|
||||||
|
|
@ -135,52 +113,19 @@ async function init(opts: { output?: string }) {
|
||||||
|
|
||||||
const answers = await p.group(
|
const answers = await p.group(
|
||||||
{
|
{
|
||||||
publicUrl: () =>
|
|
||||||
p.text({
|
|
||||||
message: "Domain name or URL for the public viewer",
|
|
||||||
placeholder: "share.example.com",
|
|
||||||
validate: (v) => validateHttpUrlOrHost(v, "Public viewer URL"),
|
|
||||||
}),
|
|
||||||
adminUrl: () =>
|
|
||||||
p.text({
|
|
||||||
message: "Domain name or URL for the Myst admin UI",
|
|
||||||
placeholder: "admin.example.com",
|
|
||||||
validate: (v) => validateHttpUrlOrHost(v, "Private admin URL"),
|
|
||||||
}),
|
|
||||||
forgejoBaseUrl: () =>
|
forgejoBaseUrl: () =>
|
||||||
p.text({
|
|
||||||
message: "Domain name or URL for your existing Forgejo instance?",
|
|
||||||
placeholder: "git.example.com",
|
|
||||||
validate: (v) => validateHttpUrlOrHost(v, "Forgejo base URL"),
|
|
||||||
}),
|
|
||||||
databaseUrl: () =>
|
|
||||||
p.text({
|
|
||||||
message: "What Postgres connection string should Myst use?",
|
|
||||||
placeholder: "postgresql://myst:password@db.example.com:5432/myst",
|
|
||||||
validate: validatePostgresUrl,
|
|
||||||
}),
|
|
||||||
forgejoBotUsername: () =>
|
|
||||||
p.text({
|
p.text({
|
||||||
message:
|
message:
|
||||||
"What dedicated Forgejo username should Myst use for API access?",
|
"Forgejo API URL (how Myst reaches Forgejo, not your browser URL)?",
|
||||||
placeholder: "myst-bot",
|
placeholder: "http://forgejo:3000",
|
||||||
validate: (v) =>
|
validate: (v) => validateHttpUrlOrHost(v, "Forgejo base URL"),
|
||||||
v?.trim() ? undefined : "Dedicated Forgejo username is required",
|
|
||||||
}),
|
}),
|
||||||
forgejoPat: () =>
|
forgejoPat: () =>
|
||||||
p.password({
|
p.password({
|
||||||
message:
|
message: "Paste the Forgejo personal access token",
|
||||||
"Paste the Forgejo personal access token for that dedicated Forgejo user",
|
|
||||||
mask: "*",
|
mask: "*",
|
||||||
validate: (v) => (v?.trim() ? undefined : "Forgejo PAT is required"),
|
validate: (v) => (v?.trim() ? undefined : "Forgejo PAT is required"),
|
||||||
}),
|
}),
|
||||||
grantTokenSecret: () =>
|
|
||||||
p.password({
|
|
||||||
message:
|
|
||||||
"Paste a grant token secret, or press enter to generate one automatically",
|
|
||||||
mask: "*",
|
|
||||||
validate: () => undefined,
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onCancel: () => {
|
onCancel: () => {
|
||||||
|
|
@ -195,24 +140,16 @@ async function init(opts: { output?: string }) {
|
||||||
try {
|
try {
|
||||||
s.start("Generating config files");
|
s.start("Generating config files");
|
||||||
|
|
||||||
const publicUrl = normalizeHttpUrl(answers.publicUrl);
|
const forgejoBaseUrl = answers.forgejoBaseUrl.trim();
|
||||||
const adminUrl = normalizeHttpUrl(answers.adminUrl);
|
|
||||||
const forgejoBaseUrl = normalizeHttpUrl(answers.forgejoBaseUrl);
|
|
||||||
const grantTokenSecret =
|
|
||||||
answers.grantTokenSecret?.trim() || generateSecret();
|
|
||||||
|
|
||||||
const envContent = generateEnvFile({
|
const envContent = generateEnvFile({
|
||||||
adminUrl,
|
|
||||||
databaseUrl: answers.databaseUrl,
|
|
||||||
forgejoBaseUrl,
|
forgejoBaseUrl,
|
||||||
forgejoBotUsername: answers.forgejoBotUsername,
|
|
||||||
forgejoPat: answers.forgejoPat,
|
forgejoPat: answers.forgejoPat,
|
||||||
grantTokenSecret,
|
|
||||||
publicUrl,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (envExists) {
|
if (envExists) {
|
||||||
const tempPath = envPath + ".tmp";
|
const tempPath = envPath + ".tmp";
|
||||||
|
await fs.unlink(tempPath).catch(() => {});
|
||||||
await fs.writeFile(tempPath, envContent, {
|
await fs.writeFile(tempPath, envContent, {
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
flag: "wx",
|
flag: "wx",
|
||||||
|
|
@ -232,12 +169,12 @@ async function init(opts: { output?: string }) {
|
||||||
s.stop(c.yellow(`.env created at ${envPath}`));
|
s.stop(c.yellow(`.env created at ${envPath}`));
|
||||||
p.note(
|
p.note(
|
||||||
[
|
[
|
||||||
`${c.bold("Public viewer URL:")} ${c.cyan(publicUrl)}`,
|
`${c.bold("Forgejo API URL:")} ${c.cyan(forgejoBaseUrl)}`,
|
||||||
`${c.bold("Admin URL:")} ${c.cyan(adminUrl)}`,
|
|
||||||
`${c.bold("Forgejo base URL:")} ${c.cyan(forgejoBaseUrl)}`,
|
|
||||||
`${c.bold("Myst bot username:")} ${c.cyan(answers.forgejoBotUsername)}`,
|
|
||||||
"",
|
"",
|
||||||
`${c.bold("Admin URL reminder:")} protect this upstream with Tailscale, VPN, Cloudflare Access, or reverse-proxy auth.`,
|
`Myst will use this URL to call Forgejo's API.`,
|
||||||
|
`- Docker (same Compose network): http://forgejo:3000`,
|
||||||
|
`- Bare metal / non-container: http://localhost:3000`,
|
||||||
|
`- Cross-host deployment: https://git.example.com`,
|
||||||
].join("\n"),
|
].join("\n"),
|
||||||
);
|
);
|
||||||
p.log.success(
|
p.log.success(
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import * as p from "@clack/prompts";
|
||||||
import { ENV_MAPPING, parseEnvFile, REQUIRED_KEYS } from "../utils/config.js";
|
import { ENV_MAPPING, parseEnvFile, REQUIRED_KEYS } from "../utils/config.js";
|
||||||
import {
|
import {
|
||||||
checkForgejoReachability,
|
checkForgejoReachability,
|
||||||
checkForgejoApiAuthentication,
|
|
||||||
checkRepoAccess,
|
checkRepoAccess,
|
||||||
} from "../utils/forgejo-checks.js";
|
} from "../utils/forgejo-checks.js";
|
||||||
import { CommandConfig } from "../cli-router.js";
|
import { CommandConfig } from "../cli-router.js";
|
||||||
|
|
@ -47,6 +46,12 @@ function getMissingKeys(config: Record<string, string>): string[] {
|
||||||
return missing;
|
return missing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatCheck(result: CheckResult): string {
|
||||||
|
const icon = result.success ? c.green("✓") : c.red("✗");
|
||||||
|
const msg = result.message ? ` ${result.message}` : "";
|
||||||
|
return `${icon} ${result.name}${msg}`;
|
||||||
|
}
|
||||||
|
|
||||||
async function runDiagnosticMode(
|
async function runDiagnosticMode(
|
||||||
config: Record<string, string>,
|
config: Record<string, string>,
|
||||||
missing: string[],
|
missing: string[],
|
||||||
|
|
@ -73,11 +78,7 @@ async function runDiagnosticMode(
|
||||||
const forgejoResults = await runForgejoChecks(config);
|
const forgejoResults = await runForgejoChecks(config);
|
||||||
const allResults = [...results, ...forgejoResults];
|
const allResults = [...results, ...forgejoResults];
|
||||||
|
|
||||||
const lines = allResults.map((r) => {
|
const lines = allResults.map(formatCheck);
|
||||||
const icon = r.success ? c.green("✓") : c.red("✗");
|
|
||||||
const msg = r.message ? ` ${r.message}` : "";
|
|
||||||
return `${icon} ${r.name}${msg}`;
|
|
||||||
});
|
|
||||||
|
|
||||||
p.note(lines.join("\n"), c.yellow("Results"));
|
p.note(lines.join("\n"), c.yellow("Results"));
|
||||||
|
|
||||||
|
|
@ -111,7 +112,7 @@ async function runVerifyMode(
|
||||||
const firstFailure = forgejoResults.find((r) => !r.success);
|
const firstFailure = forgejoResults.find((r) => !r.success);
|
||||||
if (firstFailure) {
|
if (firstFailure) {
|
||||||
s.stop(firstFailure.message);
|
s.stop(firstFailure.message);
|
||||||
p.cancel(`${c.red("✗")} ${firstFailure.name}: ${firstFailure.message}`);
|
p.cancel(formatCheck(firstFailure));
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -125,14 +126,10 @@ async function runVerifyMode(
|
||||||
|
|
||||||
s.stop("Verification complete");
|
s.stop("Verification complete");
|
||||||
|
|
||||||
const [reachability, auth, repoAccess] = forgejoResults;
|
const [reachability, repoAccess] = forgejoResults;
|
||||||
|
|
||||||
p.note(
|
p.note(
|
||||||
[
|
[formatCheck(reachability), formatCheck(repoAccess)].join("\n"),
|
||||||
`${c.green("✓")} ${reachability.name}`,
|
|
||||||
`${c.green("✓")} ${auth.name}: ${auth.message}`,
|
|
||||||
`${c.green(repoAccess.success ? "✓" : "✗")} ${repoAccess.name}: ${repoAccess.message}`,
|
|
||||||
].join("\n"),
|
|
||||||
c.yellow("Results"),
|
c.yellow("Results"),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -142,9 +139,9 @@ async function runVerifyMode(
|
||||||
async function runForgejoChecks(
|
async function runForgejoChecks(
|
||||||
config: Record<string, string>,
|
config: Record<string, string>,
|
||||||
): Promise<CheckResult[]> {
|
): Promise<CheckResult[]> {
|
||||||
const { forgejoBaseUrl, forgejoPat, forgejoBotUsername } = config;
|
const { forgejoBaseUrl, forgejoPat } = config;
|
||||||
|
|
||||||
if (!forgejoBaseUrl || !forgejoPat || !forgejoBotUsername) {
|
if (!forgejoBaseUrl || !forgejoPat) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,18 +154,7 @@ async function runForgejoChecks(
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auth = await checkForgejoApiAuthentication(forgejoBaseUrl, forgejoPat);
|
const repoAccess = await checkRepoAccess(forgejoBaseUrl, forgejoPat);
|
||||||
results.push(auth);
|
|
||||||
|
|
||||||
if (!auth.success) {
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
const repoAccess = await checkRepoAccess(
|
|
||||||
forgejoBaseUrl,
|
|
||||||
forgejoPat,
|
|
||||||
forgejoBotUsername,
|
|
||||||
);
|
|
||||||
results.push(repoAccess);
|
results.push(repoAccess);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,20 @@
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
|
|
||||||
export interface MystEnvironmentVariables {
|
export interface MystEnvironmentVariables {
|
||||||
publicUrl: string;
|
|
||||||
adminUrl: string;
|
|
||||||
databaseUrl: string;
|
|
||||||
forgejoBaseUrl: string;
|
forgejoBaseUrl: string;
|
||||||
forgejoBotUsername: string;
|
|
||||||
forgejoPat: string;
|
forgejoPat: string;
|
||||||
grantTokenSecret: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ENV_MAPPING = {
|
export const ENV_MAPPING = {
|
||||||
MYST_PUBLIC_URL: "publicUrl",
|
|
||||||
MYST_ADMIN_URL: "adminUrl",
|
|
||||||
DATABASE_URL: "databaseUrl",
|
|
||||||
FORGEJO_BASE_URL: "forgejoBaseUrl",
|
FORGEJO_BASE_URL: "forgejoBaseUrl",
|
||||||
FORGEJO_BOT_USERNAME: "forgejoBotUsername",
|
|
||||||
FORGEJO_PAT: "forgejoPat",
|
FORGEJO_PAT: "forgejoPat",
|
||||||
GRANT_TOKEN_SECRET: "grantTokenSecret",
|
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const REQUIRED_KEYS = [
|
export const REQUIRED_KEYS = ["forgejoBaseUrl", "forgejoPat"] as const;
|
||||||
"forgejoBaseUrl",
|
|
||||||
"forgejoBotUsername",
|
|
||||||
"forgejoPat",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export const DATABASE_PROTOCOLS = ["postgres:", "postgresql:"] as const;
|
export const SECRET_KEYS = ["forgejoPat"] as const;
|
||||||
|
|
||||||
export const SECRET_KEYS = [
|
export const SECRET_ENV_KEYS = ["FORGEJO_PAT"] as const;
|
||||||
"forgejoPat",
|
|
||||||
"grantTokenSecret",
|
|
||||||
"databaseUrl",
|
|
||||||
"adminUrl",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export const SECRET_ENV_KEYS = [
|
|
||||||
"DATABASE_URL",
|
|
||||||
"FORGEJO_PAT",
|
|
||||||
"GRANT_TOKEN_SECRET",
|
|
||||||
"MYST_ADMIN_URL",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export function parseEnvFile(
|
export function parseEnvFile(
|
||||||
envPath: string,
|
envPath: string,
|
||||||
|
|
@ -67,36 +41,14 @@ export function parseEnvFile(
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateDatabaseUrl(url: string | undefined): boolean {
|
|
||||||
if (!url) return false;
|
|
||||||
try {
|
|
||||||
const parsed = new URL(url);
|
|
||||||
return DATABASE_PROTOCOLS.includes(
|
|
||||||
parsed.protocol as "postgres:" | "postgresql:",
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateEnvFile(options: MystEnvironmentVariables): string {
|
export function generateEnvFile(options: MystEnvironmentVariables): string {
|
||||||
const lines = [
|
const lines = [
|
||||||
`# Public URL used for share links.`,
|
`# Forgejo API endpoint that Myst calls.`,
|
||||||
`MYST_PUBLIC_URL=${options.publicUrl}`,
|
`# Docker (same Compose network): http://forgejo:3000`,
|
||||||
``,
|
`# Bare metal / non-container: http://localhost:3000`,
|
||||||
`# Private operator URL for the admin UI. Protect this upstream.`,
|
`# Cross-host: https://git.example.com`,
|
||||||
`MYST_ADMIN_URL=${options.adminUrl}`,
|
|
||||||
``,
|
|
||||||
`# Myst's own Postgres database.`,
|
|
||||||
`DATABASE_URL=${options.databaseUrl}`,
|
|
||||||
``,
|
|
||||||
`# Existing Forgejo instance and operator-created PAT for the dedicated Forgejo user.`,
|
|
||||||
`FORGEJO_BASE_URL=${options.forgejoBaseUrl}`,
|
`FORGEJO_BASE_URL=${options.forgejoBaseUrl}`,
|
||||||
`FORGEJO_BOT_USERNAME=${options.forgejoBotUsername}`,
|
|
||||||
`FORGEJO_PAT=${options.forgejoPat}`,
|
`FORGEJO_PAT=${options.forgejoPat}`,
|
||||||
``,
|
|
||||||
`# Secret used to sign or derive grant tokens.`,
|
|
||||||
`GRANT_TOKEN_SECRET=${options.grantTokenSecret}`,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
return lines.join("\n");
|
return lines.join("\n");
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,10 @@ function getErrorMessage(error: unknown, context: string): string {
|
||||||
if (error.message.includes("ECONNREFUSED")) {
|
if (error.message.includes("ECONNREFUSED")) {
|
||||||
return `${context}: connection refused`;
|
return `${context}: connection refused`;
|
||||||
}
|
}
|
||||||
if (error.message.includes("ENOTFOUND") || error.message.includes("getaddrinfo")) {
|
if (
|
||||||
|
error.message.includes("ENOTFOUND") ||
|
||||||
|
error.message.includes("getaddrinfo")
|
||||||
|
) {
|
||||||
return `${context}: host not found`;
|
return `${context}: host not found`;
|
||||||
}
|
}
|
||||||
return `${context}: ${error.message}`;
|
return `${context}: ${error.message}`;
|
||||||
|
|
@ -47,122 +50,73 @@ export async function checkForgejoReachability(
|
||||||
try {
|
try {
|
||||||
const response = await fetchWithTimeout(baseUrl, { method: "GET" });
|
const response = await fetchWithTimeout(baseUrl, { method: "GET" });
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return createCheckResult("Forgejo reachability", true, "connected");
|
return createCheckResult("Forgejo", true, "is reachable");
|
||||||
}
|
}
|
||||||
return createCheckResult(
|
return createCheckResult(
|
||||||
"Forgejo reachability",
|
"Forgejo",
|
||||||
false,
|
false,
|
||||||
`server returned ${response.status} ${response.statusText}`,
|
`server returned ${response.status} ${response.statusText}`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return createCheckResult(
|
return createCheckResult(
|
||||||
"Forgejo reachability",
|
"Forgejo",
|
||||||
false,
|
false,
|
||||||
getErrorMessage(error, "cannot reach server"),
|
getErrorMessage(error, "cannot reach server"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkForgejoApiAuthentication(
|
|
||||||
baseUrl: string,
|
|
||||||
pat: string,
|
|
||||||
): Promise<CheckResult> {
|
|
||||||
try {
|
|
||||||
const response = await fetchWithTimeout(`${baseUrl}/api/v1/user`, {
|
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${pat}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.status === 401) {
|
|
||||||
return createCheckResult(
|
|
||||||
"Forgejo API token",
|
|
||||||
false,
|
|
||||||
"token is invalid or expired",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.status === 403) {
|
|
||||||
return createCheckResult(
|
|
||||||
"Forgejo API token",
|
|
||||||
false,
|
|
||||||
"token lacks sufficient permissions",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
return createCheckResult(
|
|
||||||
"Forgejo API token",
|
|
||||||
false,
|
|
||||||
`server returned ${response.status}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = (await response.json()) as { login: string };
|
|
||||||
return createCheckResult(
|
|
||||||
"Forgejo API token",
|
|
||||||
true,
|
|
||||||
`authenticated as ${user.login}`,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return createCheckResult(
|
|
||||||
"Forgejo API token",
|
|
||||||
false,
|
|
||||||
getErrorMessage(error, "cannot connect to API"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function checkRepoAccess(
|
export async function checkRepoAccess(
|
||||||
baseUrl: string,
|
baseUrl: string,
|
||||||
pat: string,
|
pat: string,
|
||||||
username: string,
|
|
||||||
): Promise<CheckResult> {
|
): Promise<CheckResult> {
|
||||||
try {
|
try {
|
||||||
const response = await fetchWithTimeout(
|
const responses = await Promise.all([
|
||||||
`${baseUrl}/api/v1/users/${username}/repos?limit=1`,
|
fetchWithTimeout(`${baseUrl}/api/v1/user`, {
|
||||||
{
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${pat}`,
|
Authorization: `token ${pat}`,
|
||||||
},
|
},
|
||||||
|
}),
|
||||||
|
fetchWithTimeout(`${baseUrl}/api/v1/user/repos?limit=1`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: `token ${pat}`,
|
||||||
},
|
},
|
||||||
);
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
if (response.status === 401 || response.status === 403) {
|
const errorResponse = responses.find((res) => !res.ok);
|
||||||
return createCheckResult(
|
if (errorResponse) {
|
||||||
"Repo access",
|
const error = (await errorResponse.json()) as {
|
||||||
false,
|
message: string;
|
||||||
"token lacks permission to list repositories",
|
url: string;
|
||||||
);
|
};
|
||||||
|
return createCheckResult("Forgejo", false, error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
const [userResponse, reposResponse] = responses;
|
||||||
return createCheckResult(
|
const user = (await userResponse.json()) as { login: string };
|
||||||
"Repo access",
|
const totalRepos = parseInt(
|
||||||
false,
|
reposResponse.headers.get("x-total-count") ?? "0",
|
||||||
`server returned ${response.status}`,
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const repos = (await response.json()) as { name: string }[];
|
if (user) {
|
||||||
if (repos.length > 0) {
|
|
||||||
return createCheckResult(
|
return createCheckResult(
|
||||||
"Repo access",
|
"Forgejo",
|
||||||
true,
|
true,
|
||||||
`found ${repos.length} repository`,
|
`${user.login} has ${totalRepos} ${totalRepos === 1 ? "repository" : "repositories"}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return createCheckResult(
|
return createCheckResult(
|
||||||
"Repo access",
|
"Forgejo",
|
||||||
false,
|
false,
|
||||||
"no repositories found for user",
|
"no repositories found for user",
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return createCheckResult(
|
return createCheckResult(
|
||||||
"Repo access",
|
"Forgejo",
|
||||||
false,
|
false,
|
||||||
getErrorMessage(error, "cannot list repositories"),
|
getErrorMessage(error, "cannot list repositories"),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue