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

The following examples show how to use com.linecorp.armeria.server.file.HttpFile. 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: CentralDogma.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
static HttpFile webAppTitleFile(@Nullable String webAppTitle, String hostname) {
    requireNonNull(hostname, "hostname");
    final Map<String, String> titleAndHostname = ImmutableMap.of(
            "title", firstNonNull(webAppTitle, "Central Dogma at {{hostname}}"),
            "hostname", hostname);

    try {
        final HttpData data = HttpData.ofUtf8(Jackson.writeValueAsString(titleAndHostname));
        return HttpFile.builder(data)
                       .contentType(MediaType.JSON_UTF_8)
                       .cacheControl(ServerCacheControl.REVALIDATED)
                       .build();
    } catch (JsonProcessingException e) {
        throw new Error("Failed to encode the title and hostname:", e);
    }
}
 
Example #2
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 #3
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 #4
Source File: Main.java    From armeria with Apache License 2.0 5 votes vote down vote up
static Server newBackendServer(int port, int frameIntervalMillis) throws Exception {
    return Server.builder()
                 .http(port)
                 // Disable timeout to serve infinite streaming response.
                 .requestTimeoutMillis(0)
                 // Serve /index.html file.
                 .service("/", HttpFile.builder(Main.class.getClassLoader(), "index.html")
                                       .cacheControl(ServerCacheControl.REVALIDATED)
                                       .build()
                                       .asService())
                 .service("/animation", new AnimationService(frameIntervalMillis))
                 // Serve health check.
                 .service("/internal/l7check", HealthCheckService.of())
                 .build();
}
 
Example #5
Source File: Main.java    From armeria with Apache License 2.0 4 votes vote down vote up
static Server newServer(int httpPort, int httpsPort, Duration sendingInterval, long eventCount,
                        Supplier<String> randomStringSupplier) throws Exception {
    return Server.builder()
                 .http(httpPort)
                 .https(httpsPort)
                 .tlsSelfSigned()
                 .service("/long", (ctx, req) -> {
                     // Note that you MUST adjust the request timeout if you want to send events for a
                     // longer period than the configured request timeout. The timeout can be disabled by
                     // 'clearRequestTimeout()' like the below, but it is NOT RECOMMENDED in
                     // the real world application, because it can leave a lot of unfinished requests.
                     ctx.clearRequestTimeout();
                     return ServerSentEvents.fromPublisher(
                             Flux.interval(sendingInterval)
                                 .take(eventCount)
                                 .map(unused -> ServerSentEvent.ofData(randomStringSupplier.get())));
                 })
                 .annotatedService(new Object() {
                     // This shows how you can send events in the annotated HTTP service.
                     @Get("/short")
                     @ProducesEventStream
                     public Publisher<ServerSentEvent> sendEvents() {
                         // The event stream will be closed after
                         // the request timed out (10 seconds by default).
                         return Flux.interval(sendingInterval)
                                    .take(eventCount)
                                    // A user can use a builder to build a Server-Sent Event.
                                    .map(id -> ServerSentEvent.builder()
                                                              .id(Long.toString(id))
                                                              .data(randomStringSupplier.get())
                                                              // The client will reconnect to this server
                                                              // after 5 seconds when the on-going stream
                                                              // is closed.
                                                              .retry(Duration.ofSeconds(5))
                                                              .build());
                     }
                 })
                 .service("/", HttpFile.of(Main.class.getClassLoader(), "index.html").asService())
                 .decorator(LoggingService.newDecorator())
                 .disableServerHeader()
                 .disableDateHeader()
                 .build();
}
 
Example #6
Source File: DocService.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public HttpFile get(
        Executor fileReadExecutor, String path, Clock clock,
        @Nullable String contentEncoding, HttpHeaders additionalHeaders) {
    return file;
}