seed forges on server start
This commit is contained in:
parent
0f52823461
commit
47827494e3
@ -31,23 +31,3 @@ CREATE TABLE repositories (
|
|||||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
UNIQUE(forge_id, forge_repo_id)
|
UNIQUE(forge_id, forge_repo_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
INSERT INTO forges (forge_id, display_name, base_url)
|
|
||||||
VALUES (0, 'gitea', 'http://localhost:3000');
|
|
||||||
|
|
||||||
INSERT INTO users (forge_id, forge_user_id, access_token)
|
|
||||||
VALUES (0, 'testuser', 'dummy_token');
|
|
||||||
|
|
||||||
|
|
||||||
INSERT INTO repositories (forge_id, forge_repo_id, owner_id, repo_name, description, clone_url, ssh_url, html_url, is_private)
|
|
||||||
VALUES
|
|
||||||
(0, '1', 1, 'my-awesome-project', 'A sample project for testing MOLCI',
|
|
||||||
'http://localhost:3000/testuser/my-awesome-project.git',
|
|
||||||
'git@localhost:testuser/my-awesome-project.git',
|
|
||||||
'http://localhost:3000/testuser/my-awesome-project', false),
|
|
||||||
|
|
||||||
(0, '2', 1,'molci-frontend', 'Frontend for MOLCI CI system',
|
|
||||||
'http://localhost:3000/testuser/molci-frontend.git',
|
|
||||||
'git@localhost:testuser/molci-frontend.git',
|
|
||||||
'http://localhost:3000/testuser/molci-frontend', false);
|
|
||||||
|
|||||||
@ -4,10 +4,11 @@ import express, {
|
|||||||
} from "express";
|
} from "express";
|
||||||
import session from "express-session";
|
import session from "express-session";
|
||||||
import { OpenAPIBackend, type Request } from "openapi-backend";
|
import { OpenAPIBackend, type Request } from "openapi-backend";
|
||||||
import { connectDB } from "./config/db";
|
import { connectDB, pool } from "./config/db";
|
||||||
import { config, isProduction } from "./config/env";
|
import { config, isProduction } from "./config/env";
|
||||||
import authRouter from "./routes/auth";
|
import authRouter from "./routes/auth";
|
||||||
import { createForge } from "./services/forges";
|
import { createForge } from "./services/forges";
|
||||||
|
import { FORGE_IDS } from "./services/forges/constants";
|
||||||
import { getAccessToken } from "./types/user";
|
import { getAccessToken } from "./types/user";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -29,6 +30,41 @@ app.use(
|
|||||||
|
|
||||||
app.use("/auth", authRouter);
|
app.use("/auth", authRouter);
|
||||||
|
|
||||||
|
async function seedForges() {
|
||||||
|
for (const forgeType of config.FORGE_TYPES) {
|
||||||
|
const forgeId = FORGE_IDS[forgeType];
|
||||||
|
if (forgeId === undefined) {
|
||||||
|
console.warn(`Skipping unknown forge type: ${forgeType}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let baseUrl: string;
|
||||||
|
switch (forgeType) {
|
||||||
|
case "gitea":
|
||||||
|
baseUrl = config.GITEA_URL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.warn(`Forge type ${forgeType} not supported`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO forges (forge_id, display_name, base_url)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT (forge_id)
|
||||||
|
DO UPDATE SET base_url = EXCLUDED.base_url`,
|
||||||
|
[forgeId, forgeType, baseUrl],
|
||||||
|
);
|
||||||
|
console.log(`Forge seeded: ${forgeType} (${baseUrl})`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed to seed forge ${forgeType}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenAPI backend
|
||||||
const api = new OpenAPIBackend({
|
const api = new OpenAPIBackend({
|
||||||
definition: "./openapi.yaml",
|
definition: "./openapi.yaml",
|
||||||
validate: true,
|
validate: true,
|
||||||
@ -69,7 +105,9 @@ api.register({
|
|||||||
api.init();
|
api.init();
|
||||||
app.use((req, res) => api.handleRequest(req as Request, req, res));
|
app.use((req, res) => api.handleRequest(req as Request, req, res));
|
||||||
|
|
||||||
connectDB();
|
async function startServer() {
|
||||||
|
await connectDB();
|
||||||
|
await seedForges();
|
||||||
|
|
||||||
const PORT = config.APP_PORT;
|
const PORT = config.APP_PORT;
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
@ -77,3 +115,9 @@ app.listen(PORT, () => {
|
|||||||
`Server running in ${isProduction ? "production" : "development"} mode on port ${PORT}`,
|
`Server running in ${isProduction ? "production" : "development"} mode on port ${PORT}`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
startServer().catch((err) => {
|
||||||
|
console.error("Failed to start server:", err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user