mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-09 21:25:45 +08:00
refactor(server): better selfhost deployment (#9036)
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
# select a revision to deploy, available values: stable, beta, canary
|
||||||
|
AFFINE_REVISION=stable
|
||||||
|
|
||||||
|
# set the port for the server container it will expose the server on
|
||||||
|
PORT=3010
|
||||||
|
|
||||||
|
# set the host for the server for outgoing links
|
||||||
|
# AFFINE_SERVER_HTTPS=true
|
||||||
|
# AFFINE_SERVER_HOST=affine.yourdomain.com
|
||||||
|
# or
|
||||||
|
# AFFINE_SERVER_EXTERNAL_URL=https://affine.yourdomain.com
|
||||||
|
|
||||||
|
# position of the database data to persist
|
||||||
|
DB_DATA_LOCATION=~/.affine/self-host/postgres
|
||||||
|
# position of the upload data(images, files, etc.) to persist
|
||||||
|
UPLOAD_LOCATION=~/.affine/self-host/storage
|
||||||
|
# position of the configuration files to persist
|
||||||
|
CONFIG_LOCATION=~/.affine/self-host/config
|
||||||
|
|
||||||
|
# database credentials
|
||||||
|
DB_USERNAME=affine
|
||||||
|
DB_PASSWORD=
|
||||||
|
DB_DATABASE=affine
|
||||||
@@ -1,60 +1,66 @@
|
|||||||
|
name: affine
|
||||||
services:
|
services:
|
||||||
affine:
|
affine:
|
||||||
image: ghcr.io/toeverything/affine-graphql:stable
|
image: ghcr.io/toeverything/affine-graphql:${AFFINE_REVISION:-stable}
|
||||||
container_name: affine_selfhosted
|
container_name: affine_server
|
||||||
command:
|
|
||||||
['sh', '-c', 'node ./scripts/self-host-predeploy && node ./dist/index.js']
|
|
||||||
ports:
|
ports:
|
||||||
- '3010:3010'
|
- '${PORT:-3010}:3010'
|
||||||
- '5555:5555'
|
|
||||||
depends_on:
|
depends_on:
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
affine_migration:
|
||||||
|
condition: service_completed_successfully
|
||||||
volumes:
|
volumes:
|
||||||
# custom configurations
|
# custom configurations
|
||||||
- ~/.affine/self-host/config:/root/.affine/config
|
- ${UPLOAD_LOCATION}:/root/.affine/storage
|
||||||
# blob storage
|
- ${CONFIG_LOCATION}:/root/.affine/config
|
||||||
- ~/.affine/self-host/storage:/root/.affine/storage
|
env_file:
|
||||||
logging:
|
- .env
|
||||||
driver: 'json-file'
|
|
||||||
options:
|
|
||||||
max-size: '1000m'
|
|
||||||
restart: unless-stopped
|
|
||||||
environment:
|
environment:
|
||||||
- NODE_OPTIONS="--import=./scripts/register.js"
|
- DATABASE_URL=postgresql://${DB_USERNAME}:${DB_PASSWORD}@postgres:5432/${DB_DATABASE:-affine}
|
||||||
- AFFINE_CONFIG_PATH=/root/.affine/config
|
restart: unless-stopped
|
||||||
- REDIS_SERVER_HOST=redis
|
|
||||||
- DATABASE_URL=postgres://affine:affine@postgres:5432/affine
|
affine_migration:
|
||||||
- NODE_ENV=production
|
image: ghcr.io/toeverything/affine-graphql:${AFFINE_REVISION:-stable}
|
||||||
# Telemetry allows us to collect data on how you use the affine. This data will helps us improve the app and provide better features.
|
container_name: affine_migration_job
|
||||||
# Uncomment next line if you wish to quit telemetry.
|
volumes:
|
||||||
# - TELEMETRY_ENABLE=false
|
# custom configurations
|
||||||
|
- ${UPLOAD_LOCATION}:/root/.affine/storage
|
||||||
|
- ${CONFIG_LOCATION}:/root/.affine/config
|
||||||
|
command: ['sh', '-c', 'node ./scripts/self-host-predeploy.js']
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
environment:
|
||||||
|
- DATABASE_URL=postgresql://${DB_USERNAME}:${DB_PASSWORD}@postgres:5432/${DB_DATABASE:-affine}
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis
|
image: redis
|
||||||
container_name: affine_redis
|
container_name: redis
|
||||||
restart: unless-stopped
|
|
||||||
volumes:
|
|
||||||
- ~/.affine/self-host/redis:/data
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ['CMD', 'redis-cli', '--raw', 'incr', 'ping']
|
test: ['CMD', 'redis-cli', '--raw', 'incr', 'ping']
|
||||||
interval: 10s
|
restart: unless-stopped
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16
|
image: postgres:16
|
||||||
container_name: affine_postgres
|
container_name: postgres
|
||||||
restart: unless-stopped
|
|
||||||
volumes:
|
volumes:
|
||||||
- ~/.affine/self-host/postgres:/var/lib/postgresql/data
|
- ${DB_DATA_LOCATION}:/var/lib/postgresql/data
|
||||||
healthcheck:
|
|
||||||
test: ['CMD-SHELL', 'pg_isready -U affine']
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: affine
|
POSTGRES_USER: ${DB_USERNAME}
|
||||||
POSTGRES_PASSWORD: affine
|
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
||||||
POSTGRES_DB: affine
|
POSTGRES_DB: ${DB_DATABASE:-affine}
|
||||||
PGDATA: /var/lib/postgresql/data/pgdata
|
POSTGRES_INITDB_ARGS: '--data-checksums'
|
||||||
|
# you better set a password for you database
|
||||||
|
# or you may add 'POSTGRES_HOST_AUTH_METHOD=trust' to ignore postgres security policy
|
||||||
|
POSTGRES_HOST_AUTH_METHOD: trust
|
||||||
|
healthcheck:
|
||||||
|
test: ['CMD-SHELL', 'pg_isready']
|
||||||
|
interval: 1m
|
||||||
|
start_interval: 10s
|
||||||
|
start_period: 1m
|
||||||
|
restart: unless-stopped
|
||||||
|
|||||||
@@ -415,35 +415,39 @@ jobs:
|
|||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: affine-darwin-x64-builds
|
name: affine-darwin-x64-builds
|
||||||
path: ./
|
path: ./release
|
||||||
- name: Download Artifacts (macos-arm64)
|
- name: Download Artifacts (macos-arm64)
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: affine-darwin-arm64-builds
|
name: affine-darwin-arm64-builds
|
||||||
path: ./
|
path: ./release
|
||||||
- name: Download Artifacts (windows-x64)
|
- name: Download Artifacts (windows-x64)
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: affine-win32-x64-builds
|
name: affine-win32-x64-builds
|
||||||
path: ./
|
path: ./release
|
||||||
- name: Download Artifacts (windows-arm64)
|
- name: Download Artifacts (windows-arm64)
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: affine-win32-arm64-builds
|
name: affine-win32-arm64-builds
|
||||||
path: ./
|
path: ./release
|
||||||
- name: Download Artifacts (linux-x64)
|
- name: Download Artifacts (linux-x64)
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: affine-linux-x64-builds
|
name: affine-linux-x64-builds
|
||||||
path: ./
|
path: ./release
|
||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 20
|
node-version: 20
|
||||||
- name: Generate Release yml
|
- name: Generate Release yml
|
||||||
run: |
|
run: |
|
||||||
node ./packages/frontend/apps/electron/scripts/generate-yml.js
|
node ./scripts/generate-release-yml.js
|
||||||
env:
|
env:
|
||||||
RELEASE_VERSION: ${{ needs.before-make.outputs.RELEASE_VERSION }}
|
RELEASE_VERSION: ${{ needs.before-make.outputs.RELEASE_VERSION }}
|
||||||
|
- name: Copy Selfhost Release Files
|
||||||
|
run: |
|
||||||
|
cp ./.github/deployment/self-host/compose.yaml ./release/docker-compose.yaml
|
||||||
|
cp ./.github/deployment/self-host/.env.example ./release/.env.example
|
||||||
- name: Create Release Draft
|
- name: Create Release Draft
|
||||||
if: ${{ github.ref_type == 'tag' }}
|
if: ${{ github.ref_type == 'tag' }}
|
||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
@@ -452,16 +456,7 @@ jobs:
|
|||||||
body: ''
|
body: ''
|
||||||
draft: ${{ github.event.inputs.is-draft }}
|
draft: ${{ github.event.inputs.is-draft }}
|
||||||
prerelease: ${{ github.event.inputs.is-pre-release }}
|
prerelease: ${{ github.event.inputs.is-pre-release }}
|
||||||
files: |
|
files: ./release/*
|
||||||
./VERSION
|
|
||||||
./*.zip
|
|
||||||
./*.dmg
|
|
||||||
./*.exe
|
|
||||||
./*.appimage
|
|
||||||
./*.deb
|
|
||||||
./*.flatpak
|
|
||||||
./*.apk
|
|
||||||
./*.yml
|
|
||||||
- name: Create Nightly Release Draft
|
- name: Create Nightly Release Draft
|
||||||
if: ${{ github.ref_type == 'branch' }}
|
if: ${{ github.ref_type == 'branch' }}
|
||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
@@ -476,13 +471,4 @@ jobs:
|
|||||||
body: ''
|
body: ''
|
||||||
draft: false
|
draft: false
|
||||||
prerelease: true
|
prerelease: true
|
||||||
files: |
|
files: ./release/*
|
||||||
./VERSION
|
|
||||||
./*.zip
|
|
||||||
./*.dmg
|
|
||||||
./*.exe
|
|
||||||
./*.appimage
|
|
||||||
./*.deb
|
|
||||||
./*.apk
|
|
||||||
./*.flatpak
|
|
||||||
./*.yml
|
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# required DATABASE_URL affects app start
|
||||||
|
schema.prisma
|
||||||
@@ -165,6 +165,7 @@
|
|||||||
"*.gen.*"
|
"*.gen.*"
|
||||||
],
|
],
|
||||||
"env": {
|
"env": {
|
||||||
|
"NODE_ENV": "development",
|
||||||
"AFFINE_SERVER_EXTERNAL_URL": "http://localhost:8080",
|
"AFFINE_SERVER_EXTERNAL_URL": "http://localhost:8080",
|
||||||
"TS_NODE_TRANSPILE_ONLY": true,
|
"TS_NODE_TRANSPILE_ONLY": true,
|
||||||
"TS_NODE_PROJECT": "./tsconfig.json",
|
"TS_NODE_PROJECT": "./tsconfig.json",
|
||||||
|
|||||||
@@ -3,61 +3,47 @@ import { generateKeyPairSync } from 'node:crypto';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
|
||||||
import { parse } from 'dotenv';
|
|
||||||
|
|
||||||
const SELF_HOST_CONFIG_DIR = '/root/.affine/config';
|
const SELF_HOST_CONFIG_DIR = '/root/.affine/config';
|
||||||
/**
|
|
||||||
* @type {Array<{ from: string; to?: string, modifier?: (content: string): string }>}
|
|
||||||
*/
|
|
||||||
const configFiles = [
|
|
||||||
{ from: './.env.example', to: '.env' },
|
|
||||||
{ from: './dist/config/affine.js', modifier: configCleaner },
|
|
||||||
{ from: './dist/config/affine.env.js', modifier: configCleaner },
|
|
||||||
];
|
|
||||||
|
|
||||||
function configCleaner(content) {
|
function generateConfigFile() {
|
||||||
|
const content = fs.readFileSync('./dist/config/affine.js', 'utf-8');
|
||||||
return content.replace(
|
return content.replace(
|
||||||
/(^\/\/#.*$)|(^\/\/\s+TODO.*$)|("use\sstrict";?)|(^.*eslint-disable.*$)/gm,
|
/(^\/\/#.*$)|(^\/\/\s+TODO.*$)|("use\sstrict";?)|(^.*eslint-disable.*$)/gm,
|
||||||
''
|
''
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generatePrivateKey() {
|
||||||
|
const key = generateKeyPairSync('ec', {
|
||||||
|
namedCurve: 'prime256v1',
|
||||||
|
}).privateKey.export({
|
||||||
|
type: 'sec1',
|
||||||
|
format: 'pem',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (key instanceof Buffer) {
|
||||||
|
return key.toString('utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Array<{ to: string; generator: () => string }>}
|
||||||
|
*/
|
||||||
|
const configFiles = [
|
||||||
|
{ to: 'affine.js', generator: generateConfigFile },
|
||||||
|
{ to: 'private.key', generator: generatePrivateKey },
|
||||||
|
];
|
||||||
|
|
||||||
function prepare() {
|
function prepare() {
|
||||||
fs.mkdirSync(SELF_HOST_CONFIG_DIR, { recursive: true });
|
fs.mkdirSync(SELF_HOST_CONFIG_DIR, { recursive: true });
|
||||||
|
|
||||||
for (const { from, to, modifier } of configFiles) {
|
for (const { to, generator } of configFiles) {
|
||||||
const targetFileName = to ?? path.parse(from).base;
|
const targetFilePath = path.join(SELF_HOST_CONFIG_DIR, to);
|
||||||
const targetFilePath = path.join(SELF_HOST_CONFIG_DIR, targetFileName);
|
|
||||||
if (!fs.existsSync(targetFilePath)) {
|
if (!fs.existsSync(targetFilePath)) {
|
||||||
console.log(`creating config file [${targetFilePath}].`);
|
console.log(`creating config file [${targetFilePath}].`);
|
||||||
if (modifier) {
|
fs.writeFileSync(targetFilePath, generator(), 'utf-8');
|
||||||
const content = fs.readFileSync(from, 'utf-8');
|
|
||||||
fs.writeFileSync(targetFilePath, modifier(content), 'utf-8');
|
|
||||||
} else {
|
|
||||||
fs.cpSync(from, targetFilePath, {
|
|
||||||
force: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make the default .env
|
|
||||||
if (to === '.env') {
|
|
||||||
const dotenvFile = fs.readFileSync(targetFilePath, 'utf-8');
|
|
||||||
const envs = parse(dotenvFile);
|
|
||||||
// generate a new private key
|
|
||||||
if (!envs.AFFINE_PRIVATE_KEY) {
|
|
||||||
const privateKey = generateKeyPairSync('ec', {
|
|
||||||
namedCurve: 'prime256v1',
|
|
||||||
}).privateKey.export({
|
|
||||||
type: 'sec1',
|
|
||||||
format: 'pem',
|
|
||||||
});
|
|
||||||
|
|
||||||
fs.writeFileSync(
|
|
||||||
targetFilePath,
|
|
||||||
`AFFINE_PRIVATE_KEY=${privateKey}\n` + dotenvFile
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ AFFiNE.ENV_MAP = {
|
|||||||
MAILER_PASSWORD: 'mailer.auth.pass',
|
MAILER_PASSWORD: 'mailer.auth.pass',
|
||||||
MAILER_SENDER: 'mailer.from.address',
|
MAILER_SENDER: 'mailer.from.address',
|
||||||
MAILER_SECURE: ['mailer.secure', 'boolean'],
|
MAILER_SECURE: ['mailer.secure', 'boolean'],
|
||||||
DATABASE_URL: 'database.datasourceUrl',
|
DATABASE_URL: 'prisma.datasourceUrl',
|
||||||
OAUTH_GOOGLE_CLIENT_ID: 'plugins.oauth.providers.google.clientId',
|
OAUTH_GOOGLE_CLIENT_ID: 'plugins.oauth.providers.google.clientId',
|
||||||
OAUTH_GOOGLE_CLIENT_SECRET: 'plugins.oauth.providers.google.clientSecret',
|
OAUTH_GOOGLE_CLIENT_SECRET: 'plugins.oauth.providers.google.clientSecret',
|
||||||
OAUTH_GITHUB_CLIENT_ID: 'plugins.oauth.providers.github.clientId',
|
OAUTH_GITHUB_CLIENT_ID: 'plugins.oauth.providers.github.clientId',
|
||||||
|
|||||||
@@ -17,6 +17,11 @@
|
|||||||
// ====================================================================================
|
// ====================================================================================
|
||||||
const env = process.env;
|
const env = process.env;
|
||||||
|
|
||||||
|
AFFiNE.serverName = AFFiNE.affine.canary
|
||||||
|
? 'AFFiNE Canary Cloud'
|
||||||
|
: AFFiNE.affine.beta
|
||||||
|
? 'AFFiNE Beta Cloud'
|
||||||
|
: 'AFFiNE Cloud';
|
||||||
AFFiNE.metrics.enabled = !AFFiNE.node.test;
|
AFFiNE.metrics.enabled = !AFFiNE.node.test;
|
||||||
|
|
||||||
if (env.R2_OBJECT_STORAGE_ACCOUNT_ID) {
|
if (env.R2_OBJECT_STORAGE_ACCOUNT_ID) {
|
||||||
|
|||||||
@@ -8,22 +8,12 @@
|
|||||||
// Any changes in this file won't take effect before server restarted.
|
// Any changes in this file won't take effect before server restarted.
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// > Configurations merge order
|
|
||||||
// 1. load environment variables (`.env` if provided, and from system)
|
|
||||||
// 2. load `src/fundamentals/config/default.ts` for all default settings
|
|
||||||
// 3. apply `./affine.ts` patches (this file)
|
|
||||||
// 4. apply `./affine.env.ts` patches
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// ###############################################################
|
// ###############################################################
|
||||||
// ## General settings ##
|
// ## General settings ##
|
||||||
// ###############################################################
|
// ###############################################################
|
||||||
//
|
//
|
||||||
// /* The unique identity of the server */
|
|
||||||
// AFFiNE.serverId = 'some-randome-uuid';
|
|
||||||
//
|
|
||||||
// /* The name of AFFiNE Server, may show on the UI */
|
// /* The name of AFFiNE Server, may show on the UI */
|
||||||
// AFFiNE.serverName = 'Your Cool AFFiNE Selfhosted Cloud';
|
AFFiNE.serverName = 'My Selfhosted AFFiNE Cloud';
|
||||||
//
|
//
|
||||||
// /* Whether the server is deployed behind a HTTPS proxied environment */
|
// /* Whether the server is deployed behind a HTTPS proxied environment */
|
||||||
AFFiNE.server.https = false;
|
AFFiNE.server.https = false;
|
||||||
|
|||||||
@@ -261,7 +261,7 @@ export class ServerServiceConfigResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
database(): ServerDatabaseConfig {
|
database(): ServerDatabaseConfig {
|
||||||
const url = new URL(this.config.database.datasourceUrl);
|
const url = new URL(this.config.prisma.datasourceUrl);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
host: url.hostname,
|
host: url.hostname,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { readEnv } from './env';
|
|||||||
import { defaultStartupConfig } from './register';
|
import { defaultStartupConfig } from './register';
|
||||||
|
|
||||||
function getPredefinedAFFiNEConfig(): PreDefinedAFFiNEConfig {
|
function getPredefinedAFFiNEConfig(): PreDefinedAFFiNEConfig {
|
||||||
const NODE_ENV = readEnv<NODE_ENV>('NODE_ENV', 'development', [
|
const NODE_ENV = readEnv<NODE_ENV>('NODE_ENV', 'production', [
|
||||||
'development',
|
'development',
|
||||||
'test',
|
'test',
|
||||||
'production',
|
'production',
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export {
|
|||||||
mapSseError,
|
mapSseError,
|
||||||
OptionalModule,
|
OptionalModule,
|
||||||
} from './nestjs';
|
} from './nestjs';
|
||||||
export type { PrismaTransaction } from './prisma';
|
export { type PrismaTransaction } from './prisma';
|
||||||
export * from './storage';
|
export * from './storage';
|
||||||
export { type StorageProvider, StorageProviderFactory } from './storage';
|
export { type StorageProvider, StorageProviderFactory } from './storage';
|
||||||
export { CloudThrottlerGuard, SkipThrottle, Throttle } from './throttler';
|
export { CloudThrottlerGuard, SkipThrottle, Throttle } from './throttler';
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ interface PrismaStartupConfiguration extends Prisma.PrismaClientOptions {
|
|||||||
|
|
||||||
declare module '../config' {
|
declare module '../config' {
|
||||||
interface AppConfig {
|
interface AppConfig {
|
||||||
database: ModuleConfig<PrismaStartupConfiguration>;
|
prisma: ModuleConfig<PrismaStartupConfiguration>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defineStartupConfig('database', {
|
defineStartupConfig('prisma', {
|
||||||
datasourceUrl: '',
|
datasourceUrl: '',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const clientProvider: Provider = {
|
|||||||
return PrismaService.INSTANCE;
|
return PrismaService.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PrismaService(config.database);
|
return new PrismaService(config.prisma);
|
||||||
},
|
},
|
||||||
inject: [Config],
|
inject: [Config],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
|
|
||||||
import { cpSync } from 'node:fs';
|
import { cpSync, existsSync, readFileSync } from 'node:fs';
|
||||||
|
import { homedir } from 'node:os';
|
||||||
import { join, parse } from 'node:path';
|
import { join, parse } from 'node:path';
|
||||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||||
|
|
||||||
@@ -10,42 +11,57 @@ import {
|
|||||||
applyEnvToConfig,
|
applyEnvToConfig,
|
||||||
getAFFiNEConfigModifier,
|
getAFFiNEConfigModifier,
|
||||||
} from './fundamentals/config';
|
} from './fundamentals/config';
|
||||||
import { enablePlugin } from './plugins';
|
|
||||||
|
|
||||||
const PROJECT_CONFIG_PATH = join(fileURLToPath(import.meta.url), '../config');
|
const PROJECT_CONFIG_PATH = join(fileURLToPath(import.meta.url), '../config');
|
||||||
async function loadRemote(remoteDir: string, file: string) {
|
const CUSTOM_CONFIG_PATH = `${homedir()}/.affine/config`;
|
||||||
let fileToLoad = join(PROJECT_CONFIG_PATH, file);
|
|
||||||
|
|
||||||
if (PROJECT_CONFIG_PATH !== remoteDir) {
|
async function loadConfig(configDir: string, file: string) {
|
||||||
const remoteFile = join(remoteDir, file);
|
let fileToLoad: string | undefined;
|
||||||
|
|
||||||
|
if (PROJECT_CONFIG_PATH !== configDir) {
|
||||||
|
const remoteFile = join(configDir, file);
|
||||||
const remoteFileAtLocal = join(
|
const remoteFileAtLocal = join(
|
||||||
PROJECT_CONFIG_PATH,
|
PROJECT_CONFIG_PATH,
|
||||||
parse(file).name + '.remote.js'
|
parse(file).name + '.remote.js'
|
||||||
);
|
);
|
||||||
cpSync(remoteFile, remoteFileAtLocal, {
|
if (existsSync(remoteFile)) {
|
||||||
force: true,
|
cpSync(remoteFile, remoteFileAtLocal, {
|
||||||
});
|
force: true,
|
||||||
fileToLoad = remoteFileAtLocal;
|
});
|
||||||
|
fileToLoad = remoteFileAtLocal;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fileToLoad = join(PROJECT_CONFIG_PATH, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
await import(pathToFileURL(fileToLoad).href);
|
if (fileToLoad) {
|
||||||
|
await import(pathToFileURL(fileToLoad).href);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadPrivateKey() {
|
||||||
|
const file = join(CUSTOM_CONFIG_PATH, 'private.key');
|
||||||
|
if (!process.env.AFFINE_PRIVATE_KEY && existsSync(file)) {
|
||||||
|
const privateKey = readFileSync(file, 'utf-8');
|
||||||
|
process.env.AFFINE_PRIVATE_KEY = privateKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
const AFFiNE_CONFIG_PATH =
|
|
||||||
process.env.AFFINE_CONFIG_PATH ?? PROJECT_CONFIG_PATH;
|
|
||||||
// Initializing AFFiNE config
|
// Initializing AFFiNE config
|
||||||
//
|
//
|
||||||
// 1. load dotenv file to `process.env`
|
// 1. load dotenv file to `process.env`
|
||||||
// load `.env` under pwd
|
// load `.env` under pwd
|
||||||
config();
|
config();
|
||||||
|
// @deprecated removed
|
||||||
// load `.env` under user config folder
|
// load `.env` under user config folder
|
||||||
config({
|
config({
|
||||||
path: join(AFFiNE_CONFIG_PATH, '.env'),
|
path: join(CUSTOM_CONFIG_PATH, '.env'),
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. generate AFFiNE default config and assign to `globalThis.AFFiNE`
|
// 2. generate AFFiNE default config and assign to `globalThis.AFFiNE`
|
||||||
globalThis.AFFiNE = getAFFiNEConfigModifier();
|
globalThis.AFFiNE = getAFFiNEConfigModifier();
|
||||||
|
const { enablePlugin } = await import('./plugins/registry');
|
||||||
globalThis.AFFiNE.use = enablePlugin;
|
globalThis.AFFiNE.use = enablePlugin;
|
||||||
globalThis.AFFiNE.plugins.use = enablePlugin;
|
globalThis.AFFiNE.plugins.use = enablePlugin;
|
||||||
|
|
||||||
@@ -53,24 +69,23 @@ async function load() {
|
|||||||
// Modules may contribute to ENV_MAP, figure out a good way to involve them instead of hardcoding in `./config/affine.env`
|
// Modules may contribute to ENV_MAP, figure out a good way to involve them instead of hardcoding in `./config/affine.env`
|
||||||
// 3. load env => config map to `globalThis.AFFiNE.ENV_MAP
|
// 3. load env => config map to `globalThis.AFFiNE.ENV_MAP
|
||||||
// load local env map as well in case there are new env added
|
// load local env map as well in case there are new env added
|
||||||
await loadRemote(PROJECT_CONFIG_PATH, 'affine.env.js');
|
await loadConfig(PROJECT_CONFIG_PATH, 'affine.env');
|
||||||
const projectEnvMap = AFFiNE.ENV_MAP;
|
|
||||||
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.env.js');
|
|
||||||
const customEnvMap = AFFiNE.ENV_MAP;
|
|
||||||
AFFiNE.ENV_MAP = { ...projectEnvMap, ...customEnvMap };
|
|
||||||
|
|
||||||
// 4. load `config/affine` to patch custom configs
|
// 4. load `config/affine` to patch custom configs
|
||||||
// load local config as well in case there are new default configurations added
|
// load local config as well in case there are new default configurations added
|
||||||
await loadRemote(PROJECT_CONFIG_PATH, 'affine.js');
|
await loadConfig(PROJECT_CONFIG_PATH, 'affine');
|
||||||
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.js');
|
await loadConfig(CUSTOM_CONFIG_PATH, 'affine');
|
||||||
|
|
||||||
// 5. load `config/affine.self` to patch custom configs
|
// 5. load `config/affine.self` to patch custom configs
|
||||||
// This is the file only take effect in [AFFiNE Cloud]
|
// This is the file only take effect in [AFFiNE Cloud]
|
||||||
if (!AFFiNE.isSelfhosted) {
|
if (!AFFiNE.isSelfhosted) {
|
||||||
await loadRemote(PROJECT_CONFIG_PATH, 'affine.self.js');
|
await loadConfig(PROJECT_CONFIG_PATH, 'affine.self');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. apply `process.env` map overriding to `globalThis.AFFiNE`
|
// 6. load `config/private.key` to patch app configs
|
||||||
|
loadPrivateKey();
|
||||||
|
|
||||||
|
// 7. apply `process.env` map overriding to `globalThis.AFFiNE`
|
||||||
applyEnvToConfig(globalThis.AFFiNE);
|
applyEnvToConfig(globalThis.AFFiNE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
@@ -17,6 +17,7 @@ export type BUILD_CONFIG_TYPE = {
|
|||||||
isMobileWeb: boolean;
|
isMobileWeb: boolean;
|
||||||
isIOS: boolean;
|
isIOS: boolean;
|
||||||
isAndroid: boolean;
|
isAndroid: boolean;
|
||||||
|
isAdmin: boolean;
|
||||||
|
|
||||||
// this is for the electron app
|
// this is for the electron app
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -34,6 +34,7 @@
|
|||||||
"@radix-ui/react-tooltip": "^1.1.1",
|
"@radix-ui/react-tooltip": "^1.1.1",
|
||||||
"@sentry/react": "^8.9.0",
|
"@sentry/react": "^8.9.0",
|
||||||
"@tanstack/react-table": "^8.19.3",
|
"@tanstack/react-table": "^8.19.3",
|
||||||
|
"@toeverything/infra": "workspace:*",
|
||||||
"cmdk": "^1.0.0",
|
"cmdk": "^1.0.0",
|
||||||
"embla-carousel-react": "^8.1.5",
|
"embla-carousel-react": "^8.1.5",
|
||||||
"input-otp": "^1.2.4",
|
"input-otp": "^1.2.4",
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
import { Toaster } from '@affine/admin/components/ui/sonner';
|
import { Toaster } from '@affine/admin/components/ui/sonner';
|
||||||
|
import {
|
||||||
|
configureCloudModule,
|
||||||
|
DefaultServerService,
|
||||||
|
} from '@affine/core/modules/cloud';
|
||||||
|
import { configureLocalStorageStateStorageImpls } from '@affine/core/modules/storage';
|
||||||
|
import { configureUrlModule } from '@affine/core/modules/url';
|
||||||
import { wrapCreateBrowserRouter } from '@sentry/react';
|
import { wrapCreateBrowserRouter } from '@sentry/react';
|
||||||
|
import {
|
||||||
|
configureGlobalContextModule,
|
||||||
|
configureGlobalStorageModule,
|
||||||
|
configureLifecycleModule,
|
||||||
|
Framework,
|
||||||
|
FrameworkRoot,
|
||||||
|
FrameworkScope,
|
||||||
|
LifecycleService,
|
||||||
|
} from '@toeverything/infra';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import {
|
import {
|
||||||
createBrowserRouter as reactRouterCreateBrowserRouter,
|
createBrowserRouter as reactRouterCreateBrowserRouter,
|
||||||
@@ -109,18 +124,38 @@ export const router = _createBrowserRouter(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const framework = new Framework();
|
||||||
|
configureLifecycleModule(framework);
|
||||||
|
configureLocalStorageStateStorageImpls(framework);
|
||||||
|
configureGlobalStorageModule(framework);
|
||||||
|
configureGlobalContextModule(framework);
|
||||||
|
configureUrlModule(framework);
|
||||||
|
configureCloudModule(framework);
|
||||||
|
const frameworkProvider = framework.provider();
|
||||||
|
|
||||||
|
// setup application lifecycle events, and emit application start event
|
||||||
|
window.addEventListener('focus', () => {
|
||||||
|
frameworkProvider.get(LifecycleService).applicationFocus();
|
||||||
|
});
|
||||||
|
frameworkProvider.get(LifecycleService).applicationStart();
|
||||||
|
const serverService = frameworkProvider.get(DefaultServerService);
|
||||||
|
|
||||||
export const App = () => {
|
export const App = () => {
|
||||||
return (
|
return (
|
||||||
<TooltipProvider>
|
<FrameworkRoot framework={frameworkProvider}>
|
||||||
<SWRConfig
|
<FrameworkScope scope={serverService.server.scope}>
|
||||||
value={{
|
<TooltipProvider>
|
||||||
revalidateOnFocus: false,
|
<SWRConfig
|
||||||
revalidateOnMount: false,
|
value={{
|
||||||
}}
|
revalidateOnFocus: false,
|
||||||
>
|
revalidateOnMount: false,
|
||||||
<RouterProvider router={router} />
|
}}
|
||||||
</SWRConfig>
|
>
|
||||||
<Toaster />
|
<RouterProvider router={router} />
|
||||||
</TooltipProvider>
|
</SWRConfig>
|
||||||
|
<Toaster />
|
||||||
|
</TooltipProvider>
|
||||||
|
</FrameworkScope>
|
||||||
|
</FrameworkRoot>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,6 +8,10 @@
|
|||||||
"verbatimModuleSyntax": false,
|
"verbatimModuleSyntax": false,
|
||||||
"jsx": "react-jsx"
|
"jsx": "react-jsx"
|
||||||
},
|
},
|
||||||
"references": [{ "path": "../core" }, { "path": "../graphql" }],
|
"references": [
|
||||||
|
{ "path": "../core" },
|
||||||
|
{ "path": "../graphql" },
|
||||||
|
{ "path": "../../common/infra" }
|
||||||
|
],
|
||||||
"exclude": ["dist"]
|
"exclude": ["dist"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import type { GraphQLError } from 'graphql';
|
|||||||
import { useCallback, useMemo } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import type { SWRConfiguration, SWRResponse } from 'swr';
|
import type { SWRConfiguration, SWRResponse } from 'swr';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import useSWRImutable from 'swr/immutable';
|
import useSWRImmutable from 'swr/immutable';
|
||||||
import useSWRInfinite from 'swr/infinite';
|
import useSWRInfinite from 'swr/infinite';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,7 +60,7 @@ const createUseQuery =
|
|||||||
);
|
);
|
||||||
const graphqlService = useService(GraphQLService);
|
const graphqlService = useService(GraphQLService);
|
||||||
|
|
||||||
const useSWRFn = immutable ? useSWRImutable : useSWR;
|
const useSWRFn = immutable ? useSWRImmutable : useSWR;
|
||||||
return useSWRFn(
|
return useSWRFn(
|
||||||
options ? () => ['cloud', options.query.id, options.variables] : null,
|
options ? () => ['cloud', options.query.id, options.variables] : null,
|
||||||
options ? () => graphqlService.gql(options) : null,
|
options ? () => graphqlService.gql(options) : null,
|
||||||
|
|||||||
+4
-2
@@ -2,6 +2,8 @@ import crypto from 'node:crypto';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
|
||||||
|
const releaseDir = path.join(process.cwd(), './release');
|
||||||
|
|
||||||
const filenamesMapping = {
|
const filenamesMapping = {
|
||||||
all: 'latest.yml',
|
all: 'latest.yml',
|
||||||
macos: 'latest-mac.yml',
|
macos: 'latest-mac.yml',
|
||||||
@@ -22,11 +24,11 @@ const generateYml = platform => {
|
|||||||
? new RegExp(`.(${releaseFiles.join('|')})$`)
|
? new RegExp(`.(${releaseFiles.join('|')})$`)
|
||||||
: new RegExp(`.+-${platform}-.+.(${releaseFiles.join('|')})$`);
|
: new RegExp(`.+-${platform}-.+.(${releaseFiles.join('|')})$`);
|
||||||
|
|
||||||
const files = fs.readdirSync(process.cwd()).filter(file => regex.test(file));
|
const files = fs.readdirSync(releaseDir).filter(file => regex.test(file));
|
||||||
const outputFileName = filenamesMapping[platform];
|
const outputFileName = filenamesMapping[platform];
|
||||||
|
|
||||||
files.forEach(fileName => {
|
files.forEach(fileName => {
|
||||||
const filePath = path.join(process.cwd(), './', fileName);
|
const filePath = path.join(releaseDir, fileName);
|
||||||
try {
|
try {
|
||||||
const fileData = fs.readFileSync(filePath);
|
const fileData = fs.readFileSync(filePath);
|
||||||
const hash = crypto
|
const hash = crypto
|
||||||
@@ -341,7 +341,7 @@ export const createConfiguration: (
|
|||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
buildFlags.mode === 'production' &&
|
buildFlags.mode === 'production' &&
|
||||||
(buildConfig.isWeb || buildConfig.isMobileWeb) &&
|
(buildConfig.isWeb || buildConfig.isMobileWeb || buildConfig.isAdmin) &&
|
||||||
process.env.R2_SECRET_ACCESS_KEY
|
process.env.R2_SECRET_ACCESS_KEY
|
||||||
? new WebpackS3Plugin()
|
? new WebpackS3Plugin()
|
||||||
: null,
|
: null,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export function getBuildConfig(buildFlags: BuildFlags): BUILD_CONFIG_TYPE {
|
|||||||
isMobileWeb: buildFlags.distribution === 'mobile',
|
isMobileWeb: buildFlags.distribution === 'mobile',
|
||||||
isIOS: buildFlags.distribution === 'ios',
|
isIOS: buildFlags.distribution === 'ios',
|
||||||
isAndroid: buildFlags.distribution === 'android',
|
isAndroid: buildFlags.distribution === 'android',
|
||||||
|
isAdmin: buildFlags.distribution === 'admin',
|
||||||
|
|
||||||
isSelfHosted: process.env.SELF_HOSTED === 'true',
|
isSelfHosted: process.env.SELF_HOSTED === 'true',
|
||||||
appBuildType: 'stable' as const,
|
appBuildType: 'stable' as const,
|
||||||
|
|||||||
@@ -181,6 +181,7 @@ __metadata:
|
|||||||
"@radix-ui/react-tooltip": "npm:^1.1.1"
|
"@radix-ui/react-tooltip": "npm:^1.1.1"
|
||||||
"@sentry/react": "npm:^8.9.0"
|
"@sentry/react": "npm:^8.9.0"
|
||||||
"@tanstack/react-table": "npm:^8.19.3"
|
"@tanstack/react-table": "npm:^8.19.3"
|
||||||
|
"@toeverything/infra": "workspace:*"
|
||||||
class-variance-authority: "npm:^0.7.0"
|
class-variance-authority: "npm:^0.7.0"
|
||||||
clsx: "npm:^2.1.1"
|
clsx: "npm:^2.1.1"
|
||||||
cmdk: "npm:^1.0.0"
|
cmdk: "npm:^1.0.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user