Skip to content

Commit da6b053

Browse files
s-freikilmajster
andauthored
Added NgrokConfiguration class (#53)
* Add NgrokConfiguration for configuration properties - Additionally added 'lombok' to drastically reduce boilerplate * Replace all value injections through config class - Used lombok in edited classes - Added docs from redundant NgrokProperties to config class * Move javadoc and sources generation to default - Make them available in local maven repository without the need of the 'ossrh' profile and to sign artifacts * Add configuration-processor * Adjust javadoc in config class * Fix integration test * Add NgrokConfiguration to auto-config test * Config class javadoc fixes for examples * configuration processing refactor * Apply Lombok where possible Co-authored-by: kilmajster <[email protected]>
1 parent f3a43e4 commit da6b053

18 files changed

+259
-298
lines changed

pom.xml

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@
9292
<scope>provided</scope>
9393
</dependency>
9494

95+
<dependency>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-configuration-processor</artifactId>
98+
<optional>true</optional>
99+
</dependency>
100+
101+
<dependency>
102+
<groupId>org.projectlombok</groupId>
103+
<artifactId>lombok</artifactId>
104+
<optional>true</optional>
105+
</dependency>
106+
95107
<dependency>
96108
<groupId>commons-io</groupId>
97109
<artifactId>commons-io</artifactId>
@@ -129,6 +141,32 @@
129141
<target>${java.version}</target>
130142
</configuration>
131143
</plugin>
144+
<plugin>
145+
<groupId>org.apache.maven.plugins</groupId>
146+
<artifactId>maven-javadoc-plugin</artifactId>
147+
<version>${maven-javadoc-plugin.version}</version>
148+
<executions>
149+
<execution>
150+
<id>attach-javadocs</id>
151+
<goals>
152+
<goal>jar</goal>
153+
</goals>
154+
</execution>
155+
</executions>
156+
</plugin>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-source-plugin</artifactId>
160+
<version>${maven-source-plugin.version}</version>
161+
<executions>
162+
<execution>
163+
<id>attach-sources</id>
164+
<goals>
165+
<goal>jar-no-fork</goal>
166+
</goals>
167+
</execution>
168+
</executions>
169+
</plugin>
132170
</plugins>
133171

134172
</build>
@@ -137,34 +175,6 @@
137175
<id>ossrh</id>
138176
<build>
139177
<plugins>
140-
<plugin>
141-
<groupId>org.apache.maven.plugins</groupId>
142-
<artifactId>maven-source-plugin</artifactId>
143-
<version>${maven-source-plugin.version}</version>
144-
<executions>
145-
<execution>
146-
<id>attach-sources</id>
147-
<goals>
148-
<goal>jar-no-fork</goal>
149-
</goals>
150-
</execution>
151-
</executions>
152-
</plugin>
153-
154-
<plugin>
155-
<groupId>org.apache.maven.plugins</groupId>
156-
<artifactId>maven-javadoc-plugin</artifactId>
157-
<version>${maven-javadoc-plugin.version}</version>
158-
<executions>
159-
<execution>
160-
<id>attach-javadocs</id>
161-
<goals>
162-
<goal>jar</goal>
163-
</goals>
164-
</execution>
165-
</executions>
166-
</plugin>
167-
168178
<plugin>
169179
<groupId>org.apache.maven.plugins</groupId>
170180
<artifactId>maven-gpg-plugin</artifactId>

src/main/java/ngrok/NgrokComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import java.lang.annotation.Retention;
77
import java.lang.annotation.RetentionPolicy;
88

9-
import static ngrok.NgrokProperties.NGROK_ENABLED;
9+
import static ngrok.configuration.NgrokConfiguration.NGROK_ENABLED;
1010

1111
@ConditionalOnProperty(name = NGROK_ENABLED, havingValue = "true")
1212
@Component
1313
@Retention(RetentionPolicy.RUNTIME)
1414
public @interface NgrokComponent {
15-
}
15+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package ngrok;
22

3+
import lombok.Getter;
34
import ngrok.api.model.NgrokTunnel;
45
import org.springframework.context.ApplicationEvent;
56

67
import java.util.List;
78

9+
@Getter
810
public class NgrokInitializedEvent extends ApplicationEvent {
911

1012
private final List<NgrokTunnel> tunnels;
@@ -13,7 +15,5 @@ public NgrokInitializedEvent(Object source, List<NgrokTunnel> tunnels) {
1315
super(source);
1416
this.tunnels = tunnels;
1517
}
16-
public List<NgrokTunnel> getTunnels() {
17-
return tunnels;
18-
}
18+
1919
}

src/main/java/ngrok/NgrokProperties.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/main/java/ngrok/NgrokRunner.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package ngrok;
22

3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
35
import ngrok.api.NgrokApiClient;
46
import ngrok.api.model.NgrokTunnel;
7+
import ngrok.configuration.NgrokConfiguration;
58
import ngrok.configuration.NgrokConfigurationProvider;
69
import ngrok.exception.NgrokCommandExecuteException;
710
import ngrok.exception.NgrokDownloadException;
@@ -10,10 +13,6 @@
1013
import ngrok.os.NgrokSystemCommandExecutor;
1114
import ngrok.util.NgrokDownloader;
1215
import org.apache.commons.lang3.StringUtils;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
import org.springframework.beans.factory.annotation.Autowired;
16-
import org.springframework.beans.factory.annotation.Value;
1716
import org.springframework.boot.web.context.WebServerInitializedEvent;
1817
import org.springframework.context.ApplicationEventPublisher;
1918
import org.springframework.context.event.EventListener;
@@ -24,16 +23,13 @@
2423
/**
2524
* For details see <a href="https://github.com/kilmajster/ngrok-spring-boot-starter">docs</a>.
2625
*/
26+
@Slf4j
27+
@RequiredArgsConstructor
2728
public class NgrokRunner {
2829

29-
private static final Logger log = LoggerFactory.getLogger(NgrokRunner.class);
30-
31-
@Value("${" + NgrokProperties.NGROK_COMMAND + ":}")
32-
private String ngrokCustomCommand;
33-
34-
@Autowired
35-
private ApplicationEventPublisher applicationEventPublisher;
30+
private final ApplicationEventPublisher applicationEventPublisher;
3631

32+
private final NgrokConfiguration ngrokConfiguration;
3733
private final NgrokApiClient ngrokApiClient;
3834
private final NgrokBinaryProvider ngrokBinaryProvider;
3935
private final NgrokConfigurationProvider ngrokConfigurationProvider;
@@ -42,20 +38,6 @@ public class NgrokRunner {
4238
private final NgrokSystemCommandExecutor ngrokSystemCommandExecutor;
4339
private final TaskExecutor ngrokExecutor;
4440

45-
public NgrokRunner(
46-
NgrokApiClient ngrokApiClient, NgrokBinaryProvider ngrokBinaryProvider,
47-
NgrokConfigurationProvider ngrokConfigurationProvider, NgrokDownloader ngrokDownloader,
48-
NgrokPlatformDetector ngrokPlatformDetector, NgrokSystemCommandExecutor ngrokSystemCommandExecutor,
49-
TaskExecutor ngrokExecutor) {
50-
this.ngrokApiClient = ngrokApiClient;
51-
this.ngrokBinaryProvider = ngrokBinaryProvider;
52-
this.ngrokConfigurationProvider = ngrokConfigurationProvider;
53-
this.ngrokDownloader = ngrokDownloader;
54-
this.ngrokPlatformDetector = ngrokPlatformDetector;
55-
this.ngrokSystemCommandExecutor = ngrokSystemCommandExecutor;
56-
this.ngrokExecutor = ngrokExecutor;
57-
}
58-
5941
@EventListener
6042
public void run(WebServerInitializedEvent event) throws NgrokDownloadException, NgrokCommandExecuteException {
6143
ngrokExecutor.execute(() -> {
@@ -117,16 +99,16 @@ private String buildNgrokDefaultShellCmd(int springServerPort) {
11799
}
118100

119101
private String buildCustomShellCmd() {
120-
return ngrokBinaryProvider.getNgrokBinaryFilePath() + " " + ngrokCustomCommand;
102+
return ngrokBinaryProvider.getNgrokBinaryFilePath() + " " + ngrokConfiguration.getCommand();
121103
}
122104

123105
private boolean isCustomConfigPresent() {
124-
return StringUtils.isNotBlank(ngrokCustomCommand);
106+
return StringUtils.isNotBlank(ngrokConfiguration.getCommand());
125107
}
126108

127109
private void logTunnelsDetails() {
128110
List<NgrokTunnel> tunnels = ngrokApiClient.fetchTunnels();
129111

130112
tunnels.forEach(t -> log.info("Remote url ({})\t-> [ {} ]", t.getProto(), t.getPublicUrl()));
131113
}
132-
}
114+
}

src/main/java/ngrok/api/NgrokApiClient.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
package ngrok.api;
22

3+
import lombok.Getter;
4+
import lombok.extern.slf4j.Slf4j;
35
import ngrok.NgrokComponent;
4-
import ngrok.NgrokProperties;
56
import ngrok.api.model.NgrokTunnel;
67
import ngrok.api.model.NgrokTunnelsList;
7-
import org.slf4j.Logger;
8-
import org.slf4j.LoggerFactory;
9-
import org.springframework.beans.factory.annotation.Value;
8+
import ngrok.configuration.NgrokConfiguration;
109
import org.springframework.http.ResponseEntity;
1110
import org.springframework.web.client.RestClientException;
1211
import org.springframework.web.client.RestTemplate;
1312

1413
import java.util.Collections;
1514
import java.util.List;
1615

16+
@Slf4j
1717
@NgrokComponent
1818
public class NgrokApiClient {
1919

2020
public static final String NGROK_URL_API_TUNNELS = "/api/tunnels";
2121
public static final String NGROK_URL_HTML_STATUS = "/status";
2222

23-
private static final Logger log = LoggerFactory.getLogger(NgrokApiClient.class);
24-
25-
private final RestTemplate restTemplate = new RestTemplate();
23+
private final RestTemplate restTemplate;
2624

25+
@Getter
2726
private final String ngrokApiUrl;
2827

29-
public NgrokApiClient(
30-
@Value("${" + NgrokProperties.NGROK_HOST + ":" + NgrokProperties.NGROK_HOST_DEFAULT + "}") String ngrokApiHost,
31-
@Value("${" + NgrokProperties.NGROK_PORT + ":" + NgrokProperties.NGROK_PORT_DEFAULT + "}") Integer ngrokApiPort) {
32-
this.ngrokApiUrl = ngrokApiHost + ":" + ngrokApiPort;
28+
public NgrokApiClient(NgrokConfiguration ngrokConfiguration) {
29+
this.restTemplate = new RestTemplate();
30+
this.ngrokApiUrl = ngrokConfiguration.getHost() + ":" + ngrokConfiguration.getPort();
3331
}
3432

3533
public List<NgrokTunnel> fetchTunnels() {
@@ -55,10 +53,6 @@ public boolean isResponding() {
5553
return false;
5654
}
5755

58-
public String getNgrokApiUrl() {
59-
return ngrokApiUrl;
60-
}
61-
6256
public String getNgrokStatusUrl() {
6357
return ngrokApiUrl + NGROK_URL_HTML_STATUS;
6458
}
@@ -97,4 +91,4 @@ public String getHttpsTunnelUrl() {
9791
public boolean isRunning() {
9892
return isResponding();
9993
}
100-
}
94+
}

0 commit comments

Comments
 (0)