From 9ca76948dfb1ce648de21d4c2f6c6f0798a321fd Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 25 Feb 2024 00:15:24 +0100 Subject: [PATCH] Added version display - Added error route handling --- build.gradle | 3 ++ .../src/app/credits/credits.component.html | 8 +++- frontend/src/app/credits/credits.component.ts | 25 +++++++++++-- .../sharepulse/rest/ErrorRestController.java | 19 ++++++++++ .../sharepulse/rest/mappings/Version.java | 37 +++++++++++++++++++ 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 src/main/java/de/w665/sharepulse/rest/ErrorRestController.java create mode 100644 src/main/java/de/w665/sharepulse/rest/mappings/Version.java diff --git a/build.gradle b/build.gradle index d59f36b..c082c3d 100644 --- a/build.gradle +++ b/build.gradle @@ -40,6 +40,9 @@ dependencies { bootJar { manifest { + attributes( + 'Application-Version': project.version + ) } } diff --git a/frontend/src/app/credits/credits.component.html b/frontend/src/app/credits/credits.component.html index b65b6b4..6a83fd0 100644 --- a/frontend/src/app/credits/credits.component.html +++ b/frontend/src/app/credits/credits.component.html @@ -15,11 +15,15 @@

Version:

-

1.0.0

+

+ + {{ version }} +

API-Status:

-

online

+ +

online

Libraries:

diff --git a/frontend/src/app/credits/credits.component.ts b/frontend/src/app/credits/credits.component.ts index f23db40..2aacf5b 100644 --- a/frontend/src/app/credits/credits.component.ts +++ b/frontend/src/app/credits/credits.component.ts @@ -1,19 +1,26 @@ import { Component } from '@angular/core'; import {RouterLink} from "@angular/router"; import {LegalService} from "../../service/legalService"; +import axios from "axios"; +import {DevelopmentStore} from "../../store/DevelopmentStore"; +import {NgIf} from "@angular/common"; @Component({ selector: 'app-credits', standalone: true, - imports: [ - RouterLink - ], + imports: [ + RouterLink, + NgIf + ], templateUrl: './credits.component.html', styleUrl: './credits.component.scss' }) export class CreditsComponent { - constructor(private legalService: LegalService) { + version: string = ''; + + constructor(private legalService: LegalService, private developmentStore: DevelopmentStore) { + this.getVersion(); } openPrivacyPolicyModal() { @@ -23,4 +30,14 @@ export class CreditsComponent { openTermsOfUseModal() { this.legalService.openTermsOfUse(); } + + getVersion() { + axios.get(this.developmentStore.getBaseUrl() + 'api/v1/version') + .then((response) => { + this.version = response.data; + }) + .catch((error) => { + console.log(error); + }); + } } diff --git a/src/main/java/de/w665/sharepulse/rest/ErrorRestController.java b/src/main/java/de/w665/sharepulse/rest/ErrorRestController.java new file mode 100644 index 0000000..487a54e --- /dev/null +++ b/src/main/java/de/w665/sharepulse/rest/ErrorRestController.java @@ -0,0 +1,19 @@ +package de.w665.sharepulse.rest; + +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * This class is used to redirect the user to the home page if an endpoint is not found. + */ +@RestController +public class ErrorRestController implements ErrorController { + @RequestMapping("/error") + public String handleError() { + return ""; + } +} diff --git a/src/main/java/de/w665/sharepulse/rest/mappings/Version.java b/src/main/java/de/w665/sharepulse/rest/mappings/Version.java new file mode 100644 index 0000000..df31b41 --- /dev/null +++ b/src/main/java/de/w665/sharepulse/rest/mappings/Version.java @@ -0,0 +1,37 @@ +package de.w665.sharepulse.rest.mappings; + +import com.rethinkdb.net.Response; +import de.w665.sharepulse.rest.ApiRestController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +@RestController +public class Version extends ApiRestController { + + private final ResourceLoader resourceLoader; + + @Autowired + public Version(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + + @GetMapping("/version") + public ResponseEntity getVersion() throws Exception { + Resource resource = resourceLoader.getResource("classpath:META-INF/MANIFEST.MF"); + Manifest manifest = new Manifest(resource.getInputStream()); + Attributes attr = manifest.getMainAttributes(); + String version = attr.getValue("Application-Version"); + String response = version != null ? version : "Version is only available in production builds"; + return new ResponseEntity<>(response, HttpStatus.OK); + } +}