2022-07-06 11:02:36 +02:00
|
|
|
import Fastify from 'fastify';
|
|
|
|
import cors from '@fastify/cors';
|
|
|
|
import serve from '@fastify/static';
|
|
|
|
import env from '@fastify/env';
|
|
|
|
import cookie from '@fastify/cookie';
|
|
|
|
import path, { join } from 'path';
|
|
|
|
import autoLoad from '@fastify/autoload';
|
2022-08-25 10:04:46 +02:00
|
|
|
import { asyncExecShell, asyncSleep, isDev, listSettings, prisma, version } from './lib/common';
|
2022-07-06 11:02:36 +02:00
|
|
|
import { scheduler } from './lib/scheduler';
|
2022-08-17 11:03:44 +02:00
|
|
|
import compareVersions from 'compare-versions';
|
2022-08-26 06:29:06 +00:00
|
|
|
import Graceful from '@ladjs/graceful'
|
2022-07-06 11:02:36 +02:00
|
|
|
declare module 'fastify' {
|
|
|
|
interface FastifyInstance {
|
|
|
|
config: {
|
|
|
|
COOLIFY_APP_ID: string,
|
|
|
|
COOLIFY_SECRET_KEY: string,
|
|
|
|
COOLIFY_DATABASE_URL: string,
|
|
|
|
COOLIFY_SENTRY_DSN: string,
|
|
|
|
COOLIFY_IS_ON: string,
|
2022-08-11 16:34:39 +02:00
|
|
|
COOLIFY_WHITE_LABELED: string,
|
2022-07-06 11:02:36 +02:00
|
|
|
COOLIFY_WHITE_LABELED_ICON: string | null,
|
2022-08-11 16:34:39 +02:00
|
|
|
COOLIFY_AUTO_UPDATE: string,
|
2022-07-06 11:02:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const port = isDev ? 3001 : 3000;
|
|
|
|
const host = '0.0.0.0';
|
|
|
|
const fastify = Fastify({
|
2022-07-06 19:34:16 +02:00
|
|
|
logger: false,
|
|
|
|
trustProxy: true
|
2022-07-06 11:02:36 +02:00
|
|
|
});
|
|
|
|
const schema = {
|
|
|
|
type: 'object',
|
|
|
|
required: ['COOLIFY_SECRET_KEY', 'COOLIFY_DATABASE_URL', 'COOLIFY_IS_ON'],
|
|
|
|
properties: {
|
|
|
|
COOLIFY_APP_ID: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
COOLIFY_SECRET_KEY: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
COOLIFY_DATABASE_URL: {
|
|
|
|
type: 'string',
|
|
|
|
default: 'file:../db/dev.db'
|
|
|
|
},
|
|
|
|
COOLIFY_SENTRY_DSN: {
|
|
|
|
type: 'string',
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
COOLIFY_IS_ON: {
|
|
|
|
type: 'string',
|
|
|
|
default: 'docker'
|
|
|
|
},
|
|
|
|
COOLIFY_WHITE_LABELED: {
|
2022-08-11 16:22:07 +02:00
|
|
|
type: 'string',
|
2022-08-11 16:27:46 +02:00
|
|
|
default: 'false'
|
2022-07-06 11:02:36 +02:00
|
|
|
},
|
|
|
|
COOLIFY_WHITE_LABELED_ICON: {
|
|
|
|
type: 'string',
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
COOLIFY_AUTO_UPDATE: {
|
2022-08-11 16:22:07 +02:00
|
|
|
type: 'string',
|
2022-08-11 16:27:46 +02:00
|
|
|
default: 'false'
|
2022-07-06 11:02:36 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
2022-07-08 13:38:19 +02:00
|
|
|
schema,
|
|
|
|
dotenv: true
|
2022-07-06 11:02:36 +02:00
|
|
|
};
|
|
|
|
fastify.register(env, options);
|
|
|
|
if (!isDev) {
|
|
|
|
fastify.register(serve, {
|
|
|
|
root: path.join(__dirname, './public'),
|
|
|
|
preCompressed: true
|
|
|
|
});
|
2022-07-14 12:47:26 +00:00
|
|
|
fastify.setNotFoundHandler(async function (request, reply) {
|
2022-07-06 11:02:36 +02:00
|
|
|
if (request.raw.url && request.raw.url.startsWith('/api')) {
|
|
|
|
return reply.status(404).send({
|
|
|
|
success: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return reply.status(200).sendFile('index.html');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fastify.register(autoLoad, {
|
|
|
|
dir: join(__dirname, 'plugins')
|
|
|
|
});
|
|
|
|
fastify.register(autoLoad, {
|
|
|
|
dir: join(__dirname, 'routes')
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.register(cookie)
|
|
|
|
fastify.register(cors);
|
|
|
|
fastify.listen({ port, host }, async (err: any, address: any) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
console.log(`Coolify's API is listening on ${host}:${port}`);
|
2022-07-22 20:48:04 +00:00
|
|
|
await initServer();
|
2022-08-26 09:01:48 +02:00
|
|
|
|
2022-08-26 06:29:06 +00:00
|
|
|
const graceful = new Graceful({ brees: [scheduler] });
|
|
|
|
graceful.listen();
|
|
|
|
|
2022-08-25 12:25:46 +02:00
|
|
|
setInterval(async () => {
|
|
|
|
if (!scheduler.workers.has('deployApplication')) {
|
|
|
|
scheduler.run('deployApplication');
|
|
|
|
}
|
2022-08-26 06:29:06 +00:00
|
|
|
if (!scheduler.workers.has('infrastructure')) {
|
|
|
|
scheduler.run('infrastructure');
|
|
|
|
}
|
2022-08-25 12:25:46 +02:00
|
|
|
}, 2000)
|
2022-07-08 14:11:18 +02:00
|
|
|
|
2022-08-26 06:29:06 +00:00
|
|
|
// autoUpdater
|
2022-07-06 11:02:36 +02:00
|
|
|
setInterval(async () => {
|
2022-08-26 06:29:06 +00:00
|
|
|
scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:autoUpdater")
|
2022-08-12 09:38:11 +02:00
|
|
|
}, isDev ? 5000 : 60000 * 15)
|
2022-07-06 11:02:36 +02:00
|
|
|
|
2022-08-26 06:29:06 +00:00
|
|
|
// cleanupStorage
|
2022-07-08 14:11:18 +02:00
|
|
|
setInterval(async () => {
|
2022-08-26 06:29:06 +00:00
|
|
|
scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupStorage")
|
|
|
|
}, isDev ? 6000 : 60000 * 10)
|
|
|
|
|
|
|
|
// checkProxies
|
|
|
|
setInterval(async () => {
|
|
|
|
scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkProxies")
|
|
|
|
}, 10000)
|
|
|
|
|
|
|
|
// cleanupPrismaEngines
|
2022-08-26 09:01:48 +02:00
|
|
|
// setInterval(async () => {
|
|
|
|
// scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupPrismaEngines")
|
|
|
|
// }, 60000)
|
2022-07-06 11:02:36 +02:00
|
|
|
|
2022-07-25 19:16:30 +00:00
|
|
|
await getArch();
|
2022-07-22 20:48:04 +00:00
|
|
|
await getIPAddress();
|
2022-07-06 11:02:36 +02:00
|
|
|
});
|
2022-07-22 20:48:04 +00:00
|
|
|
async function getIPAddress() {
|
|
|
|
const { publicIpv4, publicIpv6 } = await import('public-ip')
|
|
|
|
try {
|
|
|
|
const settings = await listSettings();
|
|
|
|
if (!settings.ipv4) {
|
|
|
|
const ipv4 = await publicIpv4({ timeout: 2000 })
|
|
|
|
await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!settings.ipv6) {
|
|
|
|
const ipv6 = await publicIpv6({ timeout: 2000 })
|
|
|
|
await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } })
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
|
2022-07-22 20:48:04 +00:00
|
|
|
} catch (error) { }
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
async function initServer() {
|
|
|
|
try {
|
|
|
|
await asyncExecShell(`docker network create --attachable coolify`);
|
|
|
|
} catch (error) { }
|
2022-08-25 10:04:46 +02:00
|
|
|
try {
|
|
|
|
const isOlder = compareVersions('3.8.1', version);
|
2022-08-27 07:46:06 +00:00
|
|
|
if (isOlder === 1) {
|
2022-08-25 10:04:46 +02:00
|
|
|
await prisma.build.updateMany({ where: { status: { in: ['running', 'queued'] } }, data: { status: 'failed' } });
|
|
|
|
}
|
|
|
|
} catch (error) { }
|
2022-07-06 11:02:36 +02:00
|
|
|
}
|
2022-07-25 19:16:30 +00:00
|
|
|
async function getArch() {
|
|
|
|
try {
|
|
|
|
const settings = await prisma.setting.findFirst({})
|
|
|
|
if (settings && !settings.arch) {
|
|
|
|
await prisma.setting.update({ where: { id: settings.id }, data: { arch: process.arch } })
|
|
|
|
}
|
|
|
|
} catch (error) { }
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
|
|
|
|
|