Skip to content

Commit

Permalink
API Gateway and web data schema (customer-facing) (futurewei-cloud#143)
Browse files Browse the repository at this point in the history
*Add a API gateway/service module

*Add a common web directory to store common web object

*Add API support for API creation, get, and delete (partial).

*Docker and K8s deployment file for API gateway

Co-authored-by: Kevin <[email protected]>
  • Loading branch information
Liguang Xie and kevin-zhonghao authored Mar 30, 2020
1 parent 2de89fa commit 13efc56
Show file tree
Hide file tree
Showing 32 changed files with 1,578 additions and 19 deletions.
37 changes: 37 additions & 0 deletions kubernetes/services/api-gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: apimanager
name: apimanager
spec:
selector:
matchLabels:
app: apimanager
replicas: 1
template:
metadata:
labels:
app: apimanager
spec:
containers:
- image: zhonghaolyu/repo:apiKube4
name: apimanager-web
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: apimanager-service
labels:
name: apimanager-service
spec:
type: NodePort
ports:
- port: 8002
targetPort: 8080
nodePort: 30004
selector:
app: apimanager
10 changes: 3 additions & 7 deletions lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.futurewei.alcor.common</groupId>
<artifactId>alcor_common_lib</artifactId>
<artifactId>AlcorCommonLib</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>alcor_common_lib</name>
<name>AlcorCommonLib</name>
<description>Alcor common library</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -33,10 +33,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@

public class ResourceNotFoundException extends Exception {

public ResourceNotFoundException() {
// private static final long serialVersionUID = 1L;

public ResourceNotFoundException() {
}

public ResourceNotFoundException(String message) {
super(message);
}
//TODO: improve logging

public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public ResourceNotFoundException(Throwable cause) {
super(cause);
}

}
10 changes: 10 additions & 0 deletions lib/src/main/java/com/futurewei/alcor/common/utils/CommonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.UUID;

public class CommonUtil {
public static byte[] fromIpAddressStringToByteArray(String ipAddressString) throws UnknownHostException {
Expand All @@ -37,4 +38,13 @@ public static int getRandomNumberInRange(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}

public static boolean isUUID(String string) {
try {
UUID.fromString(string);
return true;
} catch (Exception ex) {
return false;
}
}
}
16 changes: 16 additions & 0 deletions services/api_gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Dockerfile for API Gateway

FROM openjdk:8

MAINTAINER Zhonghao Lyu <[email protected]>

# API Gateway process
EXPOSE 8080
# API Gateway admin process

# Generate container image and run container
COPY ./target/AlcorApiGateway-0.1.0-SNAPSHOT.jar /app/AlcorApiGateway-0.1.0.jar
#COPY ./config/machine.json /app/config/machine.json

CMD ["java", "-jar", "/app/AlcorApiGateway-0.1.0.jar"]

101 changes: 101 additions & 0 deletions services/api_gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.futurewei.alcor.apigateway</groupId>
<artifactId>AlcorApiGateway</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AlcorApiGateway</name>
<description>Alcor API Gateway</description>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
<snippets>${project.basedir}/target/generated-snippets/</snippets>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.futurewei.alcor.common</groupId>
<artifactId>AlcorCommonLib</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.futurewei.alcor.web</groupId>
<artifactId>AlcorWeb</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2019 The Alcor Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.futurewei.alcor.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class AlcorApiGatewayApplication {

public static void main(String[] args) {
SpringApplication.run(AlcorApiGatewayApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2019 The Alcor Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.futurewei.alcor.apigateway.admin;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(AdminWebDestinations.class)
public class AdminWebConfiguration {

@Bean
public RouteLocator debugProxyRouting(RouteLocatorBuilder builder, AdminWebDestinations adminWebDestinations) {
return builder.routes()
.route(r -> r
.path("/get")
.filters(f -> f.addRequestHeader("Hello", "Alcor"))
.uri(adminWebDestinations.getDebugServiceUrl()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2019 The Alcor Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.futurewei.alcor.apigateway.admin;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.validation.constraints.NotNull;

@Data
@ConfigurationProperties(prefix = "admin.destinations")
public class AdminWebDestinations {

private String defaultServiceUrl = "http://httpbin.org:80";

@NotNull
private String debugServiceUrl;

public String getDebugServiceUrl() {
return this.debugServiceUrl == null ? defaultServiceUrl : this.debugServiceUrl;
}

public void setDebugServiceUrl(String debugServiceUrl) {
this.debugServiceUrl = debugServiceUrl;
}
}
Loading

0 comments on commit 13efc56

Please sign in to comment.