【8.1.7】更新一个devops部署的接口

dev-8.1.9
fengshuonan 2024-06-04 18:33:15 +08:00
parent 2c6c74aa35
commit 012c3ec9bb
4 changed files with 180 additions and 2 deletions

View File

@ -12,7 +12,7 @@
<groupId>cn.stylefeng</groupId>
<artifactId>guns</artifactId>
<version>8.1.2</version>
<version>8.1.7</version>
<name>guns</name>
<description>Guns主项目</description>
@ -24,7 +24,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mysql-connector-java.version>8.0.21</mysql-connector-java.version>
<roses.kernel.version>8.1.2</roses.kernel.version>
<roses.kernel.version>8.1.7</roses.kernel.version>
<flyway.version>7.1.1</flyway.version>
</properties>

View File

@ -0,0 +1,114 @@
package cn.stylefeng.guns.config.web;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* css
*
* @author fengshuonan
* @since 2024-04-23 16:12
*/
public class EmptyResourceFilter implements Filter {
/**
* keyvalue
*/
private static final Map<String, Boolean> NEED_TO_CACHE_FLAG = new HashMap<>();
/**
*
*/
public static final String GUNS_DEVOPS_ASSETS_PREFIX = "/guns-devops/assets";
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 1. 获取请求类
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// 2. 获取请求的路径,例如:/guns-devops/assets/css/style.css
String requestURI = httpServletRequest.getRequestURI();
// 3. 获取资源文件前缀,如果是资源文件前缀,则直接返回空
if (requestURI.startsWith(GUNS_DEVOPS_ASSETS_PREFIX)) {
// 先判断是否需要缓存
Boolean urlHasEmpty = NEED_TO_CACHE_FLAG.get(requestURI);
if (urlHasEmpty != null) {
if (urlHasEmpty) {
this.doEmptyResourceReturn(response);
} else {
chain.doFilter(request, response);
}
return;
}
// 获取资源文件路径
String resourcePath = StrUtil.removePrefix(requestURI, "/");
URL resource = ResourceUtil.getResource(resourcePath);
// 1. 资源文件不存在直接返回直接走404的逻辑
if (resource == null) {
chain.doFilter(request, response);
// 缓存一下结果
NEED_TO_CACHE_FLAG.put(requestURI, false);
return;
}
// 2. 资源文件存在,但是内容为空,直接返回空字符串
String content = ResourceUtil.readUtf8Str(resourcePath);
if (ObjectUtil.isEmpty(content)) {
doEmptyResourceReturn(response);
// 缓存一下结果
NEED_TO_CACHE_FLAG.put(requestURI, true);
}
// 3. 资源文件存在,内容不为空,直接返回内容
else {
chain.doFilter(request, response);
// 缓存一下结果
NEED_TO_CACHE_FLAG.put(requestURI, false);
}
return;
}
chain.doFilter(request, response);
}
/**
* http
*
* @author fengshuonan
* @since 2024-04-23 18:23
*/
private void doEmptyResourceReturn(ServletResponse response) throws IOException {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getWriter().write("");
}
@Override
public void destroy() {
// Cleanup code
}
}

View File

@ -5,6 +5,7 @@ import cn.stylefeng.guns.core.security.TokenAndPermissionInterceptor;
import cn.stylefeng.roses.kernel.wrapper.field.jackson.CustomJacksonIntrospector;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@ -76,4 +77,18 @@ public class SpringMvcConfiguration implements WebMvcConfigurer {
registry.addResourceHandler("/guns-devops/**").addResourceLocations("classpath:/guns-devops/");
}
/**
*
*
* @author fengshuonan
* @since 2024-04-23 16:12
*/
@Bean
public FilterRegistrationBean<EmptyResourceFilter> filterRegistrationBean() {
FilterRegistrationBean<EmptyResourceFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new EmptyResourceFilter());
registrationBean.addUrlPatterns(EmptyResourceFilter.GUNS_DEVOPS_ASSETS_PREFIX + "/*");
return registrationBean;
}
}

View File

@ -0,0 +1,49 @@
package cn.stylefeng.guns.modular.deploy;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* jar
*
* @author fengshuonan
* @since 2024-04-22 18:07
*/
@Controller
public class IndexController {
/**
* devops访
*
* @author fengshuonan
* @since 2024/6/4 18:31
*/
@GetMapping("/guns-devops")
public void index(HttpServletResponse response) throws IOException {
renderIndex(response);
}
/**
*
*
* @author fengshuonan
* @date 2022/10/23 15:02
*/
private void renderIndex(HttpServletResponse response) throws IOException {
ServletOutputStream outputStream = response.getOutputStream();
response.addHeader("Content-Type", "text/html");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-cache, no-store, private, must-revalidate, proxy-revalidate");
String indexContent = ResourceUtil.readStr("classpath:/guns-devops/index.html", StandardCharsets.UTF_8);
IoUtil.write(outputStream, StandardCharsets.UTF_8, true, indexContent);
}
}