package ink.wgink.common.config;

import ink.wgink.properties.FileProperties;
import ink.wgink.properties.web.WebProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: WebConfig
 * @Description: Web配置
 * @Author: wanggeng
 * @Date: 2021/2/5 7:44 下午
 * @Version: 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private FileProperties fileProperties;
    @Autowired
    private WebProperties webProperties;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 上传文件的直接访问地址
        if (!StringUtils.isBlank(fileProperties.getUploadPath())) {
            // 静态文件
            registry.addResourceHandler("/files/**").addResourceLocations("file:" + fileProperties.getUploadPath());
        }
        if (!StringUtils.isBlank(webProperties.getPath())) {
            // 网站
            registry.addResourceHandler("/web/**").addResourceLocations("file:" + webProperties.getPath());
        }
        // 静态资源
        registry.addResourceHandler("/assets/**", "/pages/**").addResourceLocations("classpath:/static/assets/", "classpath:/static/pages/").setCachePeriod(7 * 24 * 3600);
    }

}
