0

Первые наброски WebApp

This commit is contained in:
2017-04-09 02:50:12 +03:00
parent f502665234
commit f206f03133
7 changed files with 152 additions and 1 deletions

20
pom.xml
View File

@@ -23,6 +23,7 @@
<log4j.version>2.5</log4j.version>
<spring.version>4.3.7.RELEASE</spring.version>
<spring.mongodb.version>1.10.1.RELEASE</spring.mongodb.version>
<jetty.version>9.4.0.v20161208</jetty.version>
</properties>
<dependencies>
@@ -83,6 +84,25 @@
</exclusions>
</dependency>
<!-- JETTY -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- SPRING MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- COMPONENTS -->
<dependency>
<groupId>commons-io</groupId>

View File

@@ -0,0 +1,16 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-09
*/
package kinosearch.kinosearch3.webpp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
WebApp webApp = ctx.getBean("webapp", WebApp.class);
webApp.start();
}
}

View File

@@ -0,0 +1,19 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-09
*/
package kinosearch.kinosearch3.webpp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:/application.properties")
public class SpringConfig {
@Bean
public WebApp webapp(@Value("${webapp.host}") String host, @Value("${webapp.port}") int port) {
return new WebApp(host, port);
}
}

View File

@@ -0,0 +1,14 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-09
*/
package kinosearch.kinosearch3.webpp;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
}

View File

@@ -0,0 +1,60 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-09
*/
package kinosearch.kinosearch3.webpp;
import lombok.Getter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Slf4jLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import java.net.InetSocketAddress;
public class WebApp {
private final Logger logger = LoggerFactory.getLogger(WebApp.class);
@Getter
private final String host;
@Getter
private final int port;
public WebApp(String host, int port) {
this.host = host;
this.port = port;
}
void start() {
Log.setLog(new Slf4jLog("Jetty.Logger"));
Server server = new Server(new InetSocketAddress(host, port));
server.setHandler(getServletContextHandler(getContext()));
try {
server.start();
server.join();
} catch (Exception e) {
logger.error("Start server", e);
}
}
private ServletContextHandler getServletContextHandler(WebApplicationContext context) {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath("/");
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
contextHandler.addEventListener(new ContextLoaderListener(context));
return contextHandler;
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("kinosearch.kinosearch3.webpp");
return context;
}
}

View File

@@ -0,0 +1,19 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-04-09
*/
package kinosearch.kinosearch3.webpp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class WebAppController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public String hello() {
return "hello...?";
}
}

View File

@@ -1,4 +1,7 @@
mongo.host=127.0.0.1
mongo.port=27017
mongo.db=kinosearch
saveRootDir=R:\\
saveRootDir=R:\\
webapp.host=127.0.0.1
webapp.port=8080