web服务启动逻辑更新.

This commit is contained in:
aoshiguchen
2022-07-15 21:33:57 +08:00
parent bd5c8bccb6
commit e2b53c7936
14 changed files with 429 additions and 21 deletions
@@ -74,9 +74,9 @@ public class ApplicationContext implements LifeCycle {
GlobalConfig.setIsContainerStartup(true);
this.rootBeanFactory = new SimpleBeanFactory("rootBeanFactory");
this.applicationBeanFactory = new SimpleBeanFactory(rootBeanFactory, "applicationBeanFactory");
this.rootBeanFactory.registerBean(environment, "rootEnvironment");
this.rootBeanFactory.registerBean(this, "rootApplicationContext");
this.rootBeanFactory.registerBean(environment.getConfig(), "rootApplicationConfig");
this.rootBeanFactory.registerBean(environment);
this.rootBeanFactory.registerBean(this);
this.rootBeanFactory.registerBean(environment.getConfig());
register();
List<BeanFactoryAware> beanFactoryAwareList = this.applicationBeanFactory.getBeanList(BeanFactoryAware.class);
if (CollectionUtil.notEmpty(beanFactoryAwareList)) {
@@ -25,6 +25,8 @@ import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
import fun.asgc.neutrino.core.web.HttpRequestHandler;
import fun.asgc.neutrino.core.web.WebApplicationContext;
import fun.asgc.neutrino.core.web.WebApplicationServer;
/**
@@ -36,7 +38,7 @@ import fun.asgc.neutrino.core.web.WebApplicationServer;
@Component
public class ExtensionServiceLoader implements ApplicationRunner {
@Autowired
private ApplicationConfig rootApplicationConfig;
private ApplicationConfig applicationConfig;
@Autowired
private SimpleBeanFactory applicationBeanFactory;
@@ -46,13 +48,13 @@ public class ExtensionServiceLoader implements ApplicationRunner {
}
private void startHttpServer() {
if (null == rootApplicationConfig) {
if (null == applicationConfig) {
return;
}
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
ApplicationConfig.Http http = applicationConfig.getHttp();
if (null == http || null == http.getEnable() || !http.getEnable()) {
return;
}
applicationBeanFactory.registerBean(WebApplicationServer.class);
applicationBeanFactory.registerBean(WebApplicationContext.class);
}
}
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web;
import fun.asgc.neutrino.core.util.StringUtil;
/**
*
* @author: aoshiguchen
* @date: 2022/7/15
*/
public enum HttpMethod {
GET,POST;
public static HttpMethod of(String method) {
if (StringUtil.isEmpty(method)) {
return null;
}
return HttpMethod.valueOf(method.toUpperCase());
}
}
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.context.ApplicationConfig;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
/**
*
* @author: aoshiguchen
* @date: 2022/7/15
*/
@NonIntercept
@Component
public class HttpRequestHandler {
@Autowired
private ApplicationConfig applicationConfig;
public void handle(ChannelHandlerContext context, FullHttpRequest request) {
// TODO
System.out.println("hello");
}
}
@@ -0,0 +1,70 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web;
import fun.asgc.neutrino.core.annotation.*;
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
import fun.asgc.neutrino.core.context.ApplicationContext;
import fun.asgc.neutrino.core.context.ApplicationRunner;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
/**
*
* @author: aoshiguchen
* @date: 2022/7/15
*/
@Accessors(chain = true)
@Data
@Slf4j
@NonIntercept
@Component
public class WebApplicationContext implements ApplicationRunner {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private SimpleBeanFactory applicationBeanFactory;
/**
* bean工厂
*/
private SimpleBeanFactory webApplicationBeanFactory;
@Init
public void init() {
this.webApplicationBeanFactory = new SimpleBeanFactory(applicationBeanFactory, "webApplicationBeanFactory");
this.webApplicationBeanFactory.init();
// TODO 初始化路由信息
}
@Destroy
public void destroy() {
this.webApplicationBeanFactory.destroy();
}
@Override
public void run(String[] args) throws Exception {
this.webApplicationBeanFactory.registerBean(HttpRequestHandler.class);
this.webApplicationBeanFactory.registerBean(WebApplicationServer.class);
}
}
@@ -27,9 +27,7 @@ import fun.asgc.neutrino.core.annotation.Destroy;
import fun.asgc.neutrino.core.annotation.Init;
import fun.asgc.neutrino.core.context.ApplicationConfig;
import fun.asgc.neutrino.core.context.ApplicationRunner;
import fun.asgc.neutrino.core.util.HttpServerUtil;
import fun.asgc.neutrino.core.util.NumberUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import fun.asgc.neutrino.core.util.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
@@ -49,7 +47,9 @@ import lombok.extern.slf4j.Slf4j;
@Component
public class WebApplicationServer implements ApplicationRunner {
@Autowired
private ApplicationConfig rootApplicationConfig;
private ApplicationConfig applicationConfig;
@Autowired
private HttpRequestHandler httpRequestHandler;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private ServerBootstrap serverBootstrap;
@@ -58,7 +58,7 @@ public class WebApplicationServer implements ApplicationRunner {
@Init
public void init() {
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
ApplicationConfig.Http http = applicationConfig.getHttp();
if (StringUtil.notEmpty(http.getMaxContentLengthDesc()) && null == http.getMaxContentLength()) {
http.setMaxContentLength(NumberUtil.descriptionToSize(http.getMaxContentLengthDesc(), 64 * 1024));
}
@@ -66,14 +66,12 @@ public class WebApplicationServer implements ApplicationRunner {
this.bossGroup = new NioEventLoopGroup();
this.workerGroup = new NioEventLoopGroup();
this.serverBootstrap = new ServerBootstrap();
this.initFavicon();
}
@Override
public void run(String[] args) throws Exception {
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
if (!http.getContextPath().endsWith("/")) {
http.setContextPath(http.getContextPath() + "/");
}
ApplicationConfig.Http http = applicationConfig.getHttp();
this.serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
@@ -93,8 +91,8 @@ public class WebApplicationServer implements ApplicationRunner {
protected void channelRead0(ChannelHandlerContext context, FullHttpRequest request) throws Exception {
String uri = request.uri();
log.debug("http request: {}", uri);
if (uri.startsWith(http.getContextPath())) {
// TODO
if (uri.startsWith(http.getContextPath() + "/") || uri.equals(http.getContextPath())) {
httpRequestHandler.handle(context, request);
} else if (uri.equals("/favicon.ico") && null != faviconBytes) {
FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(faviconBytes));
fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, "image/x-icon");
@@ -110,6 +108,18 @@ public class WebApplicationServer implements ApplicationRunner {
log.info("HTTP服务启动,端口:{} context-path{}", http.getPort(), http.getContextPath());
}
private void initFavicon() {
if (null == applicationConfig.getHttp().getStaticResource() || CollectionUtil.isEmpty(applicationConfig.getHttp().getStaticResource().getLocations())) {
return;
}
for (String location : applicationConfig.getHttp().getStaticResource().getLocations()) {
this.faviconBytes = FileUtil.readBytes(location.concat("favicon.ico"));
if (null != faviconBytes) {
break;
}
}
}
@Destroy
public void destroy() {
this.channelFuture.channel().close();
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author: aoshiguchen
* @date: 2022/7/15
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface GetMapping {
String[] value() default {};
}
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author: aoshiguchen
* @date: 2022/7/15
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface PostMapping {
String[] value() default {};
}
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.annotation;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author: aoshiguchen
* @date: 2022/7/15
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
@NonIntercept
public @interface RestController {
}
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.exception;
import fun.asgc.neutrino.core.exception.InternalException;
/**
* web服务异常
* @author: aoshiguchen
* @date: 2022/7/15
*/
public class WebException extends InternalException {
protected String method;
protected Integer code;
private String path;
protected String message;
public WebException(String method, Integer code, String path, String message) {
super(message);
}
public WebException(String method, Integer code, String path, String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web.exception;
/**
* web资源找不到异常
* @author: aoshiguchen
* @date: 2022/7/15
*/
public class WebResourceNotFoundException extends WebException {
public WebResourceNotFoundException(String method, String path, String message) {
super(method, 404, path, message);
}
public WebResourceNotFoundException(String method, String path, String message, Throwable cause) {
super(method, 404, path, message, cause);
}
}
@@ -70,8 +70,8 @@ public class LockUtilTest {
@Data
public static class A {
public A() {
System.out.println("new instance.");
}
// public A() {
// System.out.println("new instance.");
// }
}
}
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.web;
import org.junit.Test;
/**
*
* @author: aoshiguchen
* @date: 2022/7/15
*/
public class HttpMethodTest {
@Test
public void test1() {
HttpMethod method = HttpMethod.of("Get");
System.out.println(method);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB