chore(server): organize server configs (#6169)

This commit is contained in:
liuyi
2024-03-19 02:05:56 +00:00
parent a721b3887b
commit a4cd8d6ca3
7 changed files with 64 additions and 26 deletions

View File

@@ -61,14 +61,13 @@ MAILER_USER="auth"
MAILER_PASSWORD="auth" MAILER_PASSWORD="auth"
MAILER_HOST="localhost" MAILER_HOST="localhost"
MAILER_PORT="1025" MAILER_PORT="1025"
STRIPE_API_KEY=sk_live_1
STRIPE_WEBHOOK_KEY=1
``` ```
## Prepare prisma ## Prepare prisma
``` ```
yarn workspace @affine/server prisma db push yarn workspace @affine/server prisma db push
yarn workspace @affine/server data-migration run
``` ```
Note, you may need to do it again if db schema changed. Note, you may need to do it again if db schema changed.

View File

@@ -39,7 +39,15 @@ if (env.R2_OBJECT_STORAGE_ACCOUNT_ID) {
} }
AFFiNE.plugins.use('redis'); AFFiNE.plugins.use('redis');
AFFiNE.plugins.use('payment'); AFFiNE.plugins.use('payment', {
stripe: {
keys: {
// fake the key to ensure the server generate full GraphQL Schema even env vars are not set
APIKey: '1',
webhookKey: '1',
},
},
});
AFFiNE.plugins.use('oauth'); AFFiNE.plugins.use('oauth');
if (AFFiNE.deploy) { if (AFFiNE.deploy) {

View File

@@ -52,6 +52,18 @@ AFFiNE.port = 3010;
// /* The metrics will be available at `http://localhost:9464/metrics` with [Prometheus] format exported */ // /* The metrics will be available at `http://localhost:9464/metrics` with [Prometheus] format exported */
// AFFiNE.metrics.enabled = true; // AFFiNE.metrics.enabled = true;
// //
// /* Authentication Settings */
// /* User Signup password limitation */
// AFFiNE.auth.password = {
// minLength: 8,
// maxLength: 20,
// };
//
// /* How long the login session would last by default */
// AFFiNE.auth.session = {
// ttl: 15 * 24 * 60 * 60, // 15 days
// };
//
// /* GraphQL configurations that control the behavior of the Apollo Server behind */ // /* GraphQL configurations that control the behavior of the Apollo Server behind */
// /* @see https://www.apollographql.com/docs/apollo-server/api/apollo-server */ // /* @see https://www.apollographql.com/docs/apollo-server/api/apollo-server */
// AFFiNE.graphql = { // AFFiNE.graphql = {
@@ -84,15 +96,15 @@ AFFiNE.port = 3010;
// /* Redis Plugin */ // /* Redis Plugin */
// /* Provide caching and session storing backed by Redis. */ // /* Provide caching and session storing backed by Redis. */
// /* Useful when you deploy AFFiNE server in a cluster. */ // /* Useful when you deploy AFFiNE server in a cluster. */
AFFiNE.plugins.use('redis', { // AFFiNE.plugins.use('redis', {
/* override options */ // /* override options */
}); // });
// //
// //
// /* Payment Plugin */ // /* Payment Plugin */
AFFiNE.plugins.use('payment', { // AFFiNE.plugins.use('payment', {
stripe: { keys: {}, apiVersion: '2023-10-16' }, // stripe: { keys: {}, apiVersion: '2023-10-16' },
}); // });
// //
// //
// /* Cloudflare R2 Plugin */ // /* Cloudflare R2 Plugin */

View File

@@ -5,12 +5,13 @@ function getAuthCredentialValidator() {
const email = z.string().email({ message: 'Invalid email address' }); const email = z.string().email({ message: 'Invalid email address' });
let password = z.string(); let password = z.string();
const minPasswordLength = AFFiNE.node.prod ? 8 : 1;
password = password password = password
.min(minPasswordLength, { .min(AFFiNE.auth.password.minLength, {
message: `Password must be ${minPasswordLength} or more charactors long`, message: `Password must be ${AFFiNE.auth.password.minLength} or more charactors long`,
}) })
.max(20, { message: 'Password must be 20 or fewer charactors long' }); .max(AFFiNE.auth.password.maxLength, {
message: `Password must be ${AFFiNE.auth.password.maxLength} or fewer charactors long`,
});
return z return z
.object({ .object({

View File

@@ -3,9 +3,8 @@ import '../prelude';
import { Logger } from '@nestjs/common'; import { Logger } from '@nestjs/common';
import { CommandFactory } from 'nest-commander'; import { CommandFactory } from 'nest-commander';
import { CliAppModule } from './app';
async function bootstrap() { async function bootstrap() {
const { CliAppModule } = await import('./app');
await CommandFactory.run(CliAppModule, new Logger()).catch(e => { await CommandFactory.run(CliAppModule, new Logger()).catch(e => {
console.error(e); console.error(e);
process.exit(1); process.exit(1);

View File

@@ -220,6 +220,20 @@ export interface AFFiNEConfig {
* authentication config * authentication config
*/ */
auth: { auth: {
password: {
/**
* The minimum and maximum length of the password when registering new users
*
* @default 8
*/
minLength: number;
/**
* The maximum length of the password
*
* @default 20
*/
maxLength: number;
};
session: { session: {
/** /**
* Application auth expiration time in seconds * Application auth expiration time in seconds

View File

@@ -77,7 +77,16 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
Object.values(DeploymentType) Object.values(DeploymentType)
); );
const isSelfhosted = deploymentType === DeploymentType.Selfhosted; const isSelfhosted = deploymentType === DeploymentType.Selfhosted;
const affine = {
canary: AFFINE_ENV === 'dev',
beta: AFFINE_ENV === 'beta',
stable: AFFINE_ENV === 'production',
};
const node = {
prod: NODE_ENV === 'production',
dev: NODE_ENV === 'development',
test: NODE_ENV === 'test',
};
const defaultConfig = { const defaultConfig = {
serverId: 'affine-nestjs-server', serverId: 'affine-nestjs-server',
serverName: isSelfhosted ? 'Self-Host Cloud' : 'AFFiNE Cloud', serverName: isSelfhosted ? 'Self-Host Cloud' : 'AFFiNE Cloud',
@@ -98,19 +107,11 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
ENV_MAP: {}, ENV_MAP: {},
AFFINE_ENV, AFFINE_ENV,
get affine() { get affine() {
return { return affine;
canary: AFFINE_ENV === 'dev',
beta: AFFINE_ENV === 'beta',
stable: AFFINE_ENV === 'production',
};
}, },
NODE_ENV, NODE_ENV,
get node() { get node() {
return { return node;
prod: NODE_ENV === 'production',
dev: NODE_ENV === 'development',
test: NODE_ENV === 'test',
};
}, },
get deploy() { get deploy() {
return !this.node.dev && !this.node.test; return !this.node.dev && !this.node.test;
@@ -150,6 +151,10 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
playground: true, playground: true,
}, },
auth: { auth: {
password: {
minLength: node.prod ? 8 : 1,
maxLength: 32,
},
session: { session: {
ttl: 15 * ONE_DAY_IN_SEC, ttl: 15 * ONE_DAY_IN_SEC,
}, },