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
|
| Rev | Line | |
|---|
| [700e2f9] | 1 | package com.finki.icare.config;
|
|---|
| 2 |
|
|---|
| 3 | import io.github.cdimascio.dotenv.Dotenv;
|
|---|
| 4 | import org.springframework.context.ApplicationContextInitializer;
|
|---|
| 5 | import org.springframework.context.ConfigurableApplicationContext;
|
|---|
| 6 | import org.springframework.core.env.ConfigurableEnvironment;
|
|---|
| 7 | import org.springframework.core.env.MapPropertySource;
|
|---|
| 8 |
|
|---|
| 9 | import java.util.HashMap;
|
|---|
| 10 | import java.util.Map;
|
|---|
| 11 |
|
|---|
| 12 | public 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.