mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 13:40:50 +02:00
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:
parent
c452198ee9
commit
40fe825c09
12 changed files with 128 additions and 89 deletions
|
@ -2,8 +2,8 @@
|
||||||
Course Exercice : Application to help others
|
Course Exercice : Application to help others
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
./init.sh
|
||||||
mvn compile
|
mvn compile
|
||||||
|
|
||||||
mvn spring-boot:run
|
mvn spring-boot:run
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
1
helpapp-backend/.gitignore
vendored
Normal file
1
helpapp-backend/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*/src/main/resources/application.properties
|
|
@ -1 +0,0 @@
|
||||||
server.port=8080
|
|
|
@ -1 +0,0 @@
|
||||||
server.port=8081
|
|
|
@ -1 +0,0 @@
|
||||||
server.port=8082
|
|
|
@ -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 +0,0 @@
|
||||||
server.port=8083
|
|
|
@ -1 +0,0 @@
|
||||||
server.port=8084
|
|
1
helpapp-frontend/.gitignore
vendored
Normal file
1
helpapp-frontend/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
generated.js
|
|
@ -6,6 +6,7 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<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="https://unpkg.com/htmx.org@1.9.12"></script> <!-- Include HTMX -->
|
||||||
<script src="base.js"></script> <!-- Include BASE -->
|
<script src="base.js"></script> <!-- Include BASE -->
|
||||||
|
<script src="generated.js"></script> <!-- Include GENERATED -->
|
||||||
<title>HelpApp</title>
|
<title>HelpApp</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
@ -48,81 +49,7 @@
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<script>
|
<script src="index.js"></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>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
73
helpapp-frontend/index.js
Normal file
73
helpapp-frontend/index.js
Normal 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
51
init.sh
|
@ -2,6 +2,20 @@
|
||||||
|
|
||||||
source ./db/db.sh
|
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
|
check_env
|
||||||
echo "> Creating database 'service-architecture'..."
|
echo "> Creating database 'service-architecture'..."
|
||||||
if ! mariadb -u "$user" -p"$pwd" -e "CREATE DATABASE IF NOT EXISTS \`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
|
echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e $GREEN"Database setup successful!"$RESET
|
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 !"
|
||||||
|
|
Loading…
Add table
Reference in a new issue