Skip to content

Commit

Permalink
Should be working state
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-calleja committed Apr 16, 2016
1 parent 329d13e commit a8b3a58
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.classpath
4 changes: 4 additions & 0 deletions be/.gitignore
@@ -0,0 +1,4 @@
target
.settings
.project
.classpath
24 changes: 24 additions & 0 deletions be/pom.xml
Expand Up @@ -7,15 +7,39 @@
<artifactId>be</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.tmp</groupId>
<artifactId>fe</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
32 changes: 32 additions & 0 deletions be/src/main/java/com/tmp/App.java
@@ -0,0 +1,32 @@
package com.tmp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@SpringBootApplication
public class App implements ApplicationListener<ContextRefreshedEvent> {

private static final Logger LOG = LoggerFactory.getLogger(App.class);

@Value("${spring.profiles.active}")
protected String springProfilesActive;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
LOG.info("=======================================");
LOG.info("App running with active profiles: {}", springProfilesActive);
LOG.info("=======================================");
}

public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}

}
45 changes: 45 additions & 0 deletions be/src/main/java/com/tmp/GreeterController.java
@@ -0,0 +1,45 @@
package com.tmp;

import java.time.LocalDateTime;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/api/greetings")
public class GreeterController {

private static final String template = "Hello, %s!";

@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public @ResponseBody Greeting greetNoName() {
return new Greeting(String.format(template, "World"));
}

@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = { "application/json" })
public @ResponseBody Greeting greetName(final @PathVariable String name) {
return new Greeting(String.format(template, name));
}

public class Greeting {
private final String content;
private final LocalDateTime time;

public Greeting(String content) {
this.content = content;
this.time = LocalDateTime.now();
}

public String getContent() {
return this.content;
}

public String getTime() {
return this.time.toString();
}
}

}
46 changes: 46 additions & 0 deletions be/src/main/java/com/tmp/WebMvcConfig.java
@@ -0,0 +1,46 @@
package com.tmp;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/public/" };

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}

// During development, webpack server runs on localhost:8080
// Make the browser happy by returning CORS headers in this case
@Bean
@Profile("dev")
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**").allowedOrigins("http://localhost:8080");
}
};
}

}
2 changes: 2 additions & 0 deletions be/src/main/resources/application.properties
@@ -0,0 +1,2 @@
spring.profiles.active=dev
server.port=8090
11 changes: 11 additions & 0 deletions be/src/main/resources/public/index.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>App</title>
</head>
<body>

<script src="/webjars/fe/0.0.1-SNAPSHOT/app-bundle.js"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions fe/.gitignore
@@ -0,0 +1,4 @@
node
node_modules
target
etc
15 changes: 12 additions & 3 deletions fe/app/index.js
Expand Up @@ -2,10 +2,19 @@ var greeter = require('./greeter');

var greeting = greeter.greet();

if (document) {
if (typeof document !== 'undefined') {
var apiEndpoint = 'http://localhost:8090/api/greetings';
var el = document.createElement('h1');
el.innerHTML = greeting;
document.body.appendChild(el);

fetch(apiEndpoint + '/webpack').then(function(response) {
return response.json();
}).then(function(obj) {
el.innerHTML = greeting + '<br>' + obj.content + '<br>At ' + obj.time;
document.body.appendChild(el);
}).catch(function(err) {
el.innerHTML = 'oh no…';
document.body.appendChild(el);
});
} else {
console.log(greeting);
}
Expand Down
8 changes: 6 additions & 2 deletions fe/package.json
@@ -1,11 +1,15 @@
{
"name": "fe",
"version": "1.0.0",
"version": "0.0.1-SNAPSHOT",
"description": "",
"main": "./app",
"scripts": {
"build": "webpack -p"
},
"author": "",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"webpack": "1.12.9",
"webpack-dev-server": "1.14.0"
}
}
11 changes: 11 additions & 0 deletions fe/tmp/index.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>App</title>
</head>
<body>

<script src="../assets/app-bundle.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions fe/webpack.config.js
Expand Up @@ -8,8 +8,10 @@ const PATHS = {

module.exports = {
entry: './app/index.js',

output: {
path: PATHS.build,
publicPath: '/assets/',
filename: 'app-bundle.js'
}
};

0 comments on commit a8b3a58

Please sign in to comment.