package com.elab.spring.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
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;

/**
 * Swagger配置
 *
 * @author Liukx
 * @create 2018-04-09 11:07
 * @email liukx@elab-plus.com
 **/
@Order(Integer.MAX_VALUE)
@EnableSwagger2
@Configuration
public class SwaggerConfigBean {

    @Autowired
    private Environment environment;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 具体需要扫描的包
                .apis(RequestHandlerSelectors.basePackage(environment.getProperty("swagger2.basePackage")))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(environment.getProperty("swagger2.title"))
                .description(environment.getProperty("swagger2.description"))
                .termsOfServiceUrl(environment.getProperty("swagger2.termsOfServiceUrl"))
                .version(environment.getProperty("swagger2.licenseUrl"))
                .licenseUrl(environment.getProperty("swagger2.version"))
                .build();
    }
}
