View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.anyflow.lannister.http;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import io.netty.bootstrap.ServerBootstrap;
23  import io.netty.channel.EventLoopGroup;
24  import io.netty.channel.ServerChannel;
25  import io.netty.channel.epoll.EpollServerSocketChannel;
26  import io.netty.channel.socket.nio.NioServerSocketChannel;
27  import net.anyflow.lannister.Literals;
28  import net.anyflow.lannister.Settings;
29  
30  public class WebServer {
31  	private static final Logger logger = LoggerFactory.getLogger(WebServer.class);
32  
33  	private final EventLoopGroup bossGroup;
34  	private final EventLoopGroup workerGroup;
35  
36  	public WebServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
37  		this.bossGroup = bossGroup;
38  		this.workerGroup = workerGroup;
39  	}
40  
41  	public EventLoopGroup bossGroup() {
42  		return bossGroup;
43  	}
44  
45  	public EventLoopGroup workerGroup() {
46  		return workerGroup;
47  	}
48  
49  	public void start(String requestHandlerPakcageRoot) throws Exception {
50  		start(requestHandlerPakcageRoot, null);
51  	}
52  
53  	public void start(String requestHandlerPakcageRoot,
54  			final Class<? extends WebsocketFrameHandler> websocketFrameHandlerClass) throws Exception {
55  		HttpRequestHandler.setRequestHandlerPakcageRoot(requestHandlerPakcageRoot);
56  
57  		Class<? extends ServerChannel> serverChannelClass;
58  
59  		if (Literals.NETTY_EPOLL.equals(Settings.INSTANCE.nettyTransportMode())) {
60  			serverChannelClass = EpollServerSocketChannel.class;
61  		}
62  		else {
63  			serverChannelClass = NioServerSocketChannel.class;
64  		}
65  
66  		if (Settings.INSTANCE.httpPort() != null) {
67  			ServerBootstrap bootstrap = new ServerBootstrap();
68  
69  			bootstrap.group(bossGroup, workerGroup).channel(serverChannelClass)
70  					.childHandler(new WebServerChannelInitializer(false, websocketFrameHandlerClass));
71  
72  			bootstrap.bind(Settings.INSTANCE.httpPort()).sync();
73  		}
74  
75  		if (Settings.INSTANCE.httpsPort() != null) {
76  			ServerBootstrap bootstrap = new ServerBootstrap();
77  
78  			bootstrap.group(bossGroup, workerGroup).channel(serverChannelClass)
79  					.childHandler(new WebServerChannelInitializer(true, websocketFrameHandlerClass));
80  
81  			bootstrap.bind(Settings.INSTANCE.httpsPort()).sync();
82  		}
83  
84  		logger.info("Lannister HTTP server started [http.port={}, https.port={}]", Settings.INSTANCE.httpPort(),
85  				Settings.INSTANCE.httpsPort());
86  	}
87  }