Skip to content

Commit

Permalink
Refactored for native image support builds (#290)
Browse files Browse the repository at this point in the history
petruki authored Dec 4, 2024
1 parent e81eec1 commit b33ddae
Showing 23 changed files with 330 additions and 210 deletions.
27 changes: 27 additions & 0 deletions Dockerfile.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ghcr.io/graalvm/graalvm-community:22 AS build

ARG MAVEN_VERSION=3.9.9
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries

RUN mkdir -p /usr/share/maven /usr/share/maven/ref
ADD ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz /tmp/apache-maven.tar.gz
RUN tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

WORKDIR /usr/src/app

COPY pom.xml .
RUN mvn dependency:go-offline

COPY . .

RUN mvn -DskipTests -Pprod -Pnative native:compile

FROM debian:bookworm-slim

WORKDIR /app

COPY --from=build /usr/src/app/target/switcher-ac /app/switcher-ac

CMD ["/app/switcher-ac"]
60 changes: 32 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<version>3.4.0</version>
</parent>

<properties>
@@ -59,14 +59,15 @@
<maven.compiler.target>${java.version}</maven.compiler.target>

<jersey.version>4.0.0-M1</jersey.version>
<switcher-client.version>2.2.0</switcher-client.version>
<switcher-client.version>2.2.1</switcher-client.version>
<jsonwebtoken.version>0.12.6</jsonwebtoken.version>
<joda-time.version>2.13.0</joda-time.version>
<gson.version>2.11.0</gson.version>
<springdoc.version>2.6.0</springdoc.version>
<springdoc.version>2.7.0</springdoc.version>

<!-- Test-->
<flapdoodle.embed.mongo.version>4.16.1</flapdoodle.embed.mongo.version>
<okhttp.version>4.12.0</okhttp.version>

<!-- Plugins -->
<native-image-plugin.version>0.10.3</native-image-plugin.version>
@@ -86,21 +87,27 @@
</properties>

<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>local</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-image-plugin.version}</version>
<configuration>
<mainClass>com.github.switcherapi.ac.SwitcherAcApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>coverage</id>
<build>
@@ -268,20 +275,28 @@

<!-- Test -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${okhttp.version}</version>
<scope>test</scope>
</dependency>

