Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Banner bean initialization #312

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/ch/usi/si/seart/config/BannerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ch.usi.si.seart.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.ResourceBanner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

@Configuration
public class BannerConfig {

@Bean
public Banner banner(@Value("${spring.banner.location}") Resource resource) {
if (resource.exists()) return new ResourceBanner(resource);
else return (environment, sourceClass, out) -> {
};
}
}
29 changes: 19 additions & 10 deletions src/main/java/ch/usi/si/seart/controller/RootController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AccessLevel;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.info.BuildProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.time.Instant;

@Slf4j
@RestController
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@Tag(
Expand All @@ -33,14 +35,21 @@ public class RootController {
BuildProperties buildProperties;

@Autowired
@SneakyThrows(IOException.class)
public RootController(
@Value("${spring.banner.location}") Resource resource,
Banner banner,
Environment environment,
BuildProperties buildProperties
) {
InputStream inputStream = resource.getInputStream();
this.banner = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
this.buildProperties = buildProperties;
try (
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream)
) {
banner.printBanner(environment, null, printStream);
this.banner = outputStream.toString();
this.buildProperties = buildProperties;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

@GetMapping
Expand Down