13
src/main/java/dev/mars3142/fhq/Application.java
Normal file
13
src/main/java/dev/mars3142/fhq/Application.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package dev.mars3142.fhq;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
59
src/main/java/dev/mars3142/fhq/edge/GCPGatewayFilter.java
Normal file
59
src/main/java/dev/mars3142/fhq/edge/GCPGatewayFilter.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package dev.mars3142.fhq.edge;
|
||||
|
||||
import com.google.auth.oauth2.GoogleCredentials;
|
||||
import com.google.auth.oauth2.IdTokenCredentials;
|
||||
import com.google.auth.oauth2.IdTokenProvider;
|
||||
import java.io.IOException;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import lombok.val;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class GCPGatewayFilter extends AbstractGatewayFilterFactory<GCPGatewayFilter.Config> {
|
||||
|
||||
public GCPGatewayFilter() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return (exchange, chain) -> {
|
||||
try {
|
||||
val token = getAuthToken(config);
|
||||
exchange.getRequest()
|
||||
.mutate()
|
||||
.header("X-Serverless-Authorization", "Bearer " + token);
|
||||
return chain.filter(exchange);
|
||||
} catch (Exception e) {
|
||||
log.error("Error getting token", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String getAuthToken(Config config) throws IOException {
|
||||
val credentials = GoogleCredentials.getApplicationDefault();
|
||||
if (!(credentials instanceof IdTokenProvider)) {
|
||||
throw new IllegalArgumentException("Credentials are not an instance of IdTokenProvider.");
|
||||
}
|
||||
val tokenCredential =
|
||||
IdTokenCredentials.newBuilder()
|
||||
.setIdTokenProvider((IdTokenProvider) credentials)
|
||||
.setTargetAudience(config.getAudience())
|
||||
.build();
|
||||
|
||||
return tokenCredential.refreshAccessToken().getTokenValue();
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
public static class Config {
|
||||
|
||||
private String audience;
|
||||
}
|
||||
}
|
0
src/main/resources/application-dev.yaml
Normal file
0
src/main/resources/application-dev.yaml
Normal file
49
src/main/resources/application.yaml
Normal file
49
src/main/resources/application.yaml
Normal file
@@ -0,0 +1,49 @@
|
||||
info:
|
||||
application:
|
||||
name: ${spring.application.name}
|
||||
version: '@project.version@'
|
||||
spring-cloud-version: '@spring-cloud.version@'
|
||||
spring-boot-version: '@project.parent.version@'
|
||||
|
||||
server:
|
||||
port: ${PORT:8080}
|
||||
shutdown: graceful
|
||||
error:
|
||||
include-message: on_param
|
||||
include-stacktrace: on_param
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: edge-service
|
||||
cloud:
|
||||
gateway:
|
||||
default-filters:
|
||||
- name: Retry
|
||||
args:
|
||||
retries: 3
|
||||
methods: GET
|
||||
series: SERVER_ERROR
|
||||
exceptions: java.io.IOException, java.util.concurrent.TimeoutException
|
||||
backoff:
|
||||
firstBackoff: 50ms
|
||||
maxBackoff: 5000ms
|
||||
routes:
|
||||
- id: timezone-service
|
||||
uri: ${TIMEZONE_SERVICE_URI:http://timezone-service}
|
||||
predicates:
|
||||
- Path=/v1/timezone
|
||||
- Path=/v1/timezone/**
|
||||
filters:
|
||||
- name: GCPGatewayFilter
|
||||
args:
|
||||
audience: ${TIMEZONE_SERVICE_URI:http://timezone-service}
|
||||
|
||||
- id: backend-service
|
||||
uri: ${BACKEND_SERVICE_URI:http://backend-service}
|
||||
predicates:
|
||||
- Path=/v1/**
|
||||
filters:
|
||||
#- AuthGatewayFilter
|
||||
- name: GCPGatewayFilter
|
||||
args:
|
||||
audience: ${BACKEND_SERVICE_URI:http://backend-service}
|
8
src/main/resources/banner.txt
Normal file
8
src/main/resources/banner.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
,------. ,--. ,---. ,--.
|
||||
| .---' ,-| | ,---. ,---. ,-----.' .-' ,---. ,--.--.,--. ,--.`--' ,---. ,---.
|
||||
| `--, ' .-. || .-. || .-. :'-----'`. `-.| .-. :| .--' \ `' / ,--.| .--'| .-. :
|
||||
| `---.\ `-' |' '-' '\ --. .-' \ --.| | \ / | |\ `--.\ --.
|
||||
`------' `---' .`- / `----' `-----' `----'`--' `--' `--' `---' `----'
|
||||
`---'
|
||||
${application.title} ${application.version}
|
||||
Powered by Spring Boot ${spring-boot.version}
|
24
src/main/resources/logback.xml
Normal file
24
src/main/resources/logback.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true">
|
||||
|
||||
<!-- use Spring default values -->
|
||||
<appender class="ch.qos.logback.core.ConsoleAppender" name="Console">
|
||||
<encoder>
|
||||
<charset>utf8</charset>
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
|
||||
<!-- LOG everything at INFO level -->
|
||||
<logger additivity="false" level="debug" name="dev.mars3142">
|
||||
<appender-ref ref="Console"/>
|
||||
</logger>
|
||||
|
||||
<!-- LOG "dev.mars3142.*" at DEBUG level -->
|
||||
<root level="info">
|
||||
<appender-ref ref="Console"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
13
src/test/java/dev/mars3142/fhq/ApplicationTests.java
Normal file
13
src/test/java/dev/mars3142/fhq/ApplicationTests.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package dev.mars3142.fhq;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user