Skip to content

Commit 727231a

Browse files
committed
npm and webpack example
1 parent 80f7326 commit 727231a

12 files changed

Lines changed: 356 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.stubbornjava.examples.undertow.handlebars;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.google.common.base.Strings;
7+
import com.stubbornjava.common.SimpleResponse;
8+
import com.stubbornjava.common.undertow.Exchange;
9+
import com.stubbornjava.common.undertow.SimpleServer;
10+
import com.stubbornjava.common.undertow.handlers.CustomHandlers;
11+
import com.stubbornjava.undertow.handlers.MiddlewareBuilder;
12+
13+
import io.undertow.server.HttpHandler;
14+
import io.undertow.server.HttpServerExchange;
15+
import io.undertow.server.RoutingHandler;
16+
import io.undertow.server.handlers.BlockingHandler;
17+
18+
public class WebpackServer {
19+
private static final Logger log = LoggerFactory.getLogger(WebpackServer.class);
20+
21+
public static void notFound(HttpServerExchange exchange) {
22+
exchange.setStatusCode(404);
23+
Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/notfound", SimpleResponse.create());
24+
}
25+
26+
public static void error(HttpServerExchange exchange) {
27+
exchange.setStatusCode(500);
28+
Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/serverError", SimpleResponse.create());
29+
}
30+
31+
// Render homepage
32+
public static void home(HttpServerExchange exchange) {
33+
exception(exchange);
34+
Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/home", SimpleResponse.create());
35+
}
36+
37+
// Render hello {name} page.
38+
public static void hello(HttpServerExchange exchange) {
39+
exception(exchange);
40+
String name = Exchange.queryParams()
41+
.queryParam(exchange, "name")
42+
.filter(s -> !Strings.isNullOrEmpty(s))
43+
.orElse("world");
44+
SimpleResponse response = SimpleResponse.create()
45+
.with("name", name);
46+
Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/hello", response);
47+
}
48+
49+
private static void exception(HttpServerExchange exchange) {
50+
if (Exchange.queryParams().queryParamAsBoolean(exchange, "exception").orElse(false)) {
51+
throw new RuntimeException("Poorly Named Exception!!!");
52+
}
53+
}
54+
55+
// {{start:server}}
56+
private static HttpHandler exceptionHandler(HttpHandler next) {
57+
return CustomHandlers.exception(next)
58+
.addExceptionHandler(Throwable.class, WebpackServer::error);
59+
}
60+
61+
private static HttpHandler wrapWithMiddleware(HttpHandler handler) {
62+
return MiddlewareBuilder.begin(BlockingHandler::new)
63+
.next(CustomHandlers::gzip)
64+
.next(ex -> CustomHandlers.accessLog(ex, log))
65+
.next(CustomHandlers::statusCodeMetrics)
66+
.next(WebpackServer::exceptionHandler)
67+
.complete(handler);
68+
}
69+
70+
private static final HttpHandler ROUTES = new RoutingHandler()
71+
.get("/", WebpackServer::home)
72+
.get("/hello", WebpackServer::hello)
73+
.get("/static*", CustomHandlers.resource(""))
74+
.setFallbackHandler(WebpackServer::notFound)
75+
;
76+
77+
public static void main(String[] args) {
78+
SimpleServer server = SimpleServer.simpleServer(wrapWithMiddleware(ROUTES));
79+
server.start();
80+
}
81+
// {{end:server}}
82+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "StubbornJava-examples",
3+
"description": "StubbornJava-examples",
4+
"author": "",
5+
"dependencies": {
6+
"anchor-js": "^3.2.1",
7+
"bootstrap": "^4.0.0-alpha.6",
8+
"font-awesome": "^4.5.0",
9+
"jquery": "^3.1.0"
10+
},
11+
"devDependencies": {
12+
"babel": "^6.23.0",
13+
"babel-core": "^6.23.1",
14+
"babel-loader": "^6.3.2",
15+
"babel-preset-es2015": "^6.22.0",
16+
"copy-webpack-plugin": "^4.0.1",
17+
"css-loader": "^0.26.1",
18+
"exports-loader": "^0.6.3",
19+
"extract-text-webpack-plugin": "^2.0.0-rc.3",
20+
"file-loader": "^0.10.0",
21+
"imports-loader": "^0.7.0",
22+
"node-sass": "^4.5.0",
23+
"optimize-css-assets-webpack-plugin": "^1.3.0",
24+
"raw-loader": "^0.5.1",
25+
"resolve-url-loader": "^2.0.0",
26+
"sass-loader": "^6.0.1",
27+
"style-loader": "^0.13.1",
28+
"url-loader": "^0.5.7",
29+
"webpack": "^2.2.1",
30+
"webpack-config": "^7.0.0"
31+
},
32+
"scripts": {}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import $ from 'jquery';
2+
3+
// Bootstrap
4+
import 'bootstrap/scss/bootstrap.scss';
5+
6+
import './app.scss';
7+
8+
// Add all the js files you need.
9+
import 'bootstrap/js/src/dropdown.js';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
$theme: #5cb85c;
2+
3+
body {
4+
padding-top: 70px;
5+
}
6+
7+
// footer
8+
$footer-height: 60px;
9+
10+
html {
11+
position: relative;
12+
min-height: 100%;
13+
}
14+
15+
body {
16+
margin-bottom: $footer-height;
17+
}
18+
19+
.footer {
20+
margin-top: 35px;
21+
border-top: 3px solid $theme;
22+
23+
// sticky footer
24+
position: absolute;
25+
bottom: 0;
26+
width: 100%;
27+
/* Set the fixed height of the footer here */
28+
29+
footer {
30+
color: $theme;
31+
padding: 15px 10px 4px;
32+
//border-top: 1px solid rgba(255, 255, 255, 0.05);
33+
font-size: 12px;
34+
background: #292b2c;
35+
a {
36+
color: $theme;
37+
}
38+
}
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="footer">
2+
<footer>
3+
<div class="container">
4+
<p class="pull-left"><a href="/">StubbornJava</a> © 2017</p>
5+
<div class="clearfix"></div>
6+
</div>
7+
</footer>
8+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{#> static/templates/src/layout}}
2+
{{#*inline "title"}}Hello {{name}}!{{/inline}}
3+
{{#*inline "content"}}
4+
<div class="page-heading">
5+
<div class="container center-text">
6+
<h1>Hello {{name}}!</h1>
7+
<p>Edit the name by modifying the <code>?name=</code> query parameter. Empty string or a non existent parameter defaults to world.</p>
8+
</div>
9+
</div>
10+
{{/inline}}
11+
{{/ static/templates/src/common/_base-layout}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{#> static/templates/src/layout}}
2+
{{#*inline "title"}}Stubborn Java Demo{{/inline}}
3+
{{#*inline "content"}}
4+
<div class="page-heading">
5+
<div class="container center-text">
6+
<h1>Home Tagline!</h1>
7+
<ul class="list-unstyled">
8+
<li><a href="/hello?name=">Hello World</a></li>
9+
<li><a href="/hello?name=StubbornJava">Hello StubbornJava</a></li>
10+
</ul>
11+
</div>
12+
</div>
13+
{{/inline}}
14+
{{/ static/templates/src/common/_base-layout}}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
6+
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
7+
<meta name="description" content="{{#if metaDesc }}{{metaDesc}}{{/if}}"/>
8+
<link rel="stylesheet" href="/static/css/app.css"/>
9+
10+
<title>{{> title}}</title>
11+
</head>
12+
13+
<body>
14+
{{> static/templates/src/nav}}
15+
{{> content}}
16+
17+
{{> static/templates/src/footer}}
18+
<script type="text/javascript" src="/static/js/app.js"></script>
19+
</body>
20+
</html>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<nav class="navbar navbar-toggleable-sm navbar-inverse fixed-top bg-inverse">
2+
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
3+
<span class="navbar-toggler-icon"></span>
4+
</button>
5+
<a class="navbar-brand" href="/">Demo Company</a>
6+
7+
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
8+
<ul class="navbar-nav mr-auto">
9+
<li class="nav-item active">
10+
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
11+
</li>
12+
<li class="nav-item">
13+
<a class="nav-link" href="#">Link</a>
14+
</li>
15+
<li class="nav-item">
16+
<a class="nav-link disabled" href="#">Disabled</a>
17+
</li>
18+
<li class="nav-item dropdown">
19+
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
20+
<div class="dropdown-menu" aria-labelledby="dropdown01">
21+
<a class="dropdown-item" href="#">Action</a>
22+
<a class="dropdown-item" href="#">Another action</a>
23+
<a class="dropdown-item" href="#">Something else here</a>
24+
</div>
25+
</li>
26+
</ul>
27+
<form class="form-inline my-2 my-lg-0">
28+
<input class="form-control mr-sm-2" type="text" placeholder="Search">
29+
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
30+
</form>
31+
</div>
32+
</nav>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{#> static/templates/src/layout}}
2+
{{#*inline "title"}}Page Not Found{{/inline}}
3+
{{#*inline "content"}}
4+
<div class="page-heading">
5+
<div class="container center-text">
6+
<h1>Page Not Found.</h1>
7+
</div>
8+
</div>
9+
{{/inline}}
10+
{{/ static/templates/src/_base-layout}}

0 commit comments

Comments
 (0)