提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2018 Alibaba Group Holding Ltd. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
|
|
17 |
package com.alibaba.nacos.console.controller; |
|
18 |
|
|
19 |
import com.alibaba.nacos.common.model.RestResult; |
|
20 |
import com.alibaba.nacos.common.model.RestResultUtils; |
|
21 |
import com.alibaba.nacos.console.paramcheck.ConsoleDefaultHttpParamExtractor; |
|
22 |
import com.alibaba.nacos.core.paramcheck.ExtractorManager; |
|
23 |
import com.alibaba.nacos.sys.env.EnvUtil; |
|
24 |
import com.alibaba.nacos.sys.module.ModuleState; |
|
25 |
import com.alibaba.nacos.sys.module.ModuleStateHolder; |
|
26 |
import com.alibaba.nacos.sys.utils.DiskUtils; |
|
27 |
import org.springframework.http.ResponseEntity; |
|
28 |
import org.springframework.web.bind.annotation.GetMapping; |
|
29 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
30 |
import org.springframework.web.bind.annotation.RequestParam; |
|
31 |
import org.springframework.web.bind.annotation.RestController; |
|
32 |
|
|
33 |
import java.io.File; |
|
34 |
import java.util.HashMap; |
|
35 |
import java.util.Map; |
|
36 |
|
|
37 |
import static com.alibaba.nacos.common.utils.StringUtils.FOLDER_SEPARATOR; |
|
38 |
import static com.alibaba.nacos.common.utils.StringUtils.TOP_PATH; |
|
39 |
import static com.alibaba.nacos.common.utils.StringUtils.WINDOWS_FOLDER_SEPARATOR; |
|
40 |
|
|
41 |
/** |
|
42 |
* Server state controller. |
|
43 |
* |
|
44 |
* @author xingxuechao on:2019/2/27 11:17 AM |
|
45 |
*/ |
|
46 |
@RestController |
|
47 |
@RequestMapping("/v1/console/server") |
|
48 |
@ExtractorManager.Extractor(httpExtractor = ConsoleDefaultHttpParamExtractor.class) |
|
49 |
public class ServerStateController { |
|
50 |
|
|
51 |
private static final String ANNOUNCEMENT_FILE = "announcement.conf"; |
|
52 |
|
|
53 |
private static final String GUIDE_FILE = "console-guide.conf"; |
|
54 |
|
|
55 |
/** |
|
56 |
* Get server state of current server. |
|
57 |
* |
|
58 |
* @return state json. |
|
59 |
*/ |
|
60 |
@GetMapping("/state") |
|
61 |
public ResponseEntity<Map<String, String>> serverState() { |
|
62 |
Map<String, String> serverState = new HashMap<>(4); |
|
63 |
for (ModuleState each : ModuleStateHolder.getInstance().getAllModuleStates()) { |
|
64 |
each.getStates().forEach((s, o) -> serverState.put(s, null == o ? null : o.toString())); |
|
65 |
} |
|
66 |
return ResponseEntity.ok().body(serverState); |
|
67 |
} |
|
68 |
|
|
69 |
@GetMapping("/announcement") |
|
70 |
public RestResult<String> getAnnouncement( |
|
71 |
@RequestParam(required = false, name = "language", defaultValue = "zh-CN") String language) { |
|
72 |
String file = ANNOUNCEMENT_FILE.substring(0, ANNOUNCEMENT_FILE.length() - 5) + "_" + language + ".conf"; |
|
73 |
if (file.contains(TOP_PATH) || file.contains(FOLDER_SEPARATOR) || file.contains(WINDOWS_FOLDER_SEPARATOR)) { |
|
74 |
throw new IllegalArgumentException("Invalid filename"); |
|
75 |
} |
|
76 |
File announcementFile = new File(EnvUtil.getConfPath(), file); |
|
77 |
String announcement = null; |
|
78 |
if (announcementFile.exists() && announcementFile.isFile()) { |
|
79 |
announcement = DiskUtils.readFile(announcementFile); |
|
80 |
} |
|
81 |
return RestResultUtils.success(announcement); |
|
82 |
} |
|
83 |
|
|
84 |
@GetMapping("/guide") |
|
85 |
public RestResult<String> getConsoleUiGuide() { |
|
86 |
File guideFile = new File(EnvUtil.getConfPath(), GUIDE_FILE); |
|
87 |
String guideInformation = null; |
|
88 |
if (guideFile.exists() && guideFile.isFile()) { |
|
89 |
guideInformation = DiskUtils.readFile(guideFile); |
|
90 |
} |
|
91 |
return RestResultUtils.success(guideInformation); |
|
92 |
} |
|
93 |
} |