001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.support.swagger;
017
018import io.jboot.Jboot;
019import io.jboot.utils.ClassScanner;
020import io.jboot.web.controller.annotation.RequestMapping;
021import io.swagger.models.Contact;
022import io.swagger.models.Info;
023import io.swagger.models.License;
024import io.swagger.models.Swagger;
025
026import java.util.List;
027
028import static io.swagger.models.Scheme.HTTP;
029import static io.swagger.models.Scheme.HTTPS;
030
031/**
032 * @author Michael Yang 杨福海 (fuhai999@gmail.com)
033 * @version V1.0
034 * <p>
035 * 相关文档: https://www.gitbook.com/book/huangwenchao/swagger/details
036 */
037public class JbootSwaggerManager {
038
039
040    private JbootSwaggerConfig config = Jboot.config(JbootSwaggerConfig.class);
041    private Swagger swagger;
042    private static JbootSwaggerManager instance;
043
044    public static JbootSwaggerManager me() {
045        if (instance == null) {
046            instance = new JbootSwaggerManager();
047        }
048
049        return instance;
050    }
051
052
053    public void init() {
054        if (!config.isConfigOk()) {
055            return;
056        }
057
058        swagger = new Swagger();
059        swagger.setHost(config.getHost());
060        swagger.setBasePath("/");
061        swagger.addScheme(HTTP);
062        swagger.addScheme(HTTPS);
063
064
065        Info swaggerInfo = new Info();
066        swaggerInfo.setDescription(config.getDescription());
067        swaggerInfo.setVersion(config.getVersion());
068        swaggerInfo.setTitle(config.getTitle());
069        swaggerInfo.setTermsOfService(config.getTermsOfService());
070
071        Contact contact = new Contact();
072        contact.setName(config.getContactName());
073        contact.setEmail(config.getContactEmail());
074        contact.setUrl(config.getContactUrl());
075        swaggerInfo.setContact(contact);
076
077        License license = new License();
078        license.setName(config.getLicenseName());
079        license.setUrl(config.getLicenseUrl());
080        swaggerInfo.setLicense(license);
081
082
083        swagger.setInfo(swaggerInfo);
084
085        List<Class> classes = ClassScanner.scanClassByAnnotation(RequestMapping.class, false);
086
087        Reader.read(swagger, classes);
088
089    }
090
091
092    public Swagger getSwagger() {
093        return swagger;
094    }
095}