package org.example.controller;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.example.WsServerApplication;
|
import org.example.common.ServerResponse;
|
import org.example.pojo.User;
|
import org.example.util.JwtUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.SpringApplication;
|
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@RestController
|
@RequestMapping("/api")
|
@CrossOrigin(origins = "*")
|
@Slf4j
|
public class RunController {
|
|
@Autowired
|
private ConfigurableApplicationContext context;
|
|
@PostMapping("/restart")
|
@ResponseBody
|
public ServerResponse restart(HttpServletRequest request) {
|
String token = request.getHeader("token");
|
User user = JwtUtil.verify(token);
|
if(user.getIsRoot() != 1){
|
return ServerResponse.createByErrorMsg("没有重启权限");
|
}
|
Thread restartThread = new Thread(() -> {
|
try {
|
Thread.sleep(1000); // 等待1秒钟,确保接口返回成功响应
|
SpringApplication.exit(context, () -> 0);
|
SpringApplication.run(WsServerApplication.class); // 替换成你的Spring Boot主类
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("重启失败");
|
}
|
});
|
restartThread.setDaemon(false);
|
restartThread.start();
|
return ServerResponse.createBySuccess("重启成功");
|
}
|
|
}
|