init.sh: Now generating each application.properties depending on the .env. Each port is now a constant in generated.js

This commit is contained in:
Yohan Boujon 2024-12-21 17:02:38 +01:00
parent c452198ee9
commit 40fe825c09
12 changed files with 128 additions and 89 deletions

View file

@ -2,8 +2,8 @@
Course Exercice : Application to help others
```bash
./init.sh
mvn compile
mvn spring-boot:run
```

1
helpapp-backend/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*/src/main/resources/application.properties

View file

@ -1,7 +0,0 @@
server.port=8089
spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture
spring.datasource.username=yoboujon
spring.datasource.password=25158114
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

1
helpapp-frontend/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
generated.js

View file

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/htmx.org@1.9.12"></script> <!-- Include HTMX -->
<script src="base.js"></script> <!-- Include BASE -->
<script src="generated.js"></script> <!-- Include GENERATED -->
<title>HelpApp</title>
</head>
@ -48,81 +49,7 @@
</section>
</main>
<script>
const API_BASE = "http://localhost";
// Append the user-registration dropdown for each role from the database
function roleDefine() {
const select = document.getElementById("role");
fetchData(`${API_BASE}:8089/get_roles`).then(data => {
data.forEach(element => {
const option = createOption(element.id, element.name);
select.appendChild(option);
});
});
}
document.addEventListener("DOMContentLoaded", () => {
roleDefine();
const loginForm = document.getElementById("login-form");
const loginResponse = document.getElementById("login-response");
const registerForm = document.getElementById("register-form");
const registerResponse = document.getElementById("register-response");
const userRegistrationSection = document.getElementById("user-registration");
// Check if user is logged in
if (localStorage.getItem("userId")) {
userRegistrationSection.style.display = "none";
}
// User Login
loginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const email = document.getElementById("login-email").value;
const password = document.getElementById("login-password").value;
try {
const response = await fetch(`${API_BASE}:8083/users/authenticate`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
});
const result = await response.json();
if (result.success) {
localStorage.setItem("userId", result.userId);
loginResponse.textContent = `Connexion réussie : ID ${result.userId}`;
userRegistrationSection.style.display = "none";
} else {
loginResponse.textContent = `Échec de la connexion : ${result.message}`;
}
} catch (error) {
loginResponse.textContent = "Erreur lors de la connexion.";
}
});
// User Registration
registerForm.addEventListener("submit", async (e) => {
e.preventDefault();
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const role = document.getElementById("role").value;
try {
const response = await fetch(`${API_BASE}:8083/users`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, email, password, role }),
});
const result = await response.json();
registerResponse.textContent = `Utilisateur créé : ID ${result.id}`;
} catch (error) {
registerResponse.textContent = "Erreur lors de la création de l'utilisateur.";
}
});
});
</script>
<script src="index.js"></script>
</body>
</html>

73
helpapp-frontend/index.js Normal file
View file

@ -0,0 +1,73 @@
const API_BASE = "http://localhost";
// Append the user-registration dropdown for each role from the database
function roleDefine() {
const select = document.getElementById("role");
fetchData(`${API_BASE}:${ROLE_PORT}/get_roles`).then(data => {
data.forEach(element => {
const option = createOption(element.id, element.name);
select.appendChild(option);
});
});
}
document.addEventListener("DOMContentLoaded", () => {
roleDefine();
const loginForm = document.getElementById("login-form");
const loginResponse = document.getElementById("login-response");
const registerForm = document.getElementById("register-form");
const registerResponse = document.getElementById("register-response");
const userRegistrationSection = document.getElementById("user-registration");
// Check if user is logged in
if (localStorage.getItem("userId")) {
userRegistrationSection.style.display = "none";
}
// User Login
loginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const email = document.getElementById("login-email").value;
const password = document.getElementById("login-password").value;
try {
const response = await fetch(`${API_BASE}:8083/users/authenticate`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
});
const result = await response.json();
if (result.success) {
localStorage.setItem("userId", result.userId);
loginResponse.textContent = `Connexion réussie : ID ${result.userId}`;
userRegistrationSection.style.display = "none";
} else {
loginResponse.textContent = `Échec de la connexion : ${result.message}`;
}
} catch (error) {
loginResponse.textContent = "Erreur lors de la connexion.";
}
});
// User Registration
registerForm.addEventListener("submit", async (e) => {
e.preventDefault();
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const role = document.getElementById("role").value;
try {
const response = await fetch(`${API_BASE}:8083/users`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, email, password, role }),
});
const result = await response.json();
registerResponse.textContent = `Utilisateur créé : ID ${result.id}`;
} catch (error) {
registerResponse.textContent = "Erreur lors de la création de l'utilisateur.";
}
});
});

51
init.sh
View file

@ -2,6 +2,20 @@
source ./db/db.sh
generate_properties()
{
url="$1/src/main/resources/application.properties"
echo -e "server.port=$port
spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture
spring.datasource.username=$user
spring.datasource.password=$pwd
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect" > "$url"
((port++))
}
## CREATING DATABASE
check_env
echo "> Creating database 'service-architecture'..."
if ! mariadb -u "$user" -p"$pwd" -e "CREATE DATABASE IF NOT EXISTS \`service-architecture\`;"
@ -23,5 +37,40 @@ then
echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET
exit 1
fi
echo -e $GREEN"Database setup successful!"$RESET
## CREATING application.properties
services=()
port=8081
echo "> Checking Microservices..."
for dir in helpapp-backend/*-service; do
if [[ -d "$dir" ]]; then
services+=("$dir")
fi
done
if [[ ${#services[@]} -gt 0 ]]; then
echo -e $GREEN"Found ${#services[@]} Microservices!"$RESET
else
echo -e $RED"Error: Found no microservices:\nAre you in the right directory ?"$RESET
exit 1
fi
count=0
for service in "${services[@]}"; do
((count++))
echo -ne "\r> Generating 'application.properties'... [$count/${#services[@]}]"
generate_properties "$service"
done
port=8081
generated_js="/* !! THIS IS A GENERATED FILE, DO NOT MODIFY !! */\n\n"
echo -e "\n> Generating 'generated.js'..."
for service in "${services[@]}"; do
service_name=$(basename "$service") # Remove the last '/'
modified_name="${service_name%-service}" # Remove the "-service"
modified_name="${modified_name^^}_PORT" # Uppercase and add '_PORT'
generated_js+="const $modified_name=$port\n"
((port++))
done
echo -e "$generated_js" > helpapp-frontend/generated.js
echo -e $GREEN"Setup successfull for each microservice !"