mirror of https://github.com/halo-dev/halo
feat support restart application api.
parent
f0b94a621d
commit
a64b7ad58b
|
@ -1,9 +1,11 @@
|
|||
package run.halo.app;
|
||||
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
@ -25,12 +27,29 @@ import run.halo.app.repository.base.BaseRepositoryImpl;
|
|||
@EnableJpaRepositories(basePackages = "run.halo.app.repository", repositoryBaseClass = BaseRepositoryImpl.class)
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
private static ConfigurableApplicationContext context;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Customize the spring config location
|
||||
System.setProperty("spring.config.additional-location", "file:${user.home}/.halo/,file:${user.home}/halo-dev/");
|
||||
|
||||
// Run application
|
||||
SpringApplication.run(Application.class, args);
|
||||
context = SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart Application.
|
||||
*/
|
||||
public static void restart() {
|
||||
ApplicationArguments args = context.getBean(ApplicationArguments.class);
|
||||
|
||||
Thread thread = new Thread(() -> {
|
||||
context.close();
|
||||
context = SpringApplication.run(Application.class, args.getSourceArgs());
|
||||
});
|
||||
|
||||
thread.setDaemon(false);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,6 +4,7 @@ import io.swagger.annotations.ApiOperation;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import run.halo.app.Application;
|
||||
import run.halo.app.cache.lock.CacheLock;
|
||||
import run.halo.app.model.dto.EnvironmentDTO;
|
||||
import run.halo.app.model.dto.StatisticDTO;
|
||||
|
@ -100,9 +101,20 @@ public class AdminController {
|
|||
adminService.updateAdminAssets();
|
||||
}
|
||||
|
||||
@GetMapping("spring/logs")
|
||||
@ApiOperation("Get application logs")
|
||||
public BaseResponse<String> getSpringLogs() {
|
||||
return BaseResponse.ok(HttpStatus.OK.getReasonPhrase(), adminService.getSpringLogs());
|
||||
@GetMapping("spring/application.yaml")
|
||||
@ApiOperation("Get application config content")
|
||||
public BaseResponse<String> getSpringApplicationConfig() {
|
||||
return BaseResponse.ok(HttpStatus.OK.getReasonPhrase(), adminService.getApplicationConfig());
|
||||
}
|
||||
|
||||
@PutMapping("spring/application.yaml/update")
|
||||
@ApiOperation("Update application config content")
|
||||
public void updateSpringApplicationConfig(@RequestParam(name = "content") String content) {
|
||||
adminService.updateApplicationConfig(content);
|
||||
}
|
||||
|
||||
@PostMapping("/spring/restart")
|
||||
public void restartApplication() {
|
||||
Application.restart();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ public interface AdminService {
|
|||
|
||||
int REFRESH_TOKEN_EXPIRED_DAYS = 30;
|
||||
|
||||
String LOGS_PATH = "logs/spring.log";
|
||||
String APPLICATION_CONFIG_NAME = "application.yaml";
|
||||
|
||||
/**
|
||||
* Authenticates.
|
||||
|
@ -84,9 +84,16 @@ public interface AdminService {
|
|||
void updateAdminAssets();
|
||||
|
||||
/**
|
||||
* Get spring logs.
|
||||
* Get application.yaml content.
|
||||
*
|
||||
* @return recently logs.
|
||||
* @return application.yaml content
|
||||
*/
|
||||
String getSpringLogs();
|
||||
String getApplicationConfig();
|
||||
|
||||
/**
|
||||
* Save application.yaml content.
|
||||
*
|
||||
* @param content new content
|
||||
*/
|
||||
void updateApplicationConfig(String content);
|
||||
}
|
||||
|
|
|
@ -40,6 +40,8 @@ import run.halo.app.utils.HaloUtils;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
@ -443,12 +445,22 @@ public class AdminServiceImpl implements AdminService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getSpringLogs() {
|
||||
File file = new File(haloProperties.getWorkDir(), LOGS_PATH);
|
||||
public String getApplicationConfig() {
|
||||
File file = new File(haloProperties.getWorkDir(), APPLICATION_CONFIG_NAME);
|
||||
if (!file.exists()) {
|
||||
return "暂无日志";
|
||||
return "";
|
||||
}
|
||||
FileReader reader = new FileReader(file);
|
||||
return reader.readString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateApplicationConfig(String content) {
|
||||
Path path = Paths.get(haloProperties.getWorkDir(), APPLICATION_CONFIG_NAME);
|
||||
try {
|
||||
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
throw new ServiceException("保存配置文件失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue