mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-21 22:50:42 +08:00
feat(ui): support show update button
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.reajason.javaweb.boot.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
*/
|
||||
@Configuration
|
||||
public class WebConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
factory.setConnectTimeout(3000);
|
||||
factory.setReadTimeout(3000);
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,20 @@
|
||||
package com.reajason.javaweb.boot.controller;
|
||||
|
||||
import com.reajason.javaweb.boot.entity.VersionInfo;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
@@ -16,10 +26,61 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class VersionController {
|
||||
|
||||
@Value("${spring.application.version}")
|
||||
String version;
|
||||
private String version;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public VersionController(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String version() {
|
||||
public VersionInfo version() {
|
||||
String latestVersion = getLatestGithubRelease();
|
||||
return VersionInfo.builder()
|
||||
.currentVersion(version)
|
||||
.latestVersion(latestVersion)
|
||||
.hasUpdate(!StringUtils.equals(version, latestVersion))
|
||||
.build();
|
||||
}
|
||||
|
||||
private String getLatestGithubRelease() {
|
||||
try {
|
||||
String latestVersion = tryFetchRelease("https://api.github.com");
|
||||
if (latestVersion != null) {
|
||||
return latestVersion;
|
||||
}
|
||||
latestVersion = tryFetchRelease("https://gh.llkk.cc/https://api.github.com");
|
||||
if (latestVersion != null) {
|
||||
return latestVersion;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
private String tryFetchRelease(String baseUrl) {
|
||||
String apiUrl = String.format("%s/repos/%s/%s/releases", baseUrl, "ReaJason", "MemShellParty");
|
||||
|
||||
ResponseEntity<List<Map<String, Object>>> response = restTemplate.exchange(
|
||||
apiUrl,
|
||||
HttpMethod.GET,
|
||||
null,
|
||||
new ParameterizedTypeReference<>() {
|
||||
}
|
||||
);
|
||||
if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
|
||||
List<Map<String, Object>> body = response.getBody();
|
||||
for (Map<String, Object> map : body) {
|
||||
String targetCommitish = (String) map.get("target_commitish");
|
||||
Boolean prerelease = (Boolean) map.get("prerelease");
|
||||
Boolean draft = (Boolean) map.get("draft");
|
||||
if ("master".equals(targetCommitish) && !prerelease && !draft) {
|
||||
String tagName = (String) map.get("name");
|
||||
return tagName.startsWith("v") ? tagName.substring(1) : tagName;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.reajason.javaweb.boot.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Builder;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class VersionInfo {
|
||||
private String currentVersion;
|
||||
private String latestVersion;
|
||||
private boolean hasUpdate;
|
||||
}
|
||||
Reference in New Issue
Block a user