From abc652ba4bf68dd0bdb416831353f9be42c92d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=A4fer?= Date: Wed, 19 Jun 2024 13:42:11 +0200 Subject: [PATCH] Show running version in the server log and in the client browser log --- client/src/App.vue | 11 +++++++++++ common/src/dto/AppSettingsDto.ts | 1 + server/package.json | 3 ++- server/src/server.ts | 19 ++++++++++--------- server/src/settings.ts | 17 +++++++++++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/client/src/App.vue b/client/src/App.vue index 68d88b6..0e79893 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -127,6 +127,17 @@ const appData: AppSettingsDto = (JSON.parse(document.getElementById("app-data")? runMode: "production", }; +console.log( + `%c ███████ + █ ███ █ + ██ █ ██ fuBlog + ███████ + ██ █ ██ ${(appData.isProduction ? appData.appVersion ?? "‹unknown›" : "development version") + " "} + █ ███ █ + ███████`, + `color:#ff9c00;background:rgb(48, 48, 48);display:inline-block;padding:.5em 0`, +); + const setOperator = (operator: string) => { router.replace({ query: { ...route.query, operator: operator } }); }; diff --git a/common/src/dto/AppSettingsDto.ts b/common/src/dto/AppSettingsDto.ts index 83466bc..1c29358 100644 --- a/common/src/dto/AppSettingsDto.ts +++ b/common/src/dto/AppSettingsDto.ts @@ -1,6 +1,7 @@ import { HyperlinkDto } from "./HyperlinkDto.js"; export type AppSettingsDto = { + appVersion: string | undefined; githubRepositorySlug: string | undefined; imprint: HyperlinkDto | undefined; isProduction: boolean; diff --git a/server/package.json b/server/package.json index ccf70b4..69ab745 100644 --- a/server/package.json +++ b/server/package.json @@ -5,7 +5,8 @@ "main": "server.js", "scripts": { "start": "NODE_ENV='development' NODE_EXTRA_CA_CERTS=../certs/ca.crt nodemon -r dotenv/config ./src/server.ts", - "build": "tsc", + "write-app-version": "git describe --always --dirty > dist/app_version.txt", + "build": "npm run write-app-version && tsc", "run-prod": "node ./dist/server.js", "lint": "prettier --check 'src/**/*.(ts|vue)' && eslint src/**/*.ts", "lintfix": "prettier 'src/**/*.(ts|vue)' --write && eslint src/**/*.ts --fix", diff --git a/server/src/server.ts b/server/src/server.ts index db84f16..83ee154 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -1,6 +1,6 @@ import cors from "cors"; import express, { Application, NextFunction, Request, Response } from "express"; -import * as fs from "fs"; +import { readFile, writeFileSync } from "fs"; import path, { dirname } from "path"; import { fileURLToPath } from "url"; import { corsOptions } from "./config/cors-config.js"; @@ -15,7 +15,6 @@ import fileRoutes from "./routes/file.js"; import postRoutes from "./routes/posts.js"; import utilityRoutes from "./routes/utility.js"; import { errorHandler } from "./service/error-handler.js"; -import { generateShareImage } from "./service/opengraph.js"; import { initDatabase } from "./service/testdata-generator.js"; import { AppSettings, ClientSettings, DatabaseSettings, ServerSettings } from "./settings.js"; @@ -74,7 +73,7 @@ async function handleUncaught(error: Error) { // in production serve the built vue-app from static public folder: if (AppSettings.IS_PRODUCTION) { const indexResponse = (req: Request, res: Response) => { - fs.readFile( + readFile( path.join(__dirname, "public/index.html"), // { encoding: "utf-8" }, (err, data) => { @@ -90,13 +89,15 @@ if (AppSettings.IS_PRODUCTION) { app.use(express.static("./public", { redirect: false, index: false })); app.get("*", indexResponse); } else { - fs.writeFileSync("../client/.env.development", "VITE_APP_DATA=" + JSON.stringify(AppSettings.DTO), { encoding: "utf-8" }); + writeFileSync("../client/.env.development", "VITE_APP_DATA=" + JSON.stringify(AppSettings.DTO), { encoding: "utf-8" }); } app.listen(ServerSettings.PORT, () => { - logger.info(`fuBlog server running in ${AppSettings.RUN_MODE} mode on port: ${ServerSettings.PORT}`); - if (!AppSettings.IS_PRODUCTION) { - logger.info(`Connected to Postgres DB at ${DatabaseSettings.HOST}:${DatabaseSettings.PORT}`); - logger.info(`Client: ${ClientSettings.BASE_URL}`); - } + logger.info( + `▶️ fuBlog server${AppSettings.IS_PRODUCTION ? ` version ${AppSettings.APP_VERSION ?? "‹unknown›"}` : ""} running in ${ + AppSettings.RUN_MODE + } mode on port: ${ServerSettings.PORT}`, + ); + logger.info(`🗄️ Expecting Postgres DB at ${DatabaseSettings.HOST}:${DatabaseSettings.PORT}`); + logger.info(`🖥️ Expecting to be published at ${ClientSettings.BASE_URL}`); }); diff --git a/server/src/settings.ts b/server/src/settings.ts index 1d74c0f..bff7f48 100644 --- a/server/src/settings.ts +++ b/server/src/settings.ts @@ -1,5 +1,9 @@ import { AppSettingsDto, asHyperlinkDto, isOAuthType, OAUTH_TYPES, OAuthProvider, OAuthType } from "@fumix/fu-blog-common"; +import console from "console"; +import { readFileSync } from "fs"; +import path, { dirname } from "path"; import * as process from "process"; +import { fileURLToPath } from "url"; import { loadOAuthProvidersFromEnv } from "./load-oauth-providers-from-env.js"; export class AppSettings { @@ -11,7 +15,20 @@ export class AppSettings { static readonly MAIN_WEBSITE_LABEL: string | undefined = process.env.APP_MAIN_WEBSITE_LABEL; static readonly MAIN_WEBSITE_URL: string | undefined = process.env.APP_MAIN_WEBSITE_URL; + static readonly APP_VERSION = (() => { + try { + return readFileSync(path.resolve(dirname(fileURLToPath(import.meta.url)), "app_version.txt"), "utf8").trim(); + } catch (e) { + if (AppSettings.IS_PRODUCTION) { + // in dev mode this is expected, so log not needed + console.debug("Could not read app version!"); + } + return undefined; + } + })(); + static readonly DTO: AppSettingsDto = { + appVersion: AppSettings.APP_VERSION, isProduction: AppSettings.IS_PRODUCTION, runMode: AppSettings.RUN_MODE, imprint: asHyperlinkDto(AppSettings.IMPRINT_LABEL, AppSettings.IMPRINT_URL),