Skip to content

Commit

Permalink
feat: user-agent from build info
Browse files Browse the repository at this point in the history
expose build info in actuator
  • Loading branch information
jmesserli committed Oct 31, 2023
1 parent f10022f commit 356c43b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
</env>
</image>
</configuration>

<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/nu/peg/svmeal/SvmealApiApplication.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package nu.peg.svmeal;

import nu.peg.svmeal.infrastructure.config.CacheProperties;
import nu.peg.svmeal.infrastructure.config.SvmealProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
@EnableConfigurationProperties(value = {CacheProperties.class})
@EnableConfigurationProperties(value = {CacheProperties.class, SvmealProperties.class})
public class SvmealApiApplication {
public static void main(String[] args) {
SpringApplication.run(SvmealApiApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nu.peg.svmeal.infrastructure.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "svmeal")
public record SvmealProperties(String userAgentLink) {}
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
package nu.peg.svmeal.infrastructure.http;

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nu.peg.svmeal.infrastructure.config.SvmealProperties;
import org.springframework.boot.info.BuildProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class SvmealUserAgentInterceptor implements SvmealInterceptor {
private final String userAgent;
private final BuildProperties buildProperties;
private final SvmealProperties svmealProperties;

@Autowired
public SvmealUserAgentInterceptor(@Value("${svmeal.user-agent}") String userAgent) {
this.userAgent = userAgent;
@Getter(lazy = true)
private final String userAgent = userAgent();

private String userAgent() {
final var userAgent =
"%s/%s (%s)"
.formatted(
buildProperties.getArtifact(),
buildProperties.getVersion(),
svmealProperties.userAgentLink());

log.debug("User-Agent: {}", userAgent);

return userAgent;
}

@Override
public ClientHttpResponse intercept(
HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution)
throws IOException {
HttpHeaders headers = httpRequest.getHeaders();
headers.set("User-Agent", userAgent);
headers.set("User-Agent", getUserAgent());

return clientHttpRequestExecution.execute(httpRequest, bytes);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ svmeal:
restaurant-dtos:
expire-after-write: 5s
meal-plan:
expire-after-write: 5s
expire-after-write: 5s
19 changes: 18 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ resilience4j:
waitDurationInOpenState: 5m

svmeal:
user-agent: sv-meal/4.1.1 github.com/jmesserli/svmeal-api
user-agent-link: github.com/jmesserli/svmeal-api
cache:
restaurants:
max-size: 1
Expand All @@ -26,3 +26,20 @@ svmeal:
meal-plan:
max-size: 50
expire-after-write: 15m

management:
endpoints:
web:
exposure:
include: health,info
endpoint:
health:
show-details: always

health:
circuitbreakers:
enabled: true

logging:
level:
nu.peg.svmeal: DEBUG

0 comments on commit 356c43b

Please sign in to comment.