fix: improve self-host convenience (#5582)

This commit is contained in:
liuyi
2024-01-15 09:24:52 +00:00
parent 2f9b4fd0cf
commit 24e18dd475
8 changed files with 132 additions and 26 deletions
@@ -11,6 +11,7 @@ export class AppController {
return {
compatibility: this.config.version,
message: `AFFiNE ${this.config.version} Server`,
flavor: this.config.flavor,
};
}
}
@@ -0,0 +1,39 @@
import { ModuleRef } from '@nestjs/core';
import { hash } from '@node-rs/argon2';
import { PrismaClient } from '@prisma/client';
import { Config } from '../../fundamentals';
export class SelfHostAdmin1605053000403 {
// do the migration
static async up(db: PrismaClient, ref: ModuleRef) {
const config = ref.get(Config, { strict: false });
if (config.flavor === 'selfhosted') {
if (
!process.env.AFFINE_ADMIN_EMAIL ||
!process.env.AFFINE_ADMIN_PASSWORD
) {
throw new Error(
'You have to set AFFINE_ADMIN_EMAIL and AFFINE_ADMIN_PASSWORD environment variables to generate the initial user for self-hosted AFFiNE Server.'
);
}
await db.user.create({
data: {
name: 'AFFINE First User',
email: process.env.AFFINE_ADMIN_EMAIL,
emailVerified: new Date(),
password: await hash(process.env.AFFINE_ADMIN_PASSWORD),
},
});
}
}
// revert the migration
static async down(db: PrismaClient) {
await db.user.deleteMany({
where: {
email: process.env.AFFINE_ADMIN_EMAIL ?? 'admin@example.com',
},
});
}
}
@@ -16,7 +16,6 @@ export class OldUserFeature1702620653283 {
where: { NOT: { features: { some: { NOT: { id: { gt: 0 } } } } } },
select: { id: true },
});
console.log(`migrating ${userIds.join('|')} users`);
await tx.userFeatures.createMany({
data: userIds.map(({ id: userId }) => ({
@@ -9,7 +9,7 @@ try {
const require = createRequire(import.meta.url);
storageModule =
process.arch === 'arm64'
? require('../.././storage.arm64.node')
? require('../../../storage.arm64.node')
: process.arch === 'arm'
? require('../../../storage.armv7.node')
: require('../../../storage.node');
+20 -4
View File
@@ -1,5 +1,6 @@
import 'reflect-metadata';
import { cpSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -10,7 +11,22 @@ import {
getDefaultAFFiNEConfig,
} from './fundamentals/config';
const configDir = join(fileURLToPath(import.meta.url), '../config');
async function loadRemote(remoteDir: string, file: string) {
console.log(remoteDir, configDir);
const filePath = join(configDir, file);
if (configDir !== remoteDir) {
console.log('cp remote file');
cpSync(join(remoteDir, file), filePath, {
force: true,
});
}
await import(filePath);
}
async function load() {
const AFFiNE_CONFIG_PATH = process.env.AFFINE_CONFIG_PATH ?? configDir;
// Initializing AFFiNE config
//
// 1. load dotenv file to `process.env`
@@ -18,7 +34,7 @@ async function load() {
config();
// load `.env` under user config folder
config({
path: join(fileURLToPath(import.meta.url), '../config/.env'),
path: join(AFFiNE_CONFIG_PATH, '.env'),
});
// 2. generate AFFiNE default config and assign to `globalThis.AFFiNE`
@@ -27,13 +43,13 @@ async function load() {
// TODO(@forehalo):
// 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
await import('./config/affine.env');
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.env.js');
// 4. apply `process.env` map overriding to `globalThis.AFFiNE`
applyEnvToConfig(globalThis.AFFiNE);
// 5. load `./config/affine` to patch custom configs
await import('./config/affine');
// 5. load `config/affine` to patch custom configs
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.js');
if (process.env.NODE_ENV === 'development') {
console.log('AFFiNE Config:', JSON.stringify(globalThis.AFFiNE, null, 2));