com.linecorp.armeria.server.file.FileService Java Examples

The following examples show how to use com.linecorp.armeria.server.file.FileService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From armeria with Apache License 2.0 6 votes vote down vote up
static Server newServer(int httpPort, int httpsPort) throws Exception {
    return Server.builder()
                 .http(httpPort)
                 .https(httpsPort)
                 .tlsSelfSigned()
                 // Serve an individual file.
                 .service("/favicon.ico",
                          HttpFile.of(Main.class.getClassLoader(), "favicon.ico")
                                  .asService())
                 // Serve the files under the current user's home directory.
                 .service("prefix:/",
                          FileService.builder(Paths.get(System.getProperty("user.home")))
                                     .autoIndex(true)
                                     .build())
                 .build();
}
 
Example #2
Source File: DocService.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance with example HTTP headers and example requests and injected scripts.
 */
DocService(Map<String, ListMultimap<String, HttpHeaders>> exampleHttpHeaders,
           Map<String, ListMultimap<String, String>> exampleRequests,
           Map<String, ListMultimap<String, String>> examplePaths,
           Map<String, ListMultimap<String, String>> exampleQueries,
           List<BiFunction<ServiceRequestContext, HttpRequest, String>> injectedScriptSuppliers,
           DocServiceFilter filter) {

    super(ofExact("/specification.json", FileService.of(new DocServiceVfs())),
          ofExact("/versions.json", FileService.of(new DocServiceVfs())),
          ofExact("/injected.js",
                  (ctx, req) -> HttpResponse.of(MediaType.JAVASCRIPT_UTF_8,
                                                injectedScriptSuppliers.stream()
                                                                       .map(f -> f.apply(ctx, req))
                                                                       .collect(Collectors.joining("\n")))),
          ofCatchAll(FileService.of(DocService.class.getClassLoader(),
                                    "com/linecorp/armeria/server/docs")));
    this.exampleHttpHeaders = immutableCopyOf(exampleHttpHeaders, "exampleHttpHeaders");
    this.exampleRequests = immutableCopyOf(exampleRequests, "exampleRequests");
    this.examplePaths = immutableCopyOf(examplePaths, "examplePaths");
    this.exampleQueries = immutableCopyOf(exampleQueries, "exampleQueries");
    this.filter = requireNonNull(filter, "filter");
}
 
Example #3
Source File: OrElseDefaultHttpFileService.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
public OrElseDefaultHttpFileService(FileService fileService, String defaultPath) {
    requireNonNull(fileService, "fileService");
    requireNonNull(defaultPath, "defaultPath");
    // Always return '/index.html' if there is no entry on the requested path, in order to route
    // the request by 'react-router'.
    delegate = fileService.orElse(
            (ctx, req) -> fileService.serve(new DefaultHtmlServiceRequestContext(ctx, defaultPath), req));
}
 
Example #4
Source File: StaticSiteService.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * 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 HttpService of(String staticPath, String classpathRoot) {
  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);

  return SimpleCompositeService.builder()
      .serviceUnder(staticPath, staticFileService)
      .serviceUnder("/", indexService)
      .build();
}
 
Example #5
Source File: DocService.java    From armeria with Apache License 2.0 4 votes vote down vote up
private DocServiceVfs vfs(int index) {
    return (DocServiceVfs) ((FileService) serviceAt(index)).config().vfs();
}