From 6ac2b27830be462a66a3d83aa1702a2ff97a7017 Mon Sep 17 00:00:00 2001 From: Manuel Holtgrewe Date: Fri, 1 Sep 2023 09:34:08 +0200 Subject: [PATCH] feat: put the current version into docker image and display (#28) --- .gitignore | 3 ++ backend/app/main.py | 19 ++++++++ frontend/src/api/__tests__/common.spec.ts | 14 ++++-- frontend/src/api/__tests__/misc.spec.ts | 21 +++++++++ frontend/src/api/annonars.ts | 8 ++-- frontend/src/api/common.ts | 5 ++- frontend/src/api/misc.ts | 20 +++++++++ frontend/src/components/HeaderDefault.vue | 14 ++++++ .../__tests__/HeaderDefault.spec.ts | 16 ++++++- .../__tests__/HeaderDetailPage.spec.ts | 2 +- .../src/stores/__tests__/geneInfo.spec.ts | 3 +- frontend/src/stores/__tests__/misc.spec.ts | 33 ++++++++++++++ frontend/src/stores/geneInfo.ts | 8 +--- frontend/src/stores/misc.ts | 43 +++++++++++++++++++ frontend/src/views/GeneDetailView.vue | 3 +- .../src/views/__tests__/AboutView.spec.ts | 14 +++++- .../src/views/__tests__/ContactView.spec.ts | 14 +++++- .../views/__tests__/GeneDetailView.spec.ts | 2 +- frontend/src/views/__tests__/HomeView.spec.ts | 35 +++++++-------- utils/docker/Dockerfile | 3 ++ utils/docker/build-docker.sh | 2 + utils/docker/empty-file-dont-remove | 0 22 files changed, 239 insertions(+), 43 deletions(-) create mode 100644 frontend/src/api/__tests__/misc.spec.ts create mode 100644 frontend/src/api/misc.ts create mode 100644 frontend/src/stores/__tests__/misc.spec.ts create mode 100644 frontend/src/stores/misc.ts create mode 100644 utils/docker/empty-file-dont-remove diff --git a/.gitignore b/.gitignore index 5144b746..8eb0cd09 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ .coverage coverage.lcov +# Version file +/VERSION + # Environment variables .env diff --git a/backend/app/main.py b/backend/app/main.py index ee6cb9a3..0ef6a98d 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,5 +1,6 @@ import os import pathlib +import subprocess import sys import httpx @@ -25,6 +26,14 @@ BACKEND_PREFIX_MEHARI = env.get("REEV_BACKEND_PREFIX_MEHARI", "http://mehari:8080") #: Prefix for the backend of viguno service BACKEND_PREFIX_VIGUNO = env.get("REEV_BACKEND_PREFIX_VIGUNO", "http://viguno:8080") +#: Path to REEV version file. +VERSION_FILE = env.get("REEV_VERSION_FILE", "/VERSION") +#: The REEV version from the file (``None`` if to load dynamically from git) +REEV_VERSION = None +# Try to obtain version from file, otherwise keep it at ``None`` +if os.path.exists(VERSION_FILE): + with open(VERSION_FILE) as f: + REEV_VERSION = f.read().strip() or None app = FastAPI() @@ -82,6 +91,16 @@ async def reverse_proxy(request: Request) -> Response: app.add_route("/proxy/{path:path}", reverse_proxy, methods=["GET", "POST"]) +# Register app for returning REEV version. +@app.get("/version") +async def version(): + if REEV_VERSION: + version = REEV_VERSION + else: + version = subprocess.check_output(["git", "describe", "--tags", "--dirty"]).strip() + return Response(content=version) + + # Register route for favicon. @app.get("/favicon.ico") async def favicon(): diff --git a/frontend/src/api/__tests__/common.spec.ts b/frontend/src/api/__tests__/common.spec.ts index 38c974f0..e341d0df 100644 --- a/frontend/src/api/__tests__/common.spec.ts +++ b/frontend/src/api/__tests__/common.spec.ts @@ -1,11 +1,17 @@ import { describe, it, expect } from 'vitest' -import { API_BASE_PREFIX } from '../common' +import { API_BASE_PREFIX, API_PROXY_BASE_PREFIX } from '../common' -describe('API_BASE_PREFIX constant', () => { - it('returns the correct API base prefix in production mode', () => { +describe('constants', () => { + it('returns the correct proxy API base prefix in production mode', () => { const originalMode = import.meta.env.MODE - expect(API_BASE_PREFIX).toBe('/proxy/annonars') + expect(API_BASE_PREFIX).toBe('/') + import.meta.env.MODE = originalMode + }) + + it('returns the correct proxy API base prefix in production mode', () => { + const originalMode = import.meta.env.MODE + expect(API_PROXY_BASE_PREFIX).toBe('/proxy/') import.meta.env.MODE = originalMode }) }) diff --git a/frontend/src/api/__tests__/misc.spec.ts b/frontend/src/api/__tests__/misc.spec.ts new file mode 100644 index 00000000..491d2bad --- /dev/null +++ b/frontend/src/api/__tests__/misc.spec.ts @@ -0,0 +1,21 @@ +import { beforeEach, describe, it, expect, vi } from 'vitest' +import createFetchMock from 'vitest-fetch-mock' + +import { MiscClient } from '../misc' + +const fetchMocker = createFetchMock(vi) + +describe('Misc Client', () => { + beforeEach(() => { + fetchMocker.enableMocks() + fetchMocker.resetMocks() + }) + + it('fetches version info correctly', async () => { + fetchMocker.mockResponseOnce('v0.0.0') + + const client = new MiscClient() + const result = await client.fetchVersion() + expect(result).toEqual('v0.0.0') + }) +}) diff --git a/frontend/src/api/annonars.ts b/frontend/src/api/annonars.ts index 4df593f4..bd65b065 100644 --- a/frontend/src/api/annonars.ts +++ b/frontend/src/api/annonars.ts @@ -1,18 +1,16 @@ -import { API_BASE_PREFIX } from '@/api/common' - -const API_BASE_URL = `${API_BASE_PREFIX}/` +import { API_PROXY_BASE_PREFIX } from '@/api/common' export class AnnonarsClient { private apiBaseUrl: string private csrfToken: string | null constructor(apiBaseUrl?: string, csrfToken?: string) { - this.apiBaseUrl = apiBaseUrl ?? API_BASE_URL + this.apiBaseUrl = apiBaseUrl ?? `${API_PROXY_BASE_PREFIX}annonars` this.csrfToken = csrfToken ?? null } async fetchGeneInfo(hgncId: string): Promise { - const response = await fetch(`${this.apiBaseUrl}genes/info?hgnc_id=${hgncId}`, { + const response = await fetch(`${this.apiBaseUrl}/genes/info?hgnc_id=${hgncId}`, { method: 'GET' }) return await response.json() diff --git a/frontend/src/api/common.ts b/frontend/src/api/common.ts index 1af5c18b..1b1627b0 100644 --- a/frontend/src/api/common.ts +++ b/frontend/src/api/common.ts @@ -1,2 +1,3 @@ -export const API_BASE_PREFIX = - import.meta.env.MODE == 'development' ? '//localhost:8080/proxy/annonars' : '/proxy/annonars' +export const API_BASE_PREFIX = import.meta.env.MODE == 'development' ? '//localhost:8080/' : '/' + +export const API_PROXY_BASE_PREFIX = `${API_BASE_PREFIX}proxy/` diff --git a/frontend/src/api/misc.ts b/frontend/src/api/misc.ts new file mode 100644 index 00000000..dac0d2fa --- /dev/null +++ b/frontend/src/api/misc.ts @@ -0,0 +1,20 @@ +import { API_BASE_PREFIX } from '@/api/common' + +const API_BASE_URL = API_BASE_PREFIX + +export class MiscClient { + private apiBaseUrl: string + private csrfToken: string | null + + constructor(apiBaseUrl?: string, csrfToken?: string) { + this.apiBaseUrl = apiBaseUrl ?? API_BASE_URL + this.csrfToken = csrfToken ?? null + } + + async fetchVersion(): Promise { + const response = await fetch(`${this.apiBaseUrl}version`, { + method: 'GET' + }) + return await response.text() + } +} diff --git a/frontend/src/components/HeaderDefault.vue b/frontend/src/components/HeaderDefault.vue index b5ade63b..d3bfe5a2 100644 --- a/frontend/src/components/HeaderDefault.vue +++ b/frontend/src/components/HeaderDefault.vue @@ -1,9 +1,23 @@ + +