File History: src/main/java/com/paymentlink/service/NodeIdService.java

← View file content

File Content at Commit f0438c2

1 package com.paymentlink.service;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.beans.factory.annotation.Value;
6 import org.springframework.stereotype.Service;
7
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11 import java.nio.file.Paths;
12 import java.security.SecureRandom;
13
14 @Service
15 public class NodeIdService {
16
17 private static final Logger logger = LoggerFactory.getLogger(NodeIdService.class);
18 private static final SecureRandom RANDOM = new SecureRandom();
19 private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789";
20
21 @Value("${app.server-id-file:incl/server-id.txt}")
22 private String serverIdFile;
23
24 @Value("${app.tld:ironpath.ai}")
25 private String tld;
26
27 private String nodeId;
28
29 /**
30 * Get or create node ID
31 */
32 public String getNodeId() {
33 if (nodeId == null) {
34 nodeId = loadOrCreateNodeId();
35 }
36 return nodeId;
37 }
38
39 /**
40 * Get short ID (without TLD)
41 */
42 public String getShortId() {
43 String fullId = getNodeId();
44 return fullId.split("\\.")[0];
45 }
46
47 private String loadOrCreateNodeId() {
48 try {
49 Path filePath = Paths.get(serverIdFile);
50
51 // Ensure parent directory exists
52 if (filePath.getParent() != null) {
53 Files.createDirectories(filePath.getParent());
54 }
55
56 // Try to read existing ID
57 if (Files.exists(filePath)) {
58 String id = Files.readString(filePath).trim();
59 if (!id.isEmpty()) {
60 logger.info("Loaded existing server ID: {}", id);
61 return id;
62 }
63 }
64
65 // Generate new ID
66 String newId = generateNodeId();
67 Files.writeString(filePath, newId);
68 logger.info("Generated new server ID: {}", newId);
69 return newId;
70
71 } catch (IOException e) {
72 logger.error("Failed to load/create server ID file", e);
73 // Generate temporary ID
74 String tempId = generateNodeId();
75 logger.warn("Using temporary server ID: {}", tempId);
76 return tempId;
77 }
78 }
79
80 private String generateNodeId() {
81 StringBuilder sb = new StringBuilder(6);
82 for (int i = 0; i < 6; i++) {
83 sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
84 }
85 return sb.toString() + "." + tld;
86 }
87 }
88
89

Commits

Commit Author Date Message File SHA Actions
f0438c2 <f69e50@finnacloud.com> 1766443042 +0300 12/22/2025, 10:37:22 PM increment once more 3a3bdd4 Hide
188fc92 <f69e50@finnacloud.com> 1766442998 +0300 12/22/2025, 10:36:38 PM increment 3a3bdd4 View
4617f76 <f69e50@finnacloud.com> 1766442953 +0300 12/22/2025, 10:35:53 PM rename branch from main to master oops 3a3bdd4 View
e6d1548 <f69e50@finnacloud.com> 1766442769 +0300 12/22/2025, 10:32:49 PM add initial test workflow file 3a3bdd4 View
9c24ca4 <f69e50@finnacloud.com> 1766442705 +0300 12/22/2025, 10:31:45 PM add CI configuration and test script for Jenkins build 3a3bdd4 View
690c1f6 <f69e50@finnacloud.com> 1766368110 +0300 12/22/2025, 1:48:30 AM initialize backend structure with controllers, DTOs, and configuration files 3a3bdd4 View