Skip to content

Commit 349454f

Browse files
committed
- add example
npm install npm run autobuild npm run start
1 parent 80c8416 commit 349454f

File tree

10 files changed

+644
-0
lines changed

10 files changed

+644
-0
lines changed

examples/website/build.hxml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
### COMMON
2+
3+
# Full dead code elimination
4+
-dce full
5+
# Enable the analyzer optimizations
6+
-D analyzer-optimize
7+
# Compress javascript and css
8+
-D compress
9+
# Use uglifyJS instead of closure to compress javascript
10+
-D uglifyjs
11+
# https://haxe.org/manual/target-javascript-expose.html
12+
-D shallow-expose
13+
# Ithril provides templating and mithril:
14+
-lib ithril
15+
# Classpaths:
16+
# primary source files
17+
-cp src
18+
# Haxe externs
19+
-cp extern
20+
# Utility classes
21+
-cp util
22+
23+
--each
24+
25+
### NPM DEPENDENCIES __________________________________________/`
26+
-cmd bash -c "if [ ! -d node_modules ]; then echo Running npm install && npm install; fi"
27+
--next
28+
29+
### CLIENT
30+
31+
-cmd echo Building browser.js
32+
--next
33+
# browser define
34+
-D browser
35+
# Main class
36+
-main App
37+
# Target output filename
38+
-js obj/browser.js
39+
--next
40+
41+
### WEBSERVER
42+
43+
-cmd echo Building webserver.js
44+
--next
45+
# js-kit provides js externs
46+
-lib js-kit
47+
# hxnodejs provides nodejs externs
48+
-lib hxnodejs
49+
# webserver define
50+
-D webserver
51+
# Main class
52+
-main App
53+
# Target output filename
54+
-js obj/webserver.out.js
55+
--next
56+
# Insert interpreter reference into output
57+
-cmd sed -i '1s/^/#!\/usr\/bin\/env node\n/' obj/webserver.out.js
58+
# Make output executable
59+
-cmd chmod a+x obj/webserver.out.js
60+
# Replace executable
61+
-cmd cp obj/webserver.out.js obj/webserver.js
62+
# Remove temporary output
63+
-cmd rm obj/webserver.out.js
64+
--next
65+
66+
### RENDERVIEW
67+
68+
-cmd echo Building renderview.js
69+
--next
70+
# nodejs define
71+
-D nodejs
72+
# js-kit provides js externs
73+
-lib js-kit
74+
# hxnodejs provides nodejs externs
75+
-lib hxnodejs
76+
# render view define
77+
-D renderview
78+
# Main class
79+
-main App
80+
# Target output filename
81+
-js obj/renderview.js
82+
--next
83+
84+
# Insert interpreter reference into output
85+
-cmd sed -i '1s/^/#!\/usr\/bin\/env node\n/' obj/renderview.js
86+
# Make output executable
87+
-cmd chmod a+x obj/renderview.js
88+
--next
89+
90+
# STATIC HTML
91+
-cmd echo Generating static html
92+
--next
93+
94+
# Output directory
95+
-cmd mkdir -p htdocs
96+
# Render static pages
97+
-cmd obj/renderview.js -c config/webserver-config.json -p / > htdocs/index.html
98+
-cmd obj/renderview.js -c config/webserver-config.json -p /page1.html > htdocs/page1.html
99+
-cmd obj/renderview.js -c config/webserver-config.json -p /page2.html > htdocs/page2.html
100+
-cmd obj/renderview.js -c config/webserver-config.json -p /page3.html > htdocs/page3.html
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
">>>": "Webserver Configuration File",
3+
4+
"html": {
5+
"path": "htdocs", "//":"Path to static HTML files",
6+
"render": true, "//":"Should rendered HTML be served",
7+
"charset": "utf-8", "//":"Default charset meta",
8+
"javascript": true, "//":"Include javascript",
9+
"css": true, "//":"Include css"
10+
},
11+
12+
"listen": {
13+
"port": "4200", "//":"Port to listen on",
14+
"host": "localhost", "//":"IP address to bind to",
15+
"httpLogFormat": "dev", "//":"Morgan HTTP format string",
16+
"compression": true, "//":"Enable gzip compression",
17+
"backlog": 511, "//":"Maximum queued connections"
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@import "../../node_modules/bootstrap/scss/bootstrap.scss";
2+
3+
#header {
4+
@extend .navbar-brand;
5+
}
6+
7+
#navigation {
8+
@extend .navbar;
9+
@extend .navbar-toggleable-md;
10+
@extend .navbar-inverse;
11+
@extend .bg-primary;
12+
13+
#brand {
14+
@extend .navbar-brand;
15+
}
16+
17+
ul {
18+
@extend .navbar-nav;
19+
@extend .mr-auto;
20+
li {
21+
@extend .nav-item;
22+
a {
23+
@extend .nav-link;
24+
}
25+
}
26+
}
27+
}
28+
29+
.content {
30+
margin-top: 0.42em;
31+
}

examples/website/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "website",
3+
"version": "1.0.0",
4+
"main": "obj/webserver.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"bootstrap": "4.0.0-alpha.6",
8+
"compression": "^1.6.2",
9+
"express": "^4.15.2",
10+
"mithril": "^1.1.1",
11+
"morgan": "^1.8.1",
12+
"nodemon": "^1.11.0"
13+
},
14+
"scripts": {
15+
"build": "haxe build.hxml",
16+
"start": "node_modules/nodemon/bin/nodemon.js -w obj/webserver.js -x obj/webserver.js -c config/webserver-config.json",
17+
"autobuild": "node_modules/nodemon/bin/nodemon.js -x \"haxe build.hxml\" -e \"hx hxml json scss\"",
18+
"clean": "rm -r htdocs obj node_modules .sass-cache"
19+
}
20+
}

examples/website/src/App.hx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import ithril.*;
2+
import ithril.M.*;
3+
using Reflect;
4+
// Main application entry point
5+
class App {
6+
public static var config:Dynamic = Config.load();
7+
#if browser
8+
static function main() View.execute();
9+
#elseif webserver
10+
static function main() WebServer.execute();
11+
#elseif renderview
12+
static function main() View.render({ path: App.config.path }, { send: Sys.println });
13+
#end
14+
}
15+
16+
// Application configuration
17+
class Config {
18+
static public function load() {
19+
App.config = {};
20+
#if !browser
21+
var args = Sys.args();
22+
var help = function help(msg) { Sys.println(msg); Sys.exit(0); }
23+
var argHandler = util.Args.generate([
24+
@doc("Configuration file") ["-c", "--config"] => function(config:String) App.config = haxe.Json.parse(sys.io.File.getContent(config)),
25+
@doc("Path to render") ["-p", "--path"] => function(path:String) App.config.path = path,
26+
_ => function(arg:String) help('Error: $arg\n')
27+
]);
28+
if (args.length < 2) help('Missing arguments\n' + argHandler.getDoc()); else argHandler.parse(args);
29+
#end
30+
App.config.routes = { };
31+
for (href in State.current.pages.fields()) App.config.routes.setField(href, { render: Website.view(href) });
32+
return App.config;
33+
}
34+
}
35+
36+
// Execute and render views
37+
class View {
38+
public static function execute() {
39+
routePrefix("");
40+
route(js.Browser.document.body, "/", App.config.routes);
41+
}
42+
43+
public static function render(request, response) {
44+
App.config.path = request.path;
45+
HTMLRenderer.render(App.config.routes.field(request.path).render())
46+
.then(function(rslt) { response.send(rslt); },
47+
function(err) { trace('ERR: $err'); });
48+
}
49+
}
50+
51+
// A node+express webserver
52+
#if (webserver && nodejs)
53+
class WebServer {
54+
static public function execute() {
55+
var srv = new js.npm.Express();
56+
if (App.config.listen.compression) srv.use(new js.npm.express.Compression());
57+
if (App.config.listen.httpLogFormat != null) srv.use(new js.npm.express.Morgan(App.config.listen.httpLogFormat));
58+
srv.get('/favicon.ico', function (req, res:Dynamic) res.status(404).end());
59+
if (App.config.html.render)
60+
srv.get("*", View.render);
61+
else
62+
srv.use('/', new js.npm.express.Static(App.config.html.path));
63+
srv.use(function(req, res:Dynamic) res.status(404).end());
64+
var http = cast js.node.Http.createServer().on('request', cast srv);
65+
http.listen(App.config.listen);
66+
}
67+
}
68+
#end

examples/website/src/Resources.hx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import util.From;
2+
class Resources {
3+
#if compress
4+
public static var javascript = [
5+
#if uglifyjs
6+
From.command('uglifyjs', [
7+
'--compress',
8+
'--mangle',
9+
'--',
10+
'node_modules/mithril/mithril.min.js',
11+
'obj/browser.js',
12+
])
13+
#else
14+
From.file('node_modules/mithril/mithril.min.js'),
15+
From.command('closure-compiler', [
16+
'-O', 'SIMPLE',
17+
'obj/browser.js',
18+
]),
19+
#end
20+
];
21+
22+
public static var css = [
23+
From.command('sass', [
24+
'include/css/style.scss',
25+
'--style', 'compressed',
26+
]),
27+
];
28+
#else
29+
public static var javascript = [
30+
From.file('node_modules/mithril/mithril.js'),
31+
From.file('obj/browser.js'),
32+
];
33+
public static var css = [
34+
From.command('sass', [
35+
'include/css/style.scss',
36+
'--style', 'compact'
37+
]),
38+
];
39+
#end
40+
}

examples/website/src/State.hx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import ithril.*;
2+
using Reflect;
3+
// Application state
4+
class State {
5+
static public function page(href:String)
6+
return Attributes.combine(Attributes.combine(State.current.pages.field(href), State.current), { href: href });
7+
8+
static public var current(get, null):Dynamic;
9+
static function get_current() {
10+
if (current == null) {
11+
current = {
12+
#if !browser
13+
javascript: App.config.html.javascript ? Resources.javascript : [],
14+
css: App.config.html.css ? Resources.css : [],
15+
meta: ([
16+
{ charset: App.config.html.charset },
17+
{ name: "viewport", content: "width=device-width, initial-scale=1.0" }
18+
]:Array<Dynamic>),
19+
#end
20+
favicon: "data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAA/7VrAP8AJgD///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEQARAAEAEQERABEQEQARERERERERABEREREREREAEREREREREQARERERERERABERMxERMxEAERMyIRMyIQABEzIhEzIgAAETMzETMzAAAREzEREzEAAAEREREREAAAABEREREAAAAAABERAAAAAAAAAAAAAAD//wAAuZ0AAJGJAACAAQAAgAEAAIABAACAAQAAgAEAAIABAADAAwAAwAMAAMADAADgBwAA8A8AAPw/AAD//wAA",
21+
faviconType: 'image/x-icon',
22+
23+
brand: 'Home',
24+
brandPage: '/',
25+
26+
pages: {
27+
28+
"/": {
29+
component: "HomePage",
30+
title: "Home",
31+
homeHeader: "Home",
32+
homeText: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc.",
33+
content: [
34+
{ header: 'Home-A', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
35+
{ header: 'Home-B', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
36+
{ header: 'Home-C', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
37+
]},
38+
39+
"/page1.html": {
40+
component: "ContentPage",
41+
title: "Page One",
42+
nav: "one",
43+
content: [
44+
{ header: 'Page One', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
45+
]},
46+
47+
"/page2.html": {
48+
component: "ContentPage",
49+
title: "Page Two",
50+
nav: "two",
51+
content: [
52+
{ header: 'Page Two', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
53+
]},
54+
55+
"/page3.html": {
56+
component: "ContentPage",
57+
title: "Page Three",
58+
nav: "three",
59+
content: [
60+
{ header: 'Page Three-A', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
61+
{ header: 'Page Three-B', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
62+
{ header: 'Page Three-C', text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et efficitur diam, feugiat lobortis nunc." },
63+
]},
64+
65+
},
66+
};
67+
}
68+
return current;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)