diff --git a/continew-starter-api-doc/pom.xml b/continew-starter-api-doc/pom.xml
new file mode 100644
index 00000000..1467ee65
--- /dev/null
+++ b/continew-starter-api-doc/pom.xml
@@ -0,0 +1,31 @@
+
+
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.apidoc.autoconfigure; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import jakarta.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import top.charles7c.continew.starter.core.autoconfigure.ProjectProperties; + +/** + * API 文档自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@Lazy +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true") +public class SpringDocAutoConfiguration { + + /** + * Open API 配置 + */ + @Bean + @ConditionalOnMissingBean + public OpenAPI openApi(ProjectProperties properties) { + Info info = new Info() + .title(String.format("%s %s", properties.getName(), "API 文档")) + .version(properties.getVersion()) + .description(properties.getDescription()); + ProjectProperties.Contact contact = properties.getContact(); + if (null != contact) { + info.contact(new Contact().name(contact.getName()) + .email(contact.getEmail()) + .url(contact.getUrl())); + } + ProjectProperties.License license = properties.getLicense(); + if (null != license) { + info.license(new License().name(license.getName()) + .url(license.getUrl())); + } + return new OpenAPI().info(info); + } + + @PostConstruct + public void postConstruct() { + log.info("[ContiNew Starter] - Auto Configuration 'ApiDoc' completed initialization."); + } +} diff --git a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java new file mode 100644 index 00000000..7b1d020c --- /dev/null +++ b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.apidoc.autoconfigure; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.CacheControl; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.concurrent.TimeUnit; + +/** + * 静态资源处理器自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@EnableWebMvc +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true") +public class StaticResourceAutoConfiguration implements WebMvcConfigurer { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/"); + registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/") + .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic()); + } +} diff --git a/continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..e48225e8 --- /dev/null +++ b/continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,2 @@ +top.charles7c.continew.starter.apidoc.autoconfigure.SpringDocAutoConfiguration +top.charles7c.continew.starter.apidoc.autoconfigure.StaticResourceAutoConfiguration \ No newline at end of file diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java new file mode 100644 index 00000000..c2b52390 --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.core.autoconfigure;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.context.properties.NestedConfigurationProperty;
+import org.springframework.stereotype.Component;
+
+/**
+ * 项目配置属性
+ *
+ * @author Charles7c
+ * @since 1.0.0
+ */
+@Data
+@Component
+@ConfigurationProperties(prefix = "project")
+public class ProjectProperties {
+
+ /**
+ * 名称
+ */
+ private String name;
+
+ /**
+ * 应用名称
+ */
+ private String appName;
+
+ /**
+ * 版本
+ */
+ private String version;
+
+ /**
+ * 描述
+ */
+ private String description;
+
+ /**
+ * URL
+ */
+ private String url;
+
+ /**
+ * 基本包
+ */
+ private String basePackage;
+
+ /**
+ * 联系人
+ */
+ @NestedConfigurationProperty
+ private Contact contact;
+
+ /**
+ * 许可协议
+ */
+ @NestedConfigurationProperty
+ private License license;
+
+ /**
+ * 是否为生产环境
+ */
+ private boolean production = false;
+
+ /**
+ * 联系人配置属性
+ */
+ @Data
+ public static class Contact {
+ /**
+ * 名称
+ */
+ private String name;
+
+ /**
+ * 邮箱
+ */
+ private String email;
+
+ /**
+ * URL
+ */
+ private String url;
+ }
+
+ /**
+ * 许可协议配置属性
+ */
+ @Data
+ public static class License {
+ /**
+ * 名称
+ */
+ private String name;
+
+ /**
+ * URL
+ */
+ private String url;
+ }
+}
diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml
index f5f5b0c1..0953eb3b 100644
--- a/continew-starter-dependencies/pom.xml
+++ b/continew-starter-dependencies/pom.xml
@@ -55,11 +55,21 @@