package com.bcrjl.miwifi.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author yanqs
 * @since 2024-03-23
 */
@Controller
public class FrontIndexController {
    /**
     * 所有未找到的页面都跳转到首页, 用户解决 vue history 直接访问 404 的问题
     * 同时, 读取 index.html 文件, 修改 title 和 favicon 后返回.
     *
     * @return 转发到 /index.html
     */
    @RequestMapping(value = {"/**/{[path:[^\\.]*}", "/"}, produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
    @ResponseBody
    public String redirect() throws IOException {
        // 读取 resources/static/index.html 文件修改 title 和 favicon 后返回
        ClassPathResource resource = new ClassPathResource("static/index.html");
        InputStream inputStream = resource.getInputStream();
        String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        return content;
    }
}
