Przeglądaj źródła

增加nginx配置管理/导出接口

luoshi 2 lat temu
rodzic
commit
2b9620c33a

+ 48 - 0
src/main/java/com/qmth/ops/api/controller/admin/NginxConfigController.java

@@ -0,0 +1,48 @@
+package com.qmth.ops.api.controller.admin;
+
+import com.qmth.ops.api.constants.OpsApiConstants;
+import com.qmth.ops.api.security.AdminSession;
+import com.qmth.ops.biz.domain.NginxConfig;
+import com.qmth.ops.biz.domain.Role;
+import com.qmth.ops.biz.service.AppService;
+import com.qmth.ops.biz.service.EnvService;
+import com.qmth.ops.biz.service.ModuleService;
+import com.qmth.ops.biz.service.NginxConfigService;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+
+@RestController
+@RequestMapping(OpsApiConstants.ADMIN_URI_PREFIX + "/nginx_config")
+public class NginxConfigController {
+
+    @Resource
+    private NginxConfigService nginxConfigService;
+
+    @Resource
+    private AppService appService;
+
+    @Resource
+    private ModuleService moduleService;
+
+    @Resource
+    private EnvService envService;
+
+    @PostMapping("/find")
+    public NginxConfig find(@RequestAttribute AdminSession adminSession, @RequestParam Long appId,
+            @RequestParam Long envId, @RequestParam(required = false) Long moduleId) {
+        adminSession.validateApp(appService.getById(appId));
+        return nginxConfigService.find(appService.getById(appId), envService.getById(envId),
+                moduleId != null ? moduleService.getById(moduleId) : null);
+    }
+
+    @PostMapping("/update")
+    public Object update(@RequestAttribute AdminSession adminSession, @RequestParam Long appId,
+            @RequestParam Long envId, @RequestParam Long moduleId, @RequestParam String content) {
+        adminSession.validateRole(Role.OPS, Role.ADMIN);
+        return nginxConfigService.update(appService.getById(appId), envService.getById(envId),
+                moduleId != null ? moduleService.getById(moduleId) : null, content);
+    }
+
+}
+

+ 90 - 0
src/main/java/com/qmth/ops/api/controller/export/NginxConfigExportController.java

@@ -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());
+    }
+
+}

+ 8 - 0
src/main/java/com/qmth/ops/biz/dao/NginxConfigDao.java

@@ -0,0 +1,8 @@
+package com.qmth.ops.biz.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.qmth.ops.biz.domain.NginxConfig;
+
+public interface NginxConfigDao extends BaseMapper<NginxConfig> {
+
+}

+ 71 - 0
src/main/java/com/qmth/ops/biz/domain/NginxConfig.java

@@ -0,0 +1,71 @@
+package com.qmth.ops.biz.domain;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+
+import java.io.Serializable;
+
+@TableName("nginx_config")
+public class NginxConfig implements Serializable {
+
+    private static final long serialVersionUID = 1404165583641542760L;
+
+    private Long appId;
+
+    private Long envId;
+
+    private Long moduleId;
+
+    private String content;
+
+    private Long createTime;
+
+    private Long updateTime;
+
+    public Long getAppId() {
+        return appId;
+    }
+
+    public void setAppId(Long appId) {
+        this.appId = appId;
+    }
+
+    public Long getEnvId() {
+        return envId;
+    }
+
+    public void setEnvId(Long envId) {
+        this.envId = envId;
+    }
+
+    public Long getModuleId() {
+        return moduleId;
+    }
+
+    public void setModuleId(Long moduleId) {
+        this.moduleId = moduleId;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public Long getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Long createTime) {
+        this.createTime = createTime;
+    }
+
+    public Long getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Long updateTime) {
+        this.updateTime = updateTime;
+    }
+}

+ 46 - 0
src/main/java/com/qmth/ops/biz/service/NginxConfigService.java

@@ -0,0 +1,46 @@
+package com.qmth.ops.biz.service;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.qmth.ops.biz.dao.NginxConfigDao;
+import com.qmth.ops.biz.domain.App;
+import com.qmth.ops.biz.domain.Env;
+import com.qmth.ops.biz.domain.Module;
+import com.qmth.ops.biz.domain.NginxConfig;
+import org.springframework.lang.Nullable;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+import javax.validation.constraints.NotNull;
+
+public class NginxConfigService {
+
+    private static final long GLOBAL_MODULE_ID = 0L;
+
+    @Resource
+    private NginxConfigDao nginxConfigDao;
+
+    public NginxConfig find(@NotNull App app, @NotNull Env env, @Nullable Module module) {
+        return findOne(app.getId(), env.getId(), module != null ? module.getId() : GLOBAL_MODULE_ID);
+    }
+
+    @Transactional
+    public NginxConfig update(@NotNull App app, @NotNull Env env, @Nullable Module module, @NotNull String content) {
+        NginxConfig config = new NginxConfig();
+        config.setAppId(app.getId());
+        config.setEnvId(env.getId());
+        config.setModuleId(module != null ? module.getId() : GLOBAL_MODULE_ID);
+        config.setContent(content);
+        config.setCreateTime(System.currentTimeMillis());
+        config.setUpdateTime(config.getCreateTime());
+        nginxConfigDao.delete(new LambdaQueryWrapper<NginxConfig>().eq(NginxConfig::getAppId, config.getAppId())
+                .eq(NginxConfig::getEnvId, config.getEnvId()).eq(NginxConfig::getModuleId, config.getModuleId()));
+        nginxConfigDao.insert(config);
+        return findOne(config.getAppId(), config.getEnvId(), config.getModuleId());
+    }
+
+    private NginxConfig findOne(Long appId, Long envId, Long moduleId) {
+        return nginxConfigDao.selectOne(
+                new LambdaQueryWrapper<NginxConfig>().eq(NginxConfig::getAppId, appId).eq(NginxConfig::getEnvId, envId)
+                        .eq(NginxConfig::getModuleId, moduleId));
+    }
+}

+ 12 - 0
src/main/resources/script/init.sql

@@ -102,6 +102,18 @@ CREATE TABLE `version`
 ) ENGINE = InnoDB
   DEFAULT CHARSET = utf8mb4;
 
+CREATE TABLE `nginx_config`
+(
+    `app_id`      bigint(20) unsigned NOT NULL,
+    `module_id`   bigint(20) unsigned NOT NULL,
+    `env_id`      bigint(20)          NOT NULL,
+    `content`     text                NOT NULL,
+    `create_time` bigint(20)          NOT NULL,
+    `update_time` bigint(20)          NOT NULL,
+    PRIMARY KEY (`app_id`, `module_id`, `env_id`)
+) ENGINE = InnoDB
+  DEFAULT CHARSET = utf8mb4;
+
 INSERT INTO `user` (`login_name`, `name`, `password`, `role`, `access_token`, `export_secret`, `create_time`,
                     `update_time`)
 VALUES ('admin', '系统管理员', '51f7be45b644056aa7de7340a56c0409', '[\"ADMIN\"]', '', '42cc190287d3e92c',