Skip to content

Commit 202a624

Browse files
committed
Implement WAN connection to retrieve dynamic port; refactor server port configuration
1 parent 9f8403f commit 202a624

9 files changed

Lines changed: 74 additions & 21 deletions

File tree

src/main/java/fr/sandro642/github/ConnectLib.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package fr.sandro642.github;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
35
import fr.sandro642.github.enums.LangType;
46
import fr.sandro642.github.enums.lang.CategoriesType;
57
import fr.sandro642.github.hook.HookManager;
68
import fr.sandro642.github.hook.LangSupport;
79
import fr.sandro642.github.hook.MCSupport;
10+
import fr.sandro642.github.log.Logger;
11+
import fr.sandro642.github.log.Logs;
812
import fr.sandro642.github.misc.*;
913
import fr.sandro642.github.jobs.JobGetInfos;
1014
import fr.sandro642.github.enums.ResourceType;
1115
import fr.sandro642.github.spring.Application;
1216
import fr.sandro642.github.update.RetrieveLastVersion;
1317

18+
import java.net.URI;
19+
import java.net.http.HttpClient;
20+
import java.net.http.HttpRequest;
21+
import java.net.http.HttpResponse;
1422
import java.util.*;
1523

1624
/**
@@ -147,6 +155,53 @@ public void webServices(int port, String nameDashboard) {
147155
SpringApp().startApplication().subscribe();
148156
}
149157

158+
/**
159+
* Implement WAN connection to retrieve the port from the server.
160+
* @param urlServ the server URL
161+
* @param codeWan the WAN code
162+
* @return the port as a String
163+
*/
164+
public void wanImplement(String urlServ) {
165+
try {
166+
String codeWan = "";
167+
168+
HttpClient client1 = HttpClient.newHttpClient();
169+
HttpRequest request1 = HttpRequest.newBuilder()
170+
.uri(URI.create(urlServ)) // remplacer par l'URL qui
171+
.build();
172+
173+
HttpResponse<String> response1 = client1.send(request1, HttpResponse.BodyHandlers.ofString());
174+
175+
Gson gson1 = new Gson();
176+
JsonObject root1 = gson1.fromJson(response1.body(), JsonObject.class);
177+
178+
if (root1 != null) {
179+
codeWan = root1.get("code").getAsString();
180+
}
181+
182+
HttpClient client = HttpClient.newHttpClient();
183+
HttpRequest request = HttpRequest.newBuilder()
184+
.uri(URI.create(urlServ + "/connect/" + codeWan)) // remplacer par l'URL qui renvoie le JSON
185+
.build();
186+
187+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
188+
189+
Gson gson = new Gson();
190+
JsonObject root = gson.fromJson(response.body(), JsonObject.class);
191+
192+
if (root != null && root.has("user")) {
193+
JsonObject user = root.getAsJsonObject("user");
194+
if (user != null && user.has("port") && !user.get("port").isJsonNull()) {
195+
StoreAndRetrieve().put(StoreAndRetrieve().DYNAMIC_PORT, user.get("port").getAsString());
196+
}
197+
}
198+
199+
SpringApp().startApplication().subscribe();
200+
} catch (Exception e) {
201+
e.printStackTrace();
202+
}
203+
}
204+
150205
/**
151206
* Check if the ConnectLib is initialized.
152207
* @return true if initialized, false otherwise

src/main/java/fr/sandro642/github/hook/HookManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import fr.sandro642.github.ConnectLib;
44
import fr.sandro642.github.enums.ResourceType;
55
import fr.sandro642.github.enums.lang.CategoriesType;
6-
import fr.sandro642.github.misc.Logger;
76

87
/**
98
* HookManager is a class that manages hooks for different resource types.

src/main/java/fr/sandro642/github/misc/Logger.java renamed to src/main/java/fr/sandro642/github/log/Logger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fr.sandro642.github.misc;
1+
package fr.sandro642.github.log;
22

33
/**
44
* Logger is a utility class for logging messages in the ConnectLib library.

src/main/java/fr/sandro642/github/misc/Logs.java renamed to src/main/java/fr/sandro642/github/log/Logs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fr.sandro642.github.misc;
1+
package fr.sandro642.github.log;
22

33
import java.io.File;
44
import java.io.FileWriter;

src/main/java/fr/sandro642/github/misc/EnumLoader.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* This class provides a method to convert an enum class into a map where the keys are the enum constants and the values are their corresponding routes.
1111
* @author Sandro642
1212
* @version 1.0
13-
* @since 1.0
1413
*/
1514

1615
public class EnumLoader {

src/main/java/fr/sandro642/github/misc/StoreAndRetrieve.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class StoreAndRetrieve {
2323

2424
public final String NAME_DASHBOARD = "nameDashboard";
2525
public final String PORT = String.valueOf(3000);
26+
public final String DYNAMIC_PORT = "dynamicPort";
2627
public final HashMap<String, Object> store = new HashMap<>();
2728

2829
/**

src/main/java/fr/sandro642/github/spring/Application.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public class Application {
2323

2424
private static final ConnectLib connectLib = new ConnectLib();
2525

26+
private String Dyn_Port() {
27+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT) != null) {
28+
return connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString();
29+
} else {
30+
return connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().PORT).toString();
31+
}
32+
}
33+
2634
/**
2735
* Starts the Spring Boot application in a non-blocking manner.
2836
* @return a Mono that completes when the application has started
@@ -33,10 +41,12 @@ public Mono<Void> startApplication() {
3341
app.setBannerMode(Banner.Mode.OFF); // supprime le banner Spring
3442
app.setLogStartupInfo(false); // désactive l'info de démarrage
3543
Map<String, Object> props = new HashMap<>();
36-
props.put("server.port", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().PORT)); // définit le port du serveur
44+
props.put("server.port", Dyn_Port()); // définit le port du serveur
3745
props.put("logging.level.root", "OFF"); // coupe l'affichage des logs
3846
app.setDefaultProperties(props);
3947
app.run();
48+
49+
System.out.println(Dyn_Port());
4050
}).subscribeOn(Schedulers.boundedElastic()).then();
4151
}
4252

src/main/java/fr/sandro642/github/update/RetrieveLastVersion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public String fetchVersion() {
4040
JsonArray data = gson.fromJson(response.body(), JsonArray.class);
4141

4242
if (data != null && !data.isEmpty()) {
43-
JsonObject firstTag = data.get(0).getAsJsonObject();
44-
return firstTag.get("name").getAsString();
43+
JsonObject object = data.get(0).getAsJsonObject();
44+
return object.get("name").getAsString();
4545
} else {
4646
return "No tags found";
4747
}

src/test/java/fr/sandro642/github/test/MainTest.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,7 @@ public void startAppServices() {
187187
try {
188188
connectLib.Logger().showLogs();
189189
connectLib.init(ResourceType.TEST_RESOURCES, LangType.ENGLISH, TestRoutes.class)
190-
.webServices(8080, "TestDashboard");
191-
192-
CompletableFuture<ApiFactory> apiFactoryCompletableFuture = connectLib.JobGetInfos()
193-
.getRoutes(MethodType.GET, TestRoutes.HELLO)
194-
.getResponse();
195-
196-
apiFactoryCompletableFuture = connectLib.JobGetInfos()
197-
.getRoutes(TestCustomVersion.V1_API ,MethodType.GET, TestRoutes.REQUEST_TOKEN)
198-
.urlBranch(ExampleUrlBranch.PROD)
199-
.getResponse();
200-
201-
ApiFactory apiFactory = apiFactoryCompletableFuture.get(5, TimeUnit.SECONDS);
202-
203-
System.out.println("Response: " + apiFactory.display());
190+
.wanImplement("http://localhost:8080");
204191

205192
Thread.sleep(20000);
206193

@@ -210,4 +197,6 @@ public void startAppServices() {
210197
}
211198

212199

200+
201+
213202
}

0 commit comments

Comments
 (0)