mirror of https://gitee.com/stylefeng/guns
【8.1.7】更新一个devops部署的接口
parent
2c6c74aa35
commit
012c3ec9bb
4
pom.xml
4
pom.xml
|
@ -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>
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
/**
|
||||
* 需要做缓存的资源,key是请求资源路径,value是否有空的资源文件,空的资源文件会直接返回空
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue