Skip to content

Instantly share code, notes, and snippets.

@ldaley
Created January 12, 2015 09:57
Show Gist options
  • Save ldaley/9a35e5711729bcaad243 to your computer and use it in GitHub Desktop.
Save ldaley/9a35e5711729bcaad243 to your computer and use it in GitHub Desktop.
ratpack {
bindings {
add JacksonModule
add RemoteControlModule
add NewRelicModule
add new CodaHaleMetricsModule().metrics()
add MarkupTemplateModule, {
it.with {
autoNewLine = true
useDoubleQuotes = true
autoIndent = true
}
}
add new SiteModule(serverConfig)
add(TextTemplateModule) { it.staticallyCompile = true }
RxRatpack.initialize()
}
handlers {
def longCache = 60 * 60 * 24 * 365
def shortCache = 60 * 10 // ten mins
get("dangle") {
}
handler {
if (request.headers.get("host").endsWith("ratpack-framework.org")) {
redirect 301, "http://www.ratpack.io"
return
}
if (request.path.empty || request.path == "index.html") {
response.headers.set "X-UA-Compatible", "IE=edge,chrome=1"
}
next()
}
prefix("assets") {
handler {
def cacheFor = request.query ? longCache : shortCache
response.headers.add("Cache-Control", "max-age=$cacheFor, public")
next()
}
assets "assets"
}
// The generated CSS has links to /images, remap
// https://github.com/robfletcher/gradle-compass/issues/12
prefix("images") {
handler {
def cacheFor = request.query ? longCache : shortCache
response.headers.add("Cache-Control", "max-age=$cacheFor, public")
next()
}
assets "assets/images"
}
get("index.html") {
redirect 301, "/"
}
get {
render groovyMarkupTemplate("index.gtpl")
}
handler("reset") { GitHubApi gitHubApi ->
byMethod {
if (serverConfig.development) {
get {
gitHubApi.invalidateCache()
render "ok"
}
}
post {
gitHubApi.invalidateCache()
render "ok"
}
}
}
prefix("versions") {
get { RatpackVersions versions ->
versions.all.subscribe { RatpackVersions.All all ->
render groovyMarkupTemplate("versions.gtpl", versions: all)
}
}
prefix(":version") {
get { RatpackVersions versions, GitHubData gitHubData ->
versions.all.subscribe { RatpackVersions.All all ->
def version = all.find(allPathTokens.version)
if (version == null) {
clientError(404)
} else {
gitHubData.closed(version).subscribe {
render groovyMarkupTemplate("version.gtpl", version: version, issues: it)
}
}
}
}
}
}
prefix("manual") {
fileSystem("manual") {
get {
redirect 301, "manual/current/"
}
prefix(":label") {
handler { RatpackVersions versions ->
def label = pathTokens.label
versions.all.subscribe { RatpackVersions.All allVersions ->
if (label == "current" || label in allVersions.released.version) {
response.headers.add("Cache-Control", "max-age=$longCache, public")
} else if (label == "snapshot" || label in allVersions.unreleased.version) {
response.headers.add("Cache-Control", "max-age=$shortCache, public")
}
def version
if (label == "current") {
version = allVersions.current
} else if (label == "snapshot") {
version = allVersions.snapshot
} else {
version = allVersions.find(label)
if (version == null) {
clientError 404
return
}
}
next(just(new DefaultFileSystemBinding(file(version.version))))
}
}
assets ""
}
}
}
assets "public"
}
}
public class SiteMain {
public static void main(String... args) throws Exception {
RxRatpack.initialize();
RatpackServer.start(b -> b
.config(ServerConfig.findBaseDirProps())
.handler(r ->
Guice.builder(r)
.bindings(s -> s
.add(JacksonModule.class)
.add(RemoteControlModule.class)
.add(NewRelicModule.class)
.add(new CodaHaleMetricsModule().metrics())
.add(new SiteModule(s.getServerConfig()))
.add(MarkupTemplateModule.class, conf -> {
conf.setAutoNewLine(true);
conf.setUseDoubleQuotes(true);
conf.setAutoIndent(true);
})
.add(TextTemplateModule.class, conf ->
conf.setStaticallyCompile(true)
)
)
.build(c -> {
int longCache = 60 * 60 * 24 * 365;
int shortCache = 60 * 10; // ten mins
c
.handler(ctx -> {
//noinspection ConstantConditions
if (ctx.getRequest().getHeaders().get("host").endsWith("ratpack-framework.org")) {
ctx.redirect(301, "http://www.ratpack.io");
return;
}
if (ctx.getRequest().getPath().isEmpty() || ctx.getRequest().getPath().equals("index.html")) {
ctx.getResponse().getHeaders().set("X-UA-Compatible", "IE=edge,chrome=1");
}
ctx.next();
})
.prefix("assets", assets -> assets
.handler(ctx -> {
int cacheFor = ctx.getRequest().getQuery().isEmpty() ? longCache : shortCache;
ctx.getResponse().getHeaders().add("Cache-Control", "max-age=" + cacheFor + ", public");
ctx.next();
})
.assets("assets")
)
.get("index.html", ctx -> {
ctx.redirect(301, "/");
})
.get(ctx -> ctx.render(groovyMarkupTemplate("index.gtpl")))
.handler("reset", ctx -> {
GitHubApi gitHubApi = ctx.get(GitHubApi.class);
ctx.byMethod(methods -> {
NoArgAction impl = () -> {
gitHubApi.invalidateCache();
ctx.render("ok");
};
if (ctx.getServerConfig().isDevelopment()) {
methods.get(impl);
}
methods.post(impl);
});
})
.prefix("versions", v -> v
.get(ctx ->
ctx.render(
ctx.get(RatpackVersions.class).getAll()
.map(all -> groovyMarkupTemplate("versions.gtpl", m -> m.put("versions", all)))
)
)
.get(":version", ctx ->
ctx.render(
ctx.get(RatpackVersions.class).getAll()
.map(all -> all.version(ctx.getAllPathTokens().get("version")))
.onNull(() -> ctx.clientError(404))
.flatMap(version -> ctx.get(GitHubData.class).closed(version).map(i -> Pair.of(version, i)))
.map(p -> groovyMarkupTemplate("version.gtpl", m -> m.put("version", p.left).put("issues", p.right)))
)
)
)
.prefix("manual", c1 -> c1
.fileSystem("manual", c2 -> c2
.get(ctx -> ctx.redirect(301, "manual/current"))
.prefix(":label", c3 -> c3
.handler(ctx -> {
String label = ctx.getPathTokens().get("label");
ctx.get(RatpackVersions.class).getAll().then(all -> {
if (label.equals("current") || all.isReleased(label)) {
ctx.getResponse().getHeaders().add("Cache-Control", "max-age=" + longCache + ", public");
} else if (label.equals("snapshot") || all.isUnreleased(label)) {
ctx.getResponse().getHeaders().add("Cache-Control", "max-age=" + shortCache + ", public");
}
RatpackVersion version;
switch (label) {
case "current":
version = all.getCurrent();
break;
case "snapshot":
version = all.getSnapshot();
break;
default:
version = all.version(label);
if (version == null) {
ctx.clientError(404);
return;
}
break;
}
ctx.next(just(new DefaultFileSystemBinding(ctx.file(version.getVersion()))));
});
})
.assets("")
)
)
)
.assets("public");
})
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment