api: use version-info package & clean up start message

This commit is contained in:
wukko 2024-08-03 21:34:02 +06:00
parent 0d20ffc004
commit 40425ad3bf
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
7 changed files with 43 additions and 53 deletions

View file

@ -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",

View file

@ -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`)

View file

@ -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,
}

View file

@ -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"
)
})
}

View file

@ -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, '<br>');
commitInfo = d;
return d
}
export function getCurrentBranch() {
if (branch) return branch;
let b = execSync('git branch --show-current').toString().trim();
branch = b;
return b
}

View file

@ -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')
}

View file

@ -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':