Skip to content

Commit

Permalink
Show running version in the server log and in the client browser log
Browse files Browse the repository at this point in the history
  • Loading branch information
floscher committed Jun 19, 2024
1 parent ca6910d commit abc652b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
11 changes: 11 additions & 0 deletions client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
};
Expand Down
1 change: 1 addition & 0 deletions common/src/dto/AppSettingsDto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HyperlinkDto } from "./HyperlinkDto.js";

export type AppSettingsDto = {
appVersion: string | undefined;
githubRepositorySlug: string | undefined;
imprint: HyperlinkDto | undefined;
isProduction: boolean;
Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 10 additions & 9 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";

Expand Down Expand Up @@ -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) => {
Expand All @@ -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}`);
});
17 changes: 17 additions & 0 deletions server/src/settings.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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),
Expand Down

0 comments on commit abc652b

Please sign in to comment.