source: backend/src/main/java/com/finki/icare/config/DotenvConfig.java

main
Last change on this file was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[700e2f9]1package com.finki.icare.config;
2
3import io.github.cdimascio.dotenv.Dotenv;
4import org.springframework.context.ApplicationContextInitializer;
5import org.springframework.context.ConfigurableApplicationContext;
6import org.springframework.core.env.ConfigurableEnvironment;
7import org.springframework.core.env.MapPropertySource;
8
9import java.util.HashMap;
10import java.util.Map;
11
12public class DotenvConfig implements ApplicationContextInitializer<ConfigurableApplicationContext> {
13
14 @Override
15 public void initialize(ConfigurableApplicationContext applicationContext) {
16 Dotenv dotenv;
17
18 try {
19 // Configuration needed for IntelliJ, since it runs the app from the project root
20 dotenv = Dotenv.configure()
21 .directory("./backend")
22 .load();
23 } catch (Exception e) {
24 dotenv = Dotenv.configure()
25 .directory(".")
26 .load();
27 }
28
29 ConfigurableEnvironment environment = applicationContext.getEnvironment();
30 Map<String, Object> dotenvProperties = new HashMap<>();
31
32 dotenv.entries().forEach(entry -> {
33 dotenvProperties.put(entry.getKey(), entry.getValue());
34 System.setProperty(entry.getKey(), entry.getValue());
35 });
36
37 environment.getPropertySources()
38 .addFirst(new MapPropertySource("dotenvProperties", dotenvProperties));
39 }
40}
Note: See TracBrowser for help on using the repository browser.