Use env config for db

This commit is contained in:
deathg.rip 2024-03-23 14:07:14 -04:00
parent ad08d071bf
commit dad0365d08
7 changed files with 98 additions and 50 deletions

View File

@ -16,7 +16,13 @@ packages/backend-rs/target
coverage
# config
/.config
/.config/LICENSE
/.config/ci.yml
/.config/devenv.yml
/.config/docker_ci.env
/.config/docker_example.env
/.config/example.yml
/.config/helm_values_example.yml
# misskey
built

22
fly.toml Normal file
View File

@ -0,0 +1,22 @@
# fly.toml app configuration file generated for infinite-jetzt-firefish on 2024-03-21T21:20:45-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#
app = 'example-fly-firefish'
primary_region = 'bos'
[build]
[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ['firefish']
[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

View File

@ -2,7 +2,9 @@ import { loadConfig } from "./built/config.js";
import { createRedisConnection } from "./built/redis.js";
const config = loadConfig();
const redis = createRedisConnection(config);
const redis = createRedisConnection({
...config
});
redis.on("connect", () => redis.disconnect());
redis.on("error", (e) => {

View File

@ -188,12 +188,17 @@ export const entities = [
const log = process.env.NODE_ENV !== "production";
export const db = new DataSource({
type: "postgres",
host: config.db.host,
port: config.db.port,
username: config.db.user,
password: config.db.pass,
database: config.db.db,
...(process.env.DATABASE_URL ? {
url: process.env.DATABASE_URL
} : {
host: config.db.host,
port: config.db.port,
username: config.db.user,
password: config.db.pass,
database: config.db.db,
}),
extra: {
statement_timeout: 1000 * 30,
...config.db.extra,
@ -203,7 +208,10 @@ export const db = new DataSource({
cache: !config.db.disableCache
? {
type: "ioredis",
options: {
port: process.env.REDIS_URL, // typeorm passes "port" as the first argument to ioredis when using the "ioredis" cache driver so we can use it to pass the redis url
options: process.env.REDIS_URL ? {
keyPrefix: `${config.redis.prefix}:query:`,
} : {
host: config.redis.host,
port: config.redis.port,
family: config.redis.family == null ? 0 : config.redis.family,

View File

@ -6,16 +6,18 @@ export function createConnection() {
if (config.cacheServer) {
source = config.cacheServer;
}
return new Redis({
port: source.port,
host: source.host,
family: source.family ?? 0,
password: source.pass,
username: source.user ?? "default",
keyPrefix: `${source.prefix}:`,
db: source.db || 0,
tls: source.tls,
});
return new Redis(
process.env.REDIS_URL || {
port: source.port,
host: source.host,
family: source.family ?? 0,
password: source.pass,
username: source.user ?? "default",
keyPrefix: `${source.prefix}:`,
db: source.db || 0,
tls: source.tls,
}
);
}
export const subscriber = createConnection();

View File

@ -4,12 +4,16 @@ import { entities } from "./db/postgre.js";
export default new DataSource({
type: "postgres",
host: config.db.host,
port: config.db.port,
username: config.db.user,
password: config.db.pass,
database: config.db.db,
extra: config.db.extra,
...(process.env.DATABASE_URL ? {
url: process.env.DATABASE_URL
} : {
host: config.db.host,
port: config.db.port,
username: config.db.user,
password: config.db.pass,
database: config.db.db,
}),
...(config.db && { extra: config.db.extra }),
entities: entities,
migrations: ["built/migration/*.js"],
});

View File

@ -2,32 +2,36 @@ import Bull from "bull";
import config from "@/config/index.js";
export function initialize<T>(name: string, limitPerSec = -1) {
return new Bull<T>(name, {
redis: {
port: config.redis.port,
host: config.redis.host,
family: config.redis.family == null ? 0 : config.redis.family,
username: config.redis.user ?? "default",
password: config.redis.pass,
db: config.redis.db || 0,
tls: config.redis.tls,
},
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : "queue",
limiter:
limitPerSec > 0
? {
max: limitPerSec,
duration: 1000,
}
: undefined,
settings: {
stalledInterval: 60,
maxStalledCount: 2,
backoffStrategies: {
apBackoff,
},
},
});
return new Bull<T>(
name,
process.env.REDIS_URL,
{
redis: process.env.REDIS_URL ? undefined : {
port: config.redis.port,
host: config.redis.host,
family: config.redis.family == null ? 0 : config.redis.family,
username: config.redis.user ?? "default",
password: config.redis.pass,
db: config.redis.db || 0,
tls: config.redis.tls,
},
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : "queue",
limiter:
limitPerSec > 0
? {
max: limitPerSec,
duration: 1000,
}
: undefined,
settings: {
stalledInterval: 60,
maxStalledCount: 2,
backoffStrategies: {
apBackoff,
},
},
}
);
}
// ref. https://github.com/misskey-dev/misskey/pull/7635#issue-971097019