a5d739c5a27407613944f095b283fe9f55bcd36e
[demos/kafka/outbox] / src / main / java / de / trion / kafka / outbox / Application.java
1 package de.trion.kafka.outbox;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.boot.CommandLineRunner;
7 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.autoconfigure.SpringBootApplication;
9 import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.jdbc.core.JdbcTemplate;
12 import org.springframework.stereotype.Component;
13 import org.springframework.web.servlet.config.annotation.CorsRegistry;
14 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
15
16 @SpringBootApplication
17 @EnableConfigurationProperties(ApplicationProperties.class)
18 @Component
19 public class Application implements CommandLineRunner {
20
21     private final static Logger LOG = LoggerFactory.getLogger(Application.class);
22
23
24     @Autowired
25     ApplicationProperties properties;
26     @Autowired
27     JdbcTemplate jdbcTemplate;
28
29
30     @Bean
31     public String bootstrapServers() { return properties.bootstrapServers; }
32
33     @Bean
34     public String topic() {
35         return properties.topic;
36     }
37
38     @Bean
39     public String consumerGroup() {
40         return properties.consumerGroup;
41     }
42
43     @Bean
44     public WebMvcConfigurer corsConfigurer() {
45         return new WebMvcConfigurer() {
46             @Override
47             public void addCorsMappings(CorsRegistry registry) {
48                 registry
49                         .addMapping("/**")
50                         .allowedOrigins("http://localhost:4200");
51             }
52         };
53     }
54
55
56     @Override
57     public void run(String... strings) throws Exception {
58
59         LOG.info("Creating tables");
60         jdbcTemplate.execute("DROP TABLE users IF EXISTS");
61         jdbcTemplate.execute("CREATE TABLE users(id SERIAL, username VARCHAR(255), loggedIn BOOLEAN)");
62     }
63
64
65     public static void main(String[] args) {
66         SpringApplication.run(Application.class, args);
67     }
68
69 }