Skip to content

Commit f5b81ba

Browse files
authored
Merge branch 'feature/wan-impl' into main
2 parents f4df503 + 4fb0945 commit f5b81ba

File tree

13 files changed

+191
-117
lines changed

13 files changed

+191
-117
lines changed

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

Lines changed: 53 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,51 @@ 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+
*/
162+
public void wanImplement(String urlServ) {
163+
try {
164+
String codeWan = "";
165+
166+
HttpClient client1 = HttpClient.newHttpClient();
167+
HttpRequest request1 = HttpRequest.newBuilder()
168+
.uri(URI.create(urlServ)) // remplacer par l'URL qui
169+
.build();
170+
171+
HttpResponse<String> response1 = client1.send(request1, HttpResponse.BodyHandlers.ofString());
172+
173+
Gson gson1 = new Gson();
174+
JsonObject root1 = gson1.fromJson(response1.body(), JsonObject.class);
175+
176+
if (root1 != null) {
177+
codeWan = root1.get("code").getAsString();
178+
}
179+
180+
HttpClient client = HttpClient.newHttpClient();
181+
HttpRequest request = HttpRequest.newBuilder()
182+
.uri(URI.create(urlServ + "/connect/" + codeWan)) // remplacer par l'URL qui renvoie le JSON
183+
.build();
184+
185+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
186+
187+
Gson gson = new Gson();
188+
JsonObject root = gson.fromJson(response.body(), JsonObject.class);
189+
190+
if (root != null && root.has("user")) {
191+
JsonObject user = root.getAsJsonObject("user");
192+
if (user != null && user.has("port") && !user.get("port").isJsonNull()) {
193+
StoreAndRetrieve().put(StoreAndRetrieve().DYNAMIC_PORT, user.get("port").getAsString());
194+
}
195+
}
196+
197+
SpringApp().startApplication().subscribe();
198+
} catch (Exception e) {
199+
e.printStackTrace();
200+
}
201+
}
202+
150203
/**
151204
* Check if the ConnectLib is initialized.
152205
* @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: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.Banner;
55
import org.springframework.boot.SpringApplication;
66
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.web.reactive.config.WebFluxConfigurer;
78
import reactor.core.publisher.ConnectableFlux;
89
import reactor.core.publisher.Mono;
910
import reactor.core.scheduler.Schedulers;
@@ -16,13 +17,40 @@
1617
*/
1718

