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", "homepage": "https://github.com/imputnet/cobalt#readme",
"dependencies": { "dependencies": {
"@imput/version-info": "workspace:^",
"content-disposition-header": "0.6.0", "content-disposition-header": "0.6.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",

View file

@ -3,17 +3,13 @@ import "./misc/alias-envs.js";
import express from "express"; 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 path from 'path';
import { fileURLToPath } from 'url'; 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 app = express();
const gitBranch = getCurrentBranch();
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename).slice(0, -4); const __dirname = path.dirname(__filename).slice(0, -4);
@ -22,7 +18,7 @@ app.disable('x-powered-by');
if (env.apiURL) { if (env.apiURL) {
const { runAPI } = await import('./core/api.js'); const { runAPI } = await import('./core/api.js');
runAPI(express, app, gitCommit, gitBranch, __dirname) runAPI(express, app, __dirname)
} else { } else {
console.log( console.log(
Red(`cobalt wasn't configured yet or configuration is invalid.\n`) 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 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 supportedAudio = ["mp3", "ogg", "wav", "opus"];
const ffmpegArgs = { const ffmpegArgs = {
@ -46,6 +40,4 @@ export {
genericUserAgent, genericUserAgent,
supportedAudio, supportedAudio,
ffmpegArgs, ffmpegArgs,
version,
} }

View file

@ -2,7 +2,9 @@ import cors from "cors";
import rateLimit from "express-rate-limit"; import rateLimit from "express-rate-limit";
import { setGlobalDispatcher, ProxyAgent } from "undici"; 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 { generateHmac, generateSalt } from "../misc/crypto.js";
import { Bright, Cyan } from "../misc/console-text.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 match from "../processing/match.js";
import stream from "../stream/stream.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 acceptRegex = /^application\/json(; charset=utf-8)?$/;
const ipSalt = generateSalt(); const ipSalt = generateSalt();
@ -23,17 +33,16 @@ const corsConfig = env.corsWildcard ? {} : {
optionsSuccessStatus: 200 optionsSuccessStatus: 200
} }
export function runAPI(express, app, gitCommit, gitBranch, __dirname) { export function runAPI(express, app, __dirname) {
const startTime = new Date(); const startTime = new Date();
const startTimestamp = startTime.getTime(); const startTimestamp = startTime.getTime();
const serverInfo = { const serverInfo = {
version: version, version: version,
commit: gitCommit, git,
branch: gitBranch,
url: env.apiURL,
cors: Number(env.corsWildcard), cors: Number(env.corsWildcard),
startTime: `${startTimestamp}` url: env.apiURL,
startTime: `${startTimestamp}`,
} }
const apiLimiter = rateLimit({ const apiLimiter = rateLimit({
@ -226,10 +235,18 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
app.listen(env.apiPort, env.listenAddress, () => { app.listen(env.apiPort, env.listenAddress, () => {
console.log(`\n` + console.log(`\n` +
`${Cyan("cobalt")} API ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + Bright(Cyan("cobalt ")) + Bright("API ^_^") + "\n" +
`Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` +
`URL: ${Cyan(`${env.apiURL}`)}\n` + "~~~~~~\n" +
`Port: ${env.apiPort}\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) { export function loadJSON(path) {
try { try {
return JSON.parse(loadFile(path)) return JSON.parse(loadFile(path))
@ -14,7 +18,3 @@ export function loadJSON(path) {
return false return false
} }
} }
export function loadFile(path) {
return fs.readFileSync(join(root, path), 'utf-8')
}

View file

@ -6,8 +6,13 @@ settings:
importers: importers:
.: {}
api: api:
dependencies: dependencies:
'@imput/version-info':
specifier: workspace:^
version: link:../packages/version-info
content-disposition-header: content-disposition-header:
specifier: 0.6.0 specifier: 0.6.0
version: 0.6.0 version: 0.6.0
@ -61,6 +66,8 @@ importers:
specifier: ^0.2.2 specifier: ^0.2.2
version: 0.2.2 version: 0.2.2
packages/version-info: {}
web: web:
dependencies: dependencies:
'@fontsource-variable/noto-sans-mono': '@fontsource-variable/noto-sans-mono':