package com.ho.flow.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @描述 swagger2配置
 * @创建人 fancl
 * @创建时间 2021/2/12
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Value("${swagger2.projectName}")
    private String projectName;

    @Value("${swagger2.controllerPath}")
    private String controllerPath;

    //根据配置启用或关闭swagger接口
    @Value("${swagger2.enable}")
    private Boolean enable;

    //Swagger2接口文档版本
    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(controllerPath))
                .paths(PathSelectors.any()) //可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .build()
                .enable(enable);
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(projectName) //设置文档的标题
                .description("REST API")    //设置文档的描述
                .version(VERSION)
                .build();
    }
}
