1
zj
2024-08-14 5578112574d523d639765dc491ea6e0c6dc51ded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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("重启成功");
    }
 
}