Search in sources :

Example 1 with AssetPipelineModule

use of asset.pipeline.ratpack.AssetPipelineModule in project ratpack by ratpack.

the class SiteMain method main.

public static void main(String... args) throws Exception {
    RxRatpack.initialize();
    RatpackServer.start(b -> b.serverConfig(s -> s.baseDir(BaseDir.find()).env().require("/github", SiteModule.GitHubConfig.class)).registry(Guice.registry(s -> s.module(NewRelicModule.class).module(new AssetPipelineModule()).module(new DropwizardMetricsModule(), c -> c.csv(csv -> csv.enable(false))).module(SiteModule.class).module(MarkupTemplateModule.class, conf -> {
        conf.setAutoNewLine(true);
        conf.setUseDoubleQuotes(true);
        conf.setAutoIndent(true);
    }).module(TextTemplateModule.class, conf -> conf.setStaticallyCompile(true)))).handlers(c -> {
        int longCache = 60 * 60 * 24 * 365;
        int shortCache = 60 * 10;
        c.all(ctx -> {
            String host = ctx.getRequest().getHeaders().get("host");
            if (host != null && (host.endsWith("ratpack-framework.org") || host.equals("www.ratpack.io"))) {
                ctx.redirect(301, "http://ratpack.io" + ctx.getRequest().getRawUri());
                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();
        }).get("index.html", ctx -> {
            ctx.redirect(301, "/");
        }).get(ctx -> ctx.render(groovyMarkupTemplate("index.gtpl"))).get("resources", ctx -> ctx.render(groovyMarkupTemplate("resources.gtpl"))).path("reset", ctx -> ctx.byMethod(methods -> {
            Block impl = () -> {
                ctx.get(GitHubData.class).forceRefresh();
                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.all(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(Registry.single(ctx.getFileSystemBinding().binding(version.getVersion())));
            });
        }).files(f -> f.indexFiles("index.html"))))).get("robots.txt", ctx -> ctx.get(RatpackVersions.class).getAll().map(RatpackVersions.All::getAll).map(v -> FluentIterable.from(v).transform(i -> "Disallow: /manual/" + i.getVersion()).join(Joiner.on("\n"))).map(s -> "User-agent: *\nDisallow: /manual/snapshot\n" + s).then(ctx::render)).get("favicon.ico", ctx -> {
            ctx.getResponse().getHeaders().add("Cache-Control", "max-age=" + longCache + ", public");
            ctx.next();
        }).files(f -> f.dir("public").indexFiles("index.html"));
    }));
}
Also used : TextTemplateModule(ratpack.groovy.template.TextTemplateModule) RxRatpack(ratpack.rx.RxRatpack) RatpackServer(ratpack.server.RatpackServer) BaseDir(ratpack.server.BaseDir) NewRelicModule(ratpack.newrelic.NewRelicModule) Guice(ratpack.guice.Guice) RatpackVersions(ratpack.site.github.RatpackVersions) Block(ratpack.func.Block) Pair(ratpack.func.Pair) DropwizardMetricsModule(ratpack.dropwizard.metrics.DropwizardMetricsModule) AssetPipelineModule(asset.pipeline.ratpack.AssetPipelineModule) FluentIterable(com.google.common.collect.FluentIterable) Registry(ratpack.registry.Registry) GitHubData(ratpack.site.github.GitHubData) Groovy.groovyMarkupTemplate(ratpack.groovy.Groovy.groovyMarkupTemplate) MarkupTemplateModule(ratpack.groovy.template.MarkupTemplateModule) RatpackVersion(ratpack.site.github.RatpackVersion) Joiner(com.google.common.base.Joiner) MarkupTemplateModule(ratpack.groovy.template.MarkupTemplateModule) RatpackVersions(ratpack.site.github.RatpackVersions) NewRelicModule(ratpack.newrelic.NewRelicModule) AssetPipelineModule(asset.pipeline.ratpack.AssetPipelineModule) GitHubData(ratpack.site.github.GitHubData) Block(ratpack.func.Block) RatpackVersion(ratpack.site.github.RatpackVersion) DropwizardMetricsModule(ratpack.dropwizard.metrics.DropwizardMetricsModule)

Aggregations

AssetPipelineModule (asset.pipeline.ratpack.AssetPipelineModule)1 Joiner (com.google.common.base.Joiner)1 FluentIterable (com.google.common.collect.FluentIterable)1 DropwizardMetricsModule (ratpack.dropwizard.metrics.DropwizardMetricsModule)1 Block (ratpack.func.Block)1 Pair (ratpack.func.Pair)1 Groovy.groovyMarkupTemplate (ratpack.groovy.Groovy.groovyMarkupTemplate)1 MarkupTemplateModule (ratpack.groovy.template.MarkupTemplateModule)1 TextTemplateModule (ratpack.groovy.template.TextTemplateModule)1 Guice (ratpack.guice.Guice)1 NewRelicModule (ratpack.newrelic.NewRelicModule)1 Registry (ratpack.registry.Registry)1 RxRatpack (ratpack.rx.RxRatpack)1 BaseDir (ratpack.server.BaseDir)1 RatpackServer (ratpack.server.RatpackServer)1 GitHubData (ratpack.site.github.GitHubData)1 RatpackVersion (ratpack.site.github.RatpackVersion)1 RatpackVersions (ratpack.site.github.RatpackVersions)1