feat: support self-host docker build (#5506)

Test command: `docker compose -f ./.github/deployment/self-host/compose.yaml up`
This commit is contained in:
LongYinan
2024-01-10 08:35:21 +00:00
parent 0d7ffb0511
commit 237722f7f9
20 changed files with 241 additions and 37 deletions

View File

@@ -3,7 +3,7 @@ import { APP_INTERCEPTOR } from '@nestjs/core';
import { AppController } from './app.controller';
import { CacheInterceptor, CacheModule } from './cache';
import { ConfigModule } from './config';
import { ConfigModule, SERVER_FLAVOR } from './config';
import { EventModule } from './event';
import { BusinessModules } from './modules';
import { AuthModule } from './modules/auth';
@@ -29,6 +29,6 @@ const BasicModules = [
},
],
imports: [...BasicModules, ...BusinessModules],
controllers: [AppController],
controllers: SERVER_FLAVOR === 'selfhosted' ? [] : [AppController],
})
export class AppModule {}

View File

@@ -91,6 +91,7 @@ export interface AFFiNEConfig {
* @env NODE_ENV
*/
readonly env: string;
/**
* fast AFFiNE environment judge
*/

View File

@@ -49,6 +49,7 @@ const jwtKeyPair = (function () {
})();
export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
let isHttps: boolean | null = null;
const defaultConfig = {
serverId: 'affine-nestjs-server',
version: pkg.version,
@@ -56,6 +57,7 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
AFFINE_SERVER_PORT: ['port', 'int'],
AFFINE_SERVER_HOST: 'host',
AFFINE_SERVER_SUB_PATH: 'path',
AFFIHE_SERVER_HTTPS: 'https',
AFFINE_ENV: 'affineEnv',
DATABASE_URL: 'db.url',
ENABLE_CAPTCHA: ['auth.captcha.enable', 'boolean'],
@@ -117,7 +119,10 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
earlyAccessPreview: false,
},
get https() {
return !this.node.dev;
return isHttps ?? !this.node.dev;
},
set https(value: boolean) {
isHttps = value;
},
host: 'localhost',
port: 3010,

View File

@@ -33,6 +33,7 @@ import prismaInstrument from '@prisma/instrumentation';
const { PrismaInstrumentation } = prismaInstrument;
import { parseEnvValue } from '../config/def';
import { PrismaMetricProducer } from './prisma';
abstract class OpentelemetryFactor {
@@ -155,6 +156,9 @@ export function getMeter(name = 'business') {
}
export function start() {
if (parseEnvValue(process.env.DISABLE_TELEMETRY, 'boolean')) {
return;
}
const sdk = createSDK();
if (sdk) {

View File

@@ -179,7 +179,7 @@ export const NextAuthOptionsProvider: FactoryProvider<NextAuthOptions> = {
);
}
if (config.auth.oauthProviders.google) {
if (config.auth.oauthProviders.google?.enabled) {
nextAuthOptions.providers.push(
// @ts-expect-error esm interop issue
Google.default({

View File

@@ -186,15 +186,17 @@ export class NextAuthController {
}
let nextAuthTokenCookie: (CookieOption & { value: string }) | undefined;
const cookiePrefix = this.config.node.prod ? '__Secure-' : '';
const sessionCookieName = `${cookiePrefix}next-auth.session-token`;
const secureCookiePrefix = '__Secure-';
const sessionCookieName = `next-auth.session-token`;
// next-auth credentials login only support JWT strategy
// https://next-auth.js.org/configuration/providers/credentials
// let's store the session token in the database
if (
credentialsSignIn &&
(nextAuthTokenCookie = cookies?.find(
({ name }) => name === sessionCookieName
({ name }) =>
name === sessionCookieName ||
name === `${secureCookiePrefix}${sessionCookieName}`
))
) {
const cookieExpires = new Date();

View File

@@ -1,5 +1,8 @@
import { join } from 'node:path';
import { DynamicModule, Type } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { ServeStaticModule } from '@nestjs/serve-static';
import { SERVER_FLAVOR } from '../config';
import { GqlModule } from '../graphql.module';
@@ -29,7 +32,10 @@ switch (SERVER_FLAVOR) {
UsersModule,
SyncModule,
DocModule,
StorageModule
StorageModule,
ServeStaticModule.forRoot({
rootPath: join('/app', 'static'),
})
);
break;
case 'graphql':

View File

@@ -1,5 +1,17 @@
import { homedir } from 'node:os';
import { join } from 'node:path';
import { config } from 'dotenv';
import { SERVER_FLAVOR } from './config/default';
if (SERVER_FLAVOR === 'selfhosted') {
config({ path: join(homedir(), '.affine', '.env') });
} else {
config();
}
import 'reflect-metadata';
import 'dotenv/config';
import './affine';
import './affine.config';

View File

@@ -10,7 +10,9 @@ try {
storageModule =
process.arch === 'arm64'
? require('../../storage.arm64.node')
: require('../../storage.node');
: process.arch === 'arm'
? require('../../storage.armv7.node')
: require('../../storage.node');
}
export { storageModule as OctoBaseStorageModule };