diff --git a/api/package.json b/api/package.json index f55ceb4a..a8416bb8 100644 --- a/api/package.json +++ b/api/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/imputnet/cobalt#readme", "dependencies": { + "@imput/version-info": "workspace:^", "content-disposition-header": "0.6.0", "cors": "^2.8.5", "dotenv": "^16.0.1", diff --git a/api/src/cobalt.js b/api/src/cobalt.js index 53a5d71d..eec452cc 100644 --- a/api/src/cobalt.js +++ b/api/src/cobalt.js @@ -3,17 +3,13 @@ import "./misc/alias-envs.js"; import express from "express"; -import { Bright, Green, Red } from "./misc/console-text.js"; -import { getCurrentBranch, shortCommit } from "./misc/current-commit.js"; -import { env } from "./config.js" - import path from 'path'; import { fileURLToPath } from 'url'; -const app = express(); +import { env } from "./config.js" +import { Bright, Green, Red } from "./misc/console-text.js"; -const gitCommit = shortCommit(); -const gitBranch = getCurrentBranch(); +const app = express(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename).slice(0, -4); @@ -22,7 +18,7 @@ app.disable('x-powered-by'); if (env.apiURL) { const { runAPI } = await import('./core/api.js'); - runAPI(express, app, gitCommit, gitBranch, __dirname) + runAPI(express, app, __dirname) } else { console.log( Red(`cobalt wasn't configured yet or configuration is invalid.\n`) diff --git a/api/src/config.js b/api/src/config.js index 634b2d7b..19e859f3 100644 --- a/api/src/config.js +++ b/api/src/config.js @@ -1,9 +1,3 @@ -import { loadJSON } from "./misc/load-from-fs.js"; - -const packageJson = loadJSON("./package.json"); - -const version = packageJson.version; - const genericUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"; const supportedAudio = ["mp3", "ogg", "wav", "opus"]; const ffmpegArgs = { @@ -46,6 +40,4 @@ export { genericUserAgent, supportedAudio, ffmpegArgs, - - version, } diff --git a/api/src/core/api.js b/api/src/core/api.js index ebdedb55..f69442eb 100644 --- a/api/src/core/api.js +++ b/api/src/core/api.js @@ -2,7 +2,9 @@ import cors from "cors"; import rateLimit from "express-rate-limit"; import { setGlobalDispatcher, ProxyAgent } from "undici"; -import { env, version } from "../config.js"; +import { getCommit, getBranch, getRemote, getVersion } from "@imput/version-info"; + +import { env } from "../config.js"; import { generateHmac, generateSalt } from "../misc/crypto.js"; import { Bright, Cyan } from "../misc/console-text.js"; @@ -15,6 +17,14 @@ import { extract } from "../processing/url.js"; import match from "../processing/match.js"; import stream from "../stream/stream.js"; +const git = { + branch: await getBranch(), + commit: await getCommit(), + remote: await getRemote(), +} + +const version = await getVersion(); + const acceptRegex = /^application\/json(; charset=utf-8)?$/; const ipSalt = generateSalt(); @@ -23,17 +33,16 @@ const corsConfig = env.corsWildcard ? {} : { optionsSuccessStatus: 200 } -export function runAPI(express, app, gitCommit, gitBranch, __dirname) { +export function runAPI(express, app, __dirname) { const startTime = new Date(); const startTimestamp = startTime.getTime(); const serverInfo = { version: version, - commit: gitCommit, - branch: gitBranch, - url: env.apiURL, + git, cors: Number(env.corsWildcard), - startTime: `${startTimestamp}` + url: env.apiURL, + startTime: `${startTimestamp}`, } const apiLimiter = rateLimit({ @@ -226,10 +235,18 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { app.listen(env.apiPort, env.listenAddress, () => { console.log(`\n` + - `${Cyan("cobalt")} API ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + - `Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` + - `URL: ${Cyan(`${env.apiURL}`)}\n` + - `Port: ${env.apiPort}\n` + Bright(Cyan("cobalt ")) + Bright("API ^_^") + "\n" + + + "~~~~~~\n" + + Bright("version: ") + version + "\n" + + Bright("commit: ") + git.commit + "\n" + + Bright("branch: ") + git.branch + "\n" + + Bright("remote: ") + git.remote + "\n" + + Bright("start time: ") + startTime.toUTCString() + "\n" + + "~~~~~~\n" + + + Bright("url: ") + Bright(Cyan(env.apiURL)) + "\n" + + Bright("port: ") + env.apiPort + "\n" ) }) } diff --git a/api/src/misc/current-commit.js b/api/src/misc/current-commit.js deleted file mode 100644 index f3c145f5..00000000 --- a/api/src/misc/current-commit.js +++ /dev/null @@ -1,23 +0,0 @@ -import { execSync } from "child_process"; - -let commit, commitInfo, branch; - -export function shortCommit() { - if (commit) return commit; - let c = execSync('git rev-parse --short HEAD').toString().trim(); - commit = c; - return c -} -export function getCommitInfo() { - if (commitInfo) return commitInfo; - let d = execSync(`git show -s --format='%s;;;%B'`).toString().trim().replace(/[\r\n]/gm, '\n').split(';;;'); - d[1] = d[1].replace(d[0], '').trim().toString().replace(/[\r\n]/gm, '
'); - commitInfo = d; - return d -} -export function getCurrentBranch() { - if (branch) return branch; - let b = execSync('git branch --show-current').toString().trim(); - branch = b; - return b -} diff --git a/api/src/misc/load-from-fs.js b/api/src/misc/load-from-fs.js index f91a20cb..4357b8dc 100644 --- a/api/src/misc/load-from-fs.js +++ b/api/src/misc/load-from-fs.js @@ -7,6 +7,10 @@ const root = join( '../../' ); +export function loadFile(path) { + return fs.readFileSync(join(root, path), 'utf-8') +} + export function loadJSON(path) { try { return JSON.parse(loadFile(path)) @@ -14,7 +18,3 @@ export function loadJSON(path) { return false } } - -export function loadFile(path) { - return fs.readFileSync(join(root, path), 'utf-8') -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cda1121c..9e57dcb0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,8 +6,13 @@ settings: importers: + .: {} + api: dependencies: + '@imput/version-info': + specifier: workspace:^ + version: link:../packages/version-info content-disposition-header: specifier: 0.6.0 version: 0.6.0 @@ -61,6 +66,8 @@ importers: specifier: ^0.2.2 version: 0.2.2 + packages/version-info: {} + web: dependencies: '@fontsource-variable/noto-sans-mono':