mirror of
https://github.com/imgfloat/server.git
synced 2026-02-05 03:39:26 +00:00
Add version
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.imgfloat.app.controller;
|
||||
|
||||
import com.imgfloat.app.service.ChannelDirectoryService;
|
||||
import com.imgfloat.app.service.GitVersionService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
||||
@@ -14,9 +15,11 @@ import static org.springframework.http.HttpStatus.FORBIDDEN;
|
||||
public class ViewController {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ViewController.class);
|
||||
private final ChannelDirectoryService channelDirectoryService;
|
||||
private final GitVersionService gitVersionService;
|
||||
|
||||
public ViewController(ChannelDirectoryService channelDirectoryService) {
|
||||
public ViewController(ChannelDirectoryService channelDirectoryService, GitVersionService gitVersionService) {
|
||||
this.channelDirectoryService = channelDirectoryService;
|
||||
this.gitVersionService = gitVersionService;
|
||||
}
|
||||
|
||||
@org.springframework.web.bind.annotation.GetMapping("/")
|
||||
@@ -29,6 +32,7 @@ public class ViewController {
|
||||
model.addAttribute("adminChannels", channelDirectoryService.adminChannelsFor(login));
|
||||
return "dashboard";
|
||||
}
|
||||
model.addAttribute("gitVersion", gitVersionService.getVersion());
|
||||
return "index";
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.imgfloat.app.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
@Component
|
||||
public class GitVersionService {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GitVersionService.class);
|
||||
private final String version;
|
||||
|
||||
public GitVersionService() {
|
||||
this.version = resolveVersion();
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
private String resolveVersion() {
|
||||
Process process = null;
|
||||
try {
|
||||
process = new ProcessBuilder("git", "describe", "--tags", "--always")
|
||||
.redirectErrorStream(true)
|
||||
.start();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||
String result = reader.readLine();
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode == 0 && result != null && !result.isBlank()) {
|
||||
return result.trim();
|
||||
}
|
||||
LOG.warn("git describe returned exit code {} with output: {}", exitCode, result);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.warn("Unable to determine git version", e);
|
||||
if (process != null) {
|
||||
process.destroyForcibly();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warn("Interrupted while determining git version", e);
|
||||
if (process != null) {
|
||||
process.destroyForcibly();
|
||||
}
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user