@@ -297,12 +312,6 @@
<version>${flapdoodle.embed.mongo.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
@@ -317,11 +326,6 @@
</pluginManagement>

<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-image-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Original file line number Diff line number Diff line change
@@ -9,8 +9,6 @@
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.ComponentScan;

import static com.github.switcherapi.ac.config.SwitcherFeatures.checkSwitchers;

@SpringBootApplication
@ConfigurationPropertiesScan
@ComponentScan(basePackages = { "com.github.switcherapi.ac" })
@@ -29,8 +27,6 @@ public static void main(String[] args) {

@Override
public void run(String... args) {
checkSwitchers();

log.info("Loading default Plan...");
planService.createPlan(Plan.loadDefault());
log.info("Plan loaded");
63 changes: 0 additions & 63 deletions src/main/java/com/github/switcherapi/ac/config/SwitcherConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package com.github.switcherapi.ac.config;

import com.github.switcherapi.ac.util.FileUtil;
import com.github.switcherapi.client.SnapshotCallback;
import com.github.switcherapi.client.SwitcherContextBase;
import com.github.switcherapi.client.SwitcherKey;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Slf4j
@Getter
@Setter
@ConfigurationProperties(prefix = "switcher")
public class SwitcherFeatures extends SwitcherContextBase implements SnapshotCallback {

public class SwitcherFeatures extends SwitcherContextBase {

@SwitcherKey
public static final String SWITCHER_AC_ADM = "SWITCHER_AC_ADM";

private String relayCode;

@PostConstruct
@Override
protected void configureClient() {
super.truststore.setPath(FileUtil.getFilePathFromResource(truststore.getPath()));
super.configureClient();

scheduleSnapshotAutoUpdate(snapshot.getUpdateInterval(), this);
checkSwitchers();
}

@Override
public void onSnapshotUpdate(long version) {
log.info("Snapshot updated: {}", version);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.switcherapi.ac.controller;

import com.github.switcherapi.ac.config.SwitcherConfig;
import com.github.switcherapi.ac.config.SwitcherFeatures;
import com.github.switcherapi.ac.model.domain.FeaturePayload;
import com.github.switcherapi.ac.model.dto.RequestRelayDTO;
import com.github.switcherapi.ac.model.dto.ResponseRelayDTO;
import com.github.switcherapi.ac.service.AccountService;
import com.github.switcherapi.ac.service.ValidatorService;
import com.github.switcherapi.ac.service.validator.ValidatorFactory;
import com.github.switcherapi.ac.service.ValidatorBasicService;
import com.github.switcherapi.ac.service.validator.ValidatorBuilderService;
import com.google.gson.Gson;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.ResponseEntity;
@@ -25,26 +25,26 @@ public class SwitcherRelayController {

private final AccountService accountService;

private final ValidatorFactory validatorFactory;
private final ValidatorBuilderService validatorBuilderService;

private final ValidatorService validatorService;
private final ValidatorBasicService validatorBasicService;

private final SwitcherConfig switcherConfig;
private final SwitcherFeatures switcherConfig;

public SwitcherRelayController(
AccountService accountService,
ValidatorFactory validatorFactory,
ValidatorService validatorService,
SwitcherConfig switcherConfig) {
AccountService accountService,
ValidatorBuilderService validatorBuilderService,
ValidatorBasicService validatorBasicService,
SwitcherFeatures switcherConfig) {
this.accountService = accountService;
this.validatorFactory = validatorFactory;
this.validatorService = validatorService;
this.validatorBuilderService = validatorBuilderService;
this.validatorBasicService = validatorBasicService;
this.switcherConfig = switcherConfig;
}

@GetMapping(value = "/verify")
public ResponseEntity<Object> verify() {
return ResponseEntity.ok(Map.of("code", switcherConfig.relayCode()));
return ResponseEntity.ok(Map.of("code", switcherConfig.getRelayCode()));
}

@Operation(summary = "Load new account to Switcher AC")
@@ -78,7 +78,7 @@ public ResponseEntity<ResponseRelayDTO> limiter(@RequestParam String value) {
.owner(value)
.build();

return ResponseEntity.ok(validatorFactory.runValidator(request));
return ResponseEntity.ok(validatorBuilderService.runValidator(request));
} catch (ResponseStatusException e) {
return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.fail(e.getMessage()));
}
@@ -89,7 +89,7 @@ public ResponseEntity<ResponseRelayDTO> limiter(@RequestParam String value) {
public ResponseEntity<Object> validate(@RequestBody RequestRelayDTO request) {
try {
var featureRequest = gson.fromJson(request.payload(), FeaturePayload.class);
return ResponseEntity.ok(validatorService.execute(featureRequest));
return ResponseEntity.ok(validatorBasicService.execute(featureRequest));
} catch (ResponseStatusException e) {
return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.fail(e.getMessage()));
}
29 changes: 10 additions & 19 deletions src/main/java/com/github/switcherapi/ac/model/domain/Plan.java
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import java.util.List;

import static com.github.switcherapi.ac.model.domain.Feature.*;
import static com.github.switcherapi.ac.model.domain.PlanDefaults.*;

@Generated
@JsonInclude(Include.NON_NULL)
@@ -24,16 +25,6 @@
@AllArgsConstructor
public class Plan {

public static final int DEFAULT_DOMAIN = 1;
public static final int DEFAULT_GROUP = 2;
public static final int DEFAULT_SWITCHER = 3;
public static final int DEFAULT_ENVIRONMENT = 2;
public static final int DEFAULT_COMPONENT = 2;
public static final int DEFAULT_TEAM = 1;
public static final int DEFAULT_RATE_LIMIT = 100;
public static final boolean DEFAULT_HISTORY = false;
public static final boolean DEFAULT_METRICS = false;

@Id
private String id;

@@ -66,15 +57,15 @@ public static Plan loadDefault() {
return Plan.builder()
.name(PlanType.DEFAULT.name())
.attributes(Arrays.asList(
PlanAttribute.builder().feature(DOMAIN.getValue()).value(DEFAULT_DOMAIN).build(),
PlanAttribute.builder().feature(GROUP.getValue()).value(DEFAULT_GROUP).build(),
PlanAttribute.builder().feature(SWITCHER.getValue()).value(DEFAULT_SWITCHER).build(),
PlanAttribute.builder().feature(ENVIRONMENT.getValue()).value(DEFAULT_ENVIRONMENT).build(),
PlanAttribute.builder().feature(COMPONENT.getValue()).value(DEFAULT_COMPONENT).build(),
PlanAttribute.builder().feature(TEAM.getValue()).value(DEFAULT_TEAM).build(),
PlanAttribute.builder().feature(RATE_LIMIT.getValue()).value(DEFAULT_RATE_LIMIT).build(),
PlanAttribute.builder().feature(HISTORY.getValue()).value(DEFAULT_HISTORY).build(),
PlanAttribute.builder().feature(METRICS.getValue()).value(DEFAULT_METRICS).build()
PlanAttribute.builder().feature(DOMAIN.getValue()).value(DEFAULT_DOMAIN.getIntValue()).build(),
PlanAttribute.builder().feature(GROUP.getValue()).value(DEFAULT_GROUP.getIntValue()).build(),
PlanAttribute.builder().feature(SWITCHER.getValue()).value(DEFAULT_SWITCHER.getIntValue()).build(),
PlanAttribute.builder().feature(ENVIRONMENT.getValue()).value(DEFAULT_ENVIRONMENT.getIntValue()).build(),
PlanAttribute.builder().feature(COMPONENT.getValue()).value(DEFAULT_COMPONENT.getIntValue()).build(),
PlanAttribute.builder().feature(TEAM.getValue()).value(DEFAULT_TEAM.getIntValue()).build(),
PlanAttribute.builder().feature(RATE_LIMIT.getValue()).value(DEFAULT_RATE_LIMIT.getIntValue()).build(),
PlanAttribute.builder().feature(HISTORY.getValue()).value(DEFAULT_HISTORY.getValue()).build(),
PlanAttribute.builder().feature(METRICS.getValue()).value(DEFAULT_METRICS.getValue()).build()
)).build();
}

Loading

0 comments on commit b33ddae

Please sign in to comment.