1819
@SpringBootApplication
19-
public class Application {
20+
public class Application implements WebFluxConfigurer {
2021

2122
// Singleton instance
2223
private static Application instance;
2324

25+
/**
26+
* ConnectLib instance for managing connections.
27+
*/
2428
private static final ConnectLib connectLib = new ConnectLib();
2529

30+
/**
31+
* Determines the port to use for the server, checking for a dynamic port first.
32+
* @return the port number as a string
33+
*/
34+
private String Dyn_Port() {
35+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT) != null) {
36+
return connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString();
37+
} else {
38+
return connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().PORT).toString();
39+
}
40+
}
41+
42+
/**
43+
* Checks if the application is using a static port.
44+
* @return true if using a static port, false if using a dynamic port
45+
*/
46+
private boolean isStatic() {
47+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT) != null) {
48+
return false;
49+
} else {
50+
return true;
51+
}
52+
}
53+
2654
/**
2755
* Starts the Spring Boot application in a non-blocking manner.
2856
* @return a Mono that completes when the application has started
@@ -33,10 +61,12 @@ public Mono<Void> startApplication() {
3361
app.setBannerMode(Banner.Mode.OFF); // supprime le banner Spring
3462
app.setLogStartupInfo(false); // désactive l'info de démarrage
3563
Map<String, Object> props = new HashMap<>();
36-
props.put("server.port", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().PORT)); // définit le port du serveur
37-
props.put("logging.level.root", "OFF"); // coupe l'affichage des logs
64+
props.put("server.port", Dyn_Port()); // définit le port du serveur
65+
props.put("logging.level.root", "OFF");
3866
app.setDefaultProperties(props);
3967
app.run();
68+
69+
System.out.println(Dyn_Port());
4070
}).subscribeOn(Schedulers.boundedElastic()).then();
4171
}
4272

src/main/java/fr/sandro642/github/spring/controller/DataController.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import fr.sandro642.github.ConnectLib;
44
import fr.sandro642.github.spring.dto.Request;
5-
import fr.sandro642.github.spring.dto.RouteInfo;
65
import org.springframework.web.bind.annotation.*;
76

87
import java.util.*;
@@ -58,34 +57,47 @@ public static DataController getInstance() {
5857
@GetMapping("/status")
5958
public Map<String, Object> getStatus() {
6059
Map<String, Object> status = new HashMap<>();
60+
status.put("portInfo", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT));
6161
status.put("isInitialized", connectLib.isInitialized());
6262
status.put("resourceType", connectLib.HookManager().getResourceType());
6363
return status;
6464
}
6565

66-
/**
67-
* Endpoint to get the available routes from ConnectLib.
68-
* @return a list of RouteInfo objects representing the routes
69-
*/
7066
@GetMapping("/routes")
71-
public List<RouteInfo> getRoutes() {
67+
public Map<String, Object> getRoutes() {
7268
Map<String, String> map = connectLib.getRoutesMap();
73-
List<RouteInfo> result = new ArrayList<>();
69+
Map<String, Object> result = new HashMap<>();
70+
71+
// portInfo
72+
result.put("portInfo", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString());
73+
74+
List<Map<String, String>> routes = new ArrayList<>();
7475
if (map != null) {
7576
for (Map.Entry<String, String> e : map.entrySet()) {
76-
result.add(new RouteInfo(e.getKey(), e.getValue()));
77+
Map<String, String> route = new HashMap<>();
78+
route.put("name", e.getKey());
79+
route.put("url", e.getValue());
80+
routes.add(route);
7781
}
7882
}
83+
result.put("routes", routes);
7984
return result;
8085
}
8186

87+
8288
/**
8389
* Endpoint to get all requests.
8490
* @return a list of Request objects
8591
*/
8692
@GetMapping("/requests")
87-
public List<Request> getRequests() {
88-
List<Request> result = new ArrayList<>(requestsMap.values());
93+
public Map<String, Object> getRequests() {
94+
Map<String, Object> result = new HashMap<>();
95+
96+
result.put("portInfo", connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString());
97+
98+
List<Request> requests = new ArrayList<>(requestsMap.values());
99+
result.put("requests", requests);
100+
89101
return result;
90102
}
91103

@@ -108,7 +120,7 @@ public Request createRequest(String route, String branch) {
108120
}
109121
long id = requestIdCounter.incrementAndGet();
110122

111-
Request req = new Request(id, route, branch, "pending");
123+
Request req = new Request(connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().DYNAMIC_PORT).toString(), id, route, branch, "pending");
112124
requestsMap.put(id, req);
113125
return req;
114126
}

src/main/java/fr/sandro642/github/spring/dto/Request.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
@JsonIgnoreProperties(ignoreUnknown = true)
1010
public class Request {
1111

12+
/**
13+
* Port information of the request.
14+
*/
15+
private String infoPort;
16+
1217
/**
1318
* Unique identifier for the request
1419
*/
@@ -36,12 +41,14 @@ public Request() {}
3641

3742
/**
3843
* Parameterized constructor
44+
* @param infoPort Port information of the request
3945
* @param id Unique identifier for the request
4046
* @param route Route associated with the request
4147
* @param branch Branch associated with the request
4248
* @param status Status of the request
4349
*/
44-
public Request(Long id, String route, String branch, String status) {
50+
public Request(String infoPort, Long id, String route, String branch, String status) {
51+
this.infoPort = infoPort;
4552
this.id = id;
4653
this.route = route;
4754
this.branch = branch;

src/main/java/fr/sandro642/github/spring/dto/RouteInfo.java

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

0 commit comments

Comments
 (0)