|
@@ -0,0 +1,90 @@
|
|
|
+package com.qmth.ops.api.controller.export;
|
|
|
+
|
|
|
+import com.qmth.boot.api.annotation.Aac;
|
|
|
+import com.qmth.boot.api.annotation.BOOL;
|
|
|
+import com.qmth.boot.core.exception.NotFoundException;
|
|
|
+import com.qmth.boot.core.exception.ParameterException;
|
|
|
+import com.qmth.boot.core.exception.UnauthorizedException;
|
|
|
+import com.qmth.boot.tools.models.ByteArray;
|
|
|
+import com.qmth.ops.api.constants.OpsApiConstants;
|
|
|
+import com.qmth.ops.biz.domain.*;
|
|
|
+import com.qmth.ops.biz.service.*;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping(OpsApiConstants.EXPORT_URI_PREFIX + "/nginx_config")
|
|
|
+@Aac(auth = BOOL.FALSE)
|
|
|
+public class NginxConfigExportController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AppService appService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ModuleService moduleService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EnvService envService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private NginxConfigService nginxConfigService;
|
|
|
+
|
|
|
+ @RequestMapping("/{appCode}/{envCode}/{moduleCode}")
|
|
|
+ public void exportModeulNginxConfigFile(@PathVariable String appCode, @PathVariable String envCode,
|
|
|
+ @PathVariable String moduleCode, @RequestParam String secret, HttpServletResponse response)
|
|
|
+ throws IOException {
|
|
|
+ exportNginxConfigFile(appCode, envCode, moduleCode, secret, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/{appCode}/{envCode}")
|
|
|
+ public void exportAppNginxConfigFile(@PathVariable String appCode, @PathVariable String envCode,
|
|
|
+ @RequestParam String secret, HttpServletResponse response) throws IOException {
|
|
|
+ exportNginxConfigFile(appCode, envCode, null, secret, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void exportNginxConfigFile(String appCode, String envCode, String moduleCode, String exportSecret,
|
|
|
+ HttpServletResponse response) throws IOException {
|
|
|
+ User user = userService.findByExportSecret(exportSecret);
|
|
|
+ if (user == null || !user.hasRole(Role.OPS)) {
|
|
|
+ throw new UnauthorizedException("鉴权失败");
|
|
|
+ }
|
|
|
+ App app = appService.findByCode(appCode);
|
|
|
+ if (app == null) {
|
|
|
+ throw new ParameterException("app.code不存在");
|
|
|
+ }
|
|
|
+ Env env = envService.findByAppAndCode(app.getId(), envCode);
|
|
|
+ if (env == null) {
|
|
|
+ throw new ParameterException("env.code不存在");
|
|
|
+ }
|
|
|
+ Module module = null;
|
|
|
+ if (moduleCode != null) {
|
|
|
+ module = moduleService.findByAppAndCode(app.getId(), moduleCode);
|
|
|
+ if (module == null) {
|
|
|
+ throw new ParameterException("module.code不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ NginxConfig config = nginxConfigService.find(app, env, module);
|
|
|
+ if (config == null) {
|
|
|
+ throw new NotFoundException("nginx配置不存在");
|
|
|
+ }
|
|
|
+ String fileName = appCode;
|
|
|
+ if (module != null) {
|
|
|
+ fileName = fileName + "-" + moduleCode;
|
|
|
+ }
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/octet-stream; charset=gbk");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".conf");
|
|
|
+ ByteArray.fromString(config.getContent(), Charset.forName("gbk")).toOutputStream(response.getOutputStream());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|