Skip to content

Commit

Permalink
Merge pull request #2 from imminoglobulin/base-configs
Browse files Browse the repository at this point in the history
Base configs
  • Loading branch information
bkocoglu authored Jul 8, 2020
2 parents 0b4a654 + d4892a7 commit a021d46
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 2 deletions.
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@

<properties>
<java.version>14</java.version>
<springfox.swagger.version>2.9.2</springfox.swagger.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- CACHE -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
Expand All @@ -39,6 +50,34 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.swagger.version}</version>
</dependency>

<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/kstech/requeks/RequeksApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package com.kstech.requeks;

import com.kstech.requeks.util.DefaultProfileUtil;
import com.kstech.requeks.util.InitializeLogUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@EnableScheduling
@EnableAsync
public class RequeksApplication {

public static void main(String[] args) {
SpringApplication.run(RequeksApplication.class, args);
SpringApplication app = new SpringApplication(RequeksApplication.class);
DefaultProfileUtil.addDefaultProfile(app);
Environment env = app.run(args).getEnvironment();
InitializeLogUtil.logApplicationStartup(env);
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/kstech/requeks/config/SecurityConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kstech.requeks.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().anyRequest().permitAll();
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/kstech/requeks/config/WebConfigurer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.kstech.requeks.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebConfigurer extends WebMvcConfigurerAdapter {
@Primary
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kstech.requeks.constants;

import java.util.Arrays;
import java.util.List;

public interface ApplicationConstants {
String SPRING_PROFILE_DEVELOPMENT = "dev";
String SPRING_PROFILE_TEST = "test";
String SPRING_PROFILE_PRODUCTION = "prod";

String SPRING_PROFILE_DEFAULT = "spring.profiles.default";

String ZONE_ID = "Europe/Istanbul";

List<String> EXCEPT_LOG_METHODS = Arrays.asList("info");
}
30 changes: 30 additions & 0 deletions src/main/java/com/kstech/requeks/util/DefaultProfileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kstech.requeks.util;

import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;

import java.util.HashMap;
import java.util.Map;

import static com.kstech.requeks.constants.ApplicationConstants.SPRING_PROFILE_DEFAULT;
import static com.kstech.requeks.constants.ApplicationConstants.SPRING_PROFILE_DEVELOPMENT;

public final class DefaultProfileUtil {

private DefaultProfileUtil() {
}

public static void addDefaultProfile(SpringApplication app) {
Map<String, Object> defProperties = new HashMap<>();
defProperties.put(SPRING_PROFILE_DEFAULT, SPRING_PROFILE_DEVELOPMENT);
app.setDefaultProperties(defProperties);
}

public static String[] getActiveProfiles(Environment env) {
String[] profiles = env.getActiveProfiles();
if (profiles.length == 0) {
return env.getDefaultProfiles();
}
return profiles;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/kstech/requeks/util/InitializeLogUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kstech.requeks.util;

import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
public final class InitializeLogUtil {
public static void logApplicationStartup(Environment env) {
String protocol = "http";
if (env.getProperty("server.ssl.key-store") != null) {
protocol = "https";
}
String serverPort = env.getProperty("server.port");
String contextPath = env.getProperty("server.servlet.context-path");
if (StringUtils.isBlank(contextPath)) {
contextPath = "/";
}
String hostAddress = "localhost";
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
log.error("The host name could not be determined, using `localhost` as fallback");
}
log.info("\n----------------------------------------------------------\n\t" +
"Application '{}' is running! Access URLs:\n\t" +
"Local: \t\t{}://localhost:{}{}\n\t" +
"External: \t{}://{}:{}{}\n\t" + "\n----------------------------------------------------------",
env.getProperty("spring.application.name"),
protocol,
serverPort,
contextPath,
protocol,
hostAddress,
serverPort,
contextPath);
}
}
78 changes: 78 additions & 0 deletions src/main/java/com/kstech/requeks/util/ObjectMapperUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.kstech.requeks.util;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public final class ObjectMapperUtils {

private static ModelMapper modelMapper;

/**
* Model mapper property setting are specified in the following block.
* Default property matching strategy is set to Strict see {@link MatchingStrategies}
* Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
*/
static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}

/**
* Hide from public usage.
*/
private ObjectMapperUtils() {
}

/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public static <D, T> D map(final T entity, Class<D> outClass) {
if (entity == null ){
return null;
}
return modelMapper.map(entity, outClass);
}

/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
if (entityList == null){
return null;
}

return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}

/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public static <S, D> D map(final S source, D destination) {
if (source == null ){
return null;
}
modelMapper.map(source, destination);
return destination;
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
spring:
application:
name: config-server
data:
mongodb:
uri: ${mongodb.logger.uri:${mongodb.uri:mongodb://localhost:27017}}
database: requeks

server:
port: 9090
servlet:
session:
cookie:
http-only: true

logging:
file:
name: log/requeks.log

management:
endpoint:
routes:
enabled: true
health:
enabled: true
show-details: always
endpoints:
web:
exposure:
include: "*"
16 changes: 16 additions & 0 deletions src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
${AnsiColor.BLUE} _
${AnsiColor.BLUE} | |
${AnsiColor.BLUE} _ __ ___ __ _ _ _ ___| | _____
${AnsiColor.BLUE}| '__/ _ \/ _` | | | |/ _ \ |/ / __|
${AnsiColor.BLUE}| | | __/ (_| | |_| | __/ <\__ \
${AnsiColor.BLUE}|_| \___|\__, |\__,_|\___|_|\_\___/
${AnsiColor.BLUE} | |
${AnsiColor.BLUE} |_|

${AnsiColor.RED}█
${AnsiColor.RED}█
${AnsiColor.RED}█ ${AnsiColor.BLACK}Application Name : ${spring.application.name}
${AnsiColor.RED}█ ${AnsiColor.CYAN}Spring Boot Version : ${spring-boot.formatted-version}
${AnsiColor.RED}█ ${AnsiColor.BRIGHT_BLACK}Java Version : ${java.version}
${AnsiColor.RED}█
${AnsiColor.RED}█

0 comments on commit a021d46

Please sign in to comment.