Search in sources :

Example 1 with FileService

use of com.linecorp.armeria.server.file.FileService in project curiostack by curioswitch.

the class StaticSiteService method addToServer.

/**
 * Creates a new {@link StaticSiteService}.
 *
 * @param staticPath the URL path from which static resources will be served, e.g., "/static".
 * @param classpathRoot the root directory in the classpath to serve resources from.
 */
public static void addToServer(String urlRoot, String staticPath, String classpathRoot, ServerBuilder sb) {
    FileService staticFileService = FileService.builder(StaticSiteService.class.getClassLoader(), classpathRoot).serveCompressedFiles(true).cacheControl(ServerCacheControl.IMMUTABLE).addHeader(HttpHeaderNames.VARY, "Accept-Encoding").build();
    HttpService indexHtmlService = HttpFile.builder(StaticSiteService.class.getClassLoader(), classpathRoot + "/index.html").cacheControl(ServerCacheControl.DISABLED).build().asService();
    TrailingSlashAddingService indexService = FileService.builder(StaticSiteService.class.getClassLoader(), classpathRoot).serveCompressedFiles(true).cacheControl(ServerCacheControl.DISABLED).build().orElse(indexHtmlService).decorate(TrailingSlashAddingService::new);
    String urlRootWithTrailingSlash = urlRoot.endsWith("/") ? urlRoot : urlRoot + "/";
    String staticPathWithoutLeadingSlash = staticPath.startsWith("/") ? staticPath.substring(1) : staticPath;
    sb.serviceUnder(urlRootWithTrailingSlash + staticPathWithoutLeadingSlash, staticFileService).serviceUnder(urlRootWithTrailingSlash, indexService);
}
Also used : FileService(com.linecorp.armeria.server.file.FileService) SimpleDecoratingHttpService(com.linecorp.armeria.server.SimpleDecoratingHttpService) HttpService(com.linecorp.armeria.server.HttpService)

Aggregations

HttpService (com.linecorp.armeria.server.HttpService)1 SimpleDecoratingHttpService (com.linecorp.armeria.server.SimpleDecoratingHttpService)1 FileService (com.linecorp.armeria.server.file.FileService)1