extract start module (#59)

This commit is contained in:
TheoneFx
2023-03-20 13:38:34 +08:00
committed by GitHub
parent 11ad90c33c
commit 033ab83006
12 changed files with 78 additions and 86 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.alibaba.initializer.start;
import com.alibaba.initializer.configure.BootstrapProjectGenerationConfigure;
import com.alibaba.initializer.configure.InitializerMetadataConfigure;
import com.alibaba.initializer.start.web.WebConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@SpringBootConfiguration
@EnableAutoConfiguration
@EnableCaching
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
@ComponentScan("com.alibaba.initializer.protocol")
@Import({
InitializerMetadataConfigure.class,
BootstrapProjectGenerationConfigure.class,
WebConfig.class,
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.alibaba.initializer.start.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CommonPages {
@Autowired
private ResourceLoader resourceLoader;
@GetMapping({"/checkpreload.htm", "nginx_status"})
@ResponseBody
public String checkPreload() {
return "success";
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.alibaba.initializer.start.web;
import java.util.HashSet;
import java.util.Set;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@ComponentScan
@Configuration
public class WebConfig implements WebMvcConfigurer {
static private final Set<String> ALLOWED_ORIGIN_HTTP = new HashSet<>();
static {
ALLOWED_ORIGIN_HTTP.add("https://start.aliyun.com");
ALLOWED_ORIGIN_HTTP.add("https://pre-start.aliyun.com");
}
@Bean
public ErrorPageRegistrar errorPageRegistrar() {
return (registry) -> {
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
registry.addErrorPages(new ErrorPage("/error/index.html"));
};
}
@Override
public void addCorsMappings(CorsRegistry registry) {
//添加映射路径
registry.addMapping("/**")
//放行哪些原始域
.allowedOrigins(ALLOWED_ORIGIN_HTTP.toArray(new String[0]))
//是否发送Cookie信息
.allowCredentials(true)
//放行哪些原始域(请求方式)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//放行哪些原始域(头部信息)
.allowedHeaders("*");
}
}