Skip to content

Commit

Permalink
Replace ceylon.http.server dependency with Takes.
Browse files Browse the repository at this point in the history
This reduces the size of the final jar by about one megabyte. This is
something of a compromise; the first lightweight HTTP server I found was
[JLHTTP](https://www.freeutils.net/source/jlhttp/), but as it's GPL-2 and this
project is GPL-3 its license is incompatible. The second I tried was
[NanoHTTPD](https://github.com/NanoHttpd/nanohttpd), but that triggered
eclipse-archived/ceylon-ide-intellij#669 in the IDE and a 'duplicate class' backend
error (reported as being in an unrelated module) in the command-line compiler,
so I had to give up on that. Because this pulls in dependencies too (just
slightly lighter-weight ones that ceylon.http.server), I should try NanoHTTPD
again when it makes its next release.
  • Loading branch information
kingjon3377 committed Feb 15, 2019
1 parent e3bf175 commit 2ca2d7b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 129 deletions.
11 changes: 11 additions & 0 deletions .idea/libraries/Ceylon__org_takes_takes_1_16.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions resource/strategicprimer/drivers/gui/common/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ <h1>App Name Here</h1>
<li>The Window menu is managed by a library by Jeremy Wood from <a
href="https://github.com/mickleness/pumpernickel">his Pumpernickel
project on GitHub</a>.)</li>
<li>The embedded Web server in the report generator and tabular report generator is
<a href="https://www.takes.org/">the Takes library</a>.</li>
</ul>

<p>Code snippets originally taken (though subsequently modified) from
Expand Down
222 changes: 101 additions & 121 deletions source/strategicprimer/viewer/drivers/report.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ import ceylon.collection {
MutableSet,
HashSet
}
import ceylon.io {
SocketAddress
}
import ceylon.http.server {
newServer,
AsynchronousEndpoint,
Endpoint,
Request,
Response,
startsWith,
matchEquals=equals,
isRoot
}
import ceylon.http.server.endpoints {
redirect
}
import ceylon.http.common {
get,
Header
}
import lovelace.util.common {
matchingValue,
silentListener,
Expand All @@ -89,6 +69,24 @@ import lovelace.util.common {
import lovelace.util.jvm {
FileChooser
}
import org.takes.facets.fork {
Fork,
FkRegex,
TkFork
}
import org.takes.rs {
RsHtml,
RsWithType,
RsWithHeader,
RsText
}
import org.takes.tk {
TkRedirect
}
import org.takes.http {
FtBasic,
Exit
}

"An object to help us present files with only as much of their paths as
necessary to uniquely identify them, without their shared prefix."
Expand Down Expand Up @@ -139,7 +137,7 @@ shared class ReportCLIFactory() satisfies ModelDriverFactory {
shared actual IDriverUsage usage = DriverUsage {
graphical = false;
invocations = ["create-report"];
paramsWanted = ParamCount.one;
paramsWanted = ParamCount.atLeastOne;
shortDescription = "Report Generator";
longDescription = "Produce HTML report of the contents of a map";
includeInCLIList = true;
Expand Down Expand Up @@ -187,48 +185,36 @@ class ReportServingCLI(SPOptions options, model) satisfies ReadOnlyDriver {
value localCache = cache.map(
(file->report) => suffixHelper.shortestSuffix(cache.keys,
file.absolutePath)->report);
{Endpoint*} endpoints = localCache.map((file->report) =>
Endpoint {
path = startsWith("/``file``");
service(Request request, Response response) =>
response.writeString(report);
});
Endpoint|AsynchronousEndpoint rootHandler;
{Fork*} endpoints = localCache.map((file->report) => FkRegex("/``file``",
RsHtml(report)));
Fork rootHandler;
if (localCache.size == 1) {
assert (exists soleFile = localCache.first?.key);
rootHandler = AsynchronousEndpoint {
path = isRoot();
acceptMethod = [ get ];
service = redirect("/" + soleFile);
};
rootHandler = FkRegex("/", TkRedirect("/" + soleFile));
} else {
rootHandler = Endpoint {
path = isRoot();
void service(Request request, Response response) {
response.writeString(
"<!DOCTYPE html>
<html>
<head>
<title>Strategic Primer Reports</title>
</head>
<body>
<h1>Strategic Primer Reports</h1>
<ul>
");
for (file->report in localCache) {
response.writeString(
" <li><a href=\"/``file``\">``file``</a></li>");
}
response.writeString(" </ul>
</body>
</html>");
}
};
StringBuilder builder = StringBuilder();
builder.append(
"""<!DOCTYPE html>
<html>
<head>
<title>Strategic Primer Reports</title>
</head>
<body>
<h1>Strategic Primer Reports</h1>
<ul>
""");
for (file->report in localCache) {
builder.appendAll([" <li><a href=\"", file, "\">", file,
"</a></li>"]);
builder.appendNewline();
}
builder.append(""" </ul>
</body>
</html>""");
rootHandler = FkRegex("/", RsHtml(builder.string));
}
log.info("About to start serving on port ``port``");
newServer {
rootHandler, *endpoints
}.start(SocketAddress("127.0.0.1", port));
FtBasic(TkFork(rootHandler, *endpoints), port).start(Exit.never);
}
}

Expand Down Expand Up @@ -465,72 +451,66 @@ class TabularReportServingCLI(SPOptions options, model) satisfies ReadOnlyDriver
parsePath(model.mapFile?.filename else "unknown.xml"));
}

{Endpoint*} endpoints = builders.map(([file, table]->builder) =>
Endpoint {
path = matchEquals("/``file``.``table``.csv");
void service(Request request, Response response) {
response.addHeader(Header("Content-Disposition",
"attachment; filename=\"``table``.csv\""));
response.writeString(builder.string);
}
});
{Endpoint*} tocs = mapping.keys
.map(curry(suffixHelper.shortestSuffix)(mapping.keys)).map(
(path) => Endpoint {
path = matchEquals("/``path``").or(matchEquals("/``path``/"));
void service(Request request, Response response) {
response.writeString(
"<!DOCTYPE html>
<html>
<head>
<title>Tabular reports for ``path``</title>
</head>
<body>
<h1>Tabular reports for ``path``</h1>
<ul>
");
for ([mapFile, table] in builders.keys
.filter(matchingValue(path,
Tuple<String, String, String[]>.first))) {
response.writeString(" <li><a href=\"/``mapFile
``.``table``.csv\">``table``.csv</a></li>\n");
}
response.writeString(" </ul>
</body>
</html>");
}
});
Endpoint rootHandler = Endpoint {
path = isRoot();
void service(Request request, Response response) {
response.writeString(
"<!DOCTYPE html>
<html>
<head>
<title>Strategic Primer Tabular Reports</title>
</head>
<body>
<h1>Strategic Primer Tabular Reports</h1>
<ul>
");
for (file in mapping.keys
.map(curry(suffixHelper.shortestSuffix)(mapping.keys))) {
response.writeString(
" <li><a href=\"/``file``\">``file``</a></li>");
}
response.writeString(
" </ul>
</body>
</html>");
{Fork*} endpoints = builders
.map(([file, table]->builder) => FkRegex("/``file``.``table``.csv",
RsWithType(RsWithHeader(RsText(builder.string),
"Content-Disposition", "attachment; filename=\"``table``.csv\""),
"text/csv")));
String tocHtml(String path) {
StringBuilder builder = StringBuilder();
builder.append(
"<!DOCTYPE html>
<html>
<head>
<title>Tabular reports for ``path``</title>
</head>
<body>
<h1>Tabular reports for ``path``</h1>
<ul>
");
for ([mapFile, table] in builders.keys.filter(matchingValue(path,
Tuple<String, String, [String]>.first))) {
builder.appendAll([" <li><a href=\"/", mapFile, ".", table,
".csv\">", table, ".csv</a></li>"]);
builder.appendNewline();
}
};
builder.append(
""" </ul>
</body>
</html>""");
return builder.string;
}
{Fork*} tocs = mapping.keys.map(curry(suffixHelper.shortestSuffix)(mapping.keys))
.flatMap(
(path) => [FkRegex("/``path``", RsHtml(tocHtml(path))),
FkRegex("/``path``/", RsHtml(tocHtml(path)))]);

StringBuilder rootDocument = StringBuilder();
rootDocument.append(
"""<!DOCTYPE html>
<html>
<head>
<title>Strategic Primer Tabular Reports</title>
</head>
<body>
<h1>Strategic Primer Tabular Reports</h1>
<ul>
""");
for (file in mapping.keys.map(curry(suffixHelper.shortestSuffix)(mapping.keys))) {
rootDocument.appendAll([" <li><a href=\"/", file, "\">", file,
"</a></li>"]);
rootDocument.appendNewline();
}
rootDocument.append(
""" </ul>
</body>
</html>""");

log.info("About to start serving on port ``port``");
newServer {
rootHandler, *endpoints.chain(tocs)
}.start(SocketAddress("127.0.0.1", port));
FtBasic(
TkFork(FkRegex("/", RsHtml(rootDocument.string)), FkRegex("/index.html",
RsHtml(rootDocument.string)), *tocs.chain(endpoints).sequence()), port)
.start(Exit.never);
}

shared actual void startDriver() {
Expand Down
3 changes: 2 additions & 1 deletion source/strategicprimer/viewer/module.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ module strategicprimer.viewer "0.4.9018" {
import strategicprimer.drivers.generators spVersion;
shared import strategicprimer.drivers.gui.common spVersion;
import strategicprimer.drivers.utility spVersion;
import ceylon.http.server ceylonVersion;
// import ceylon.http.server ceylonVersion;
import maven:"org.takes:takes" "1.16";
import com.vasileff.ceylon.structures "1.1.3";
import strategicprimer.mining spVersion;
import strategicprimer.drivers.query spVersion;
Expand Down
8 changes: 1 addition & 7 deletions viewer-ceylon.iml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.dbc/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.decimal/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.file/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.http.common/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.http.server/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.interop.java/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.io/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.language/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.logging/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.numeric/1.3.3" level="project" />
Expand All @@ -42,12 +39,9 @@
<orderEntry type="library" scope="PROVIDED" name="Ceylon: ceylon.whole/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.massisframework:orange-extensions/1.3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.pump.swing/1.0.00" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.redhat.ceylon.common/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.redhat.ceylon.langtools.classfile/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.redhat.ceylon.model/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.redhat.ceylon.module-resolver/1.3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.vasileff.ceylon.structures/1.1.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: com.zaxxer:SparseBitSet/1.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: org.takes:takes/1.16" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Ceylon: org.xerial:sqlite-jdbc/3.23.1" level="project" />
</component>
</module>

0 comments on commit 2ca2d7b

Please sign in to comment.