experimental Azure Web adapter
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
Copied from https://github.com/awslabs/aws-serverless-java-container/tree/main/samples/spring/pet-store
|
||||
|
||||
# Serverless Spring example
|
||||
A basic pet store written with the [Spring framework](https://projects.spring.io/spring-framework/). The `StreamLambdaHandler` object is the main entry point for Lambda.
|
||||
|
||||
The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yml` file in the root folder contains the application definition.
|
||||
|
||||
## Pre-requisites
|
||||
* [AWS CLI](https://aws.amazon.com/cli/)
|
||||
* [SAM CLI](https://github.com/awslabs/aws-sam-cli)
|
||||
* [Gradle](https://gradle.org/) or [Maven](https://maven.apache.org/)
|
||||
|
||||
## Deployment
|
||||
In a shell, navigate to the sample's folder and use the SAM CLI to build a deployable package
|
||||
```
|
||||
$ sam build
|
||||
```
|
||||
|
||||
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
|
||||
|
||||
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
|
||||
|
||||
```
|
||||
$ sam deploy --guided
|
||||
```
|
||||
|
||||
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You can use `curl` or a web browser to make a call to the URL
|
||||
|
||||
```
|
||||
...
|
||||
---------------------------------------------------------------------------------------------------------
|
||||
OutputKey-Description OutputValue
|
||||
---------------------------------------------------------------------------------------------------------
|
||||
PetStoreApi - URL for application https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/pets
|
||||
---------------------------------------------------------------------------------------------------------
|
||||
|
||||
$ curl https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/pets
|
||||
```
|
||||
@@ -0,0 +1,147 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>oz.spring.petstore</groupId>
|
||||
<artifactId>pet-store</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>pet-store</name>
|
||||
<description>Simple pet store written with the Spring framework</description>
|
||||
|
||||
<scm>
|
||||
<url>https://github.com/awslabs/aws-serverless-java-container.git</url>
|
||||
</scm>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<properties>
|
||||
<java.version>8</java.version>
|
||||
<spring-boot-thin-layout.version>1.0.28.RELEASE</spring-boot-thin-layout.version>
|
||||
|
||||
<!-- Spring Boot start class! WARING: correct class must be set! -->
|
||||
<start-class>oz.spring.petstore.PetStoreSpringAppConfig</start-class>
|
||||
|
||||
<!-- AZURE FUNCTION CONFIG -->
|
||||
<azure.functions.maven.plugin.version>1.23.0</azure.functions.maven.plugin.version>
|
||||
<functionAppName>spring-cloud-function-samples</functionAppName>
|
||||
<functionAppRegion>westus</functionAppRegion>
|
||||
<functionResourceGroup>java-functions-group</functionResourceGroup>
|
||||
<functionAppServicePlanName>java-functions-app-service-plan</functionAppServicePlanName>
|
||||
<functionPricingTier>EP1</functionPricingTier>
|
||||
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
|
||||
<spring.version>5.3.25</spring.version>
|
||||
<junit.version>4.13.2</junit.version>
|
||||
<log4j.version>2.19.0</log4j.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-adapter-azure-web</artifactId>
|
||||
<version>3.2.9-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!--
|
||||
the Spring Context Indexer run an annotation processor at compile time and generates
|
||||
a META-INF/spring.components file that Spring can use to speed up component scanning at boot time.
|
||||
For small applications, this doesn't make a big difference. However, for large applications with
|
||||
complex dependencies this may improve your cold start time significantly.
|
||||
-->
|
||||
<!-- <dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-indexer</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency> -->
|
||||
|
||||
<!-- <dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency> -->
|
||||
|
||||
<!-- <dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency> -->
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-functions-maven-plugin</artifactId>
|
||||
<version>${azure.functions.maven.plugin.version}</version>
|
||||
|
||||
<configuration>
|
||||
<localDebugConfig>transport=dt_socket,server=y,suspend=y,address=5005</localDebugConfig>
|
||||
<appName>${functionAppName}</appName>
|
||||
<resourceGroup>${functionResourceGroup}</resourceGroup>
|
||||
<region>${functionAppRegion}</region>
|
||||
<appServicePlanName>${functionAppServicePlanName}</appServicePlanName>
|
||||
<pricingTier>${functionPricingTier}</pricingTier>
|
||||
|
||||
<hostJson>${project.basedir}/src/main/resources/host.json</hostJson>
|
||||
|
||||
<runtime>
|
||||
<os>linux</os>
|
||||
<javaVersion>11</javaVersion>
|
||||
</runtime>
|
||||
|
||||
<funcPort>7072</funcPort>
|
||||
|
||||
<appSettings>
|
||||
<property>
|
||||
<name>FUNCTIONS_EXTENSION_VERSION</name>
|
||||
<value>~4</value>
|
||||
</property>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>package-functions</id>
|
||||
<goals>
|
||||
<goal>package</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot.experimental</groupId>
|
||||
<artifactId>spring-boot-thin-layout</artifactId>
|
||||
<version>${spring-boot-thin-layout.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or 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
|
||||
*
|
||||
* https://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 oz.spring.petstore;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.web.servlet.HandlerAdapter;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
@Configuration
|
||||
@Import({ PetsController.class})
|
||||
public class PetStoreSpringAppConfig {
|
||||
/*
|
||||
* Create required HandlerMapping, to avoid several default HandlerMapping instances being created
|
||||
*/
|
||||
@Bean
|
||||
public HandlerMapping handlerMapping() {
|
||||
return new RequestMappingHandlerMapping();
|
||||
}
|
||||
|
||||
/*
|
||||
* Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
|
||||
*/
|
||||
@Bean
|
||||
public HandlerAdapter handlerAdapter() {
|
||||
return new RequestMappingHandlerAdapter();
|
||||
}
|
||||
|
||||
/*
|
||||
* optimization - avoids creating default exception resolvers; not required as the serverless container handles all
|
||||
* exceptions
|
||||
*
|
||||
* By default, an ExceptionHandlerExceptionResolver is created which creates many dependent object, including an
|
||||
* expensive ObjectMapper instance.
|
||||
*
|
||||
* To enable custom @ControllerAdvice classes remove this bean.
|
||||
*/
|
||||
@Bean
|
||||
public HandlerExceptionResolver handlerExceptionResolver() {
|
||||
return new HandlerExceptionResolver() {
|
||||
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception ex) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or 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
|
||||
*
|
||||
* https://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 oz.spring.petstore;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import oz.spring.petstore.model.Pet;
|
||||
import oz.spring.petstore.model.PetData;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@EnableWebMvc
|
||||
public class PetsController {
|
||||
@RequestMapping(path = "/pets", method = RequestMethod.POST)
|
||||
public Pet createPet(@RequestBody Pet newPet) {
|
||||
if (newPet.getName() == null || newPet.getBreed() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Pet dbPet = newPet;
|
||||
dbPet.setId(UUID.randomUUID().toString());
|
||||
return dbPet;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/pets", method = RequestMethod.GET)
|
||||
public Pet[] listPets(@RequestParam("limit") Optional<Integer> limit, Principal principal) {
|
||||
int queryLimit = 10;
|
||||
if (limit.isPresent()) {
|
||||
queryLimit = limit.get();
|
||||
}
|
||||
|
||||
Pet[] outputPets = new Pet[queryLimit];
|
||||
|
||||
for (int i = 0; i < queryLimit; i++) {
|
||||
Pet newPet = new Pet();
|
||||
newPet.setId(UUID.randomUUID().toString());
|
||||
newPet.setName(PetData.getRandomName());
|
||||
newPet.setBreed(PetData.getRandomBreed());
|
||||
newPet.setDateOfBirth(PetData.getRandomDoB());
|
||||
outputPets[i] = newPet;
|
||||
}
|
||||
|
||||
return outputPets;
|
||||
}
|
||||
|
||||
@GetMapping("favicon.ico")
|
||||
@ResponseBody
|
||||
void returnNoFavicon() {
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET)
|
||||
public Pet listPets() {
|
||||
Pet newPet = new Pet();
|
||||
newPet.setId(UUID.randomUUID().toString());
|
||||
newPet.setBreed(PetData.getRandomBreed());
|
||||
newPet.setDateOfBirth(PetData.getRandomDoB());
|
||||
newPet.setName(PetData.getRandomName());
|
||||
return newPet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or 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
|
||||
*
|
||||
* https://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 oz.spring.petstore.model;
|
||||
|
||||
public class Error {
|
||||
private String message;
|
||||
|
||||
public Error(String errorMessage) {
|
||||
message = errorMessage;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or 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
|
||||
*
|
||||
* https://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 oz.spring.petstore.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Pet {
|
||||
private String id;
|
||||
private String breed;
|
||||
private String name;
|
||||
private Date dateOfBirth;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getBreed() {
|
||||
return breed;
|
||||
}
|
||||
|
||||
public void setBreed(String breed) {
|
||||
this.breed = breed;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Date getDateOfBirth() {
|
||||
return dateOfBirth;
|
||||
}
|
||||
|
||||
public void setDateOfBirth(Date dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or 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
|
||||
*
|
||||
* https://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 oz.spring.petstore.model;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class PetData {
|
||||
private static List<String> breeds = new ArrayList<>();
|
||||
static {
|
||||
breeds.add("Afghan Hound");
|
||||
breeds.add("Beagle");
|
||||
breeds.add("Bernese Mountain Dog");
|
||||
breeds.add("Bloodhound");
|
||||
breeds.add("Dalmatian");
|
||||
breeds.add("Jack Russell Terrier");
|
||||
breeds.add("Norwegian Elkhound");
|
||||
}
|
||||
|
||||
private static List<String> names = new ArrayList<>();
|
||||
static {
|
||||
names.add("Bailey");
|
||||
names.add("Bella");
|
||||
names.add("Max");
|
||||
names.add("Lucy");
|
||||
names.add("Charlie");
|
||||
names.add("Molly");
|
||||
names.add("Buddy");
|
||||
names.add("Daisy");
|
||||
names.add("Rocky");
|
||||
names.add("Maggie");
|
||||
names.add("Jake");
|
||||
names.add("Sophie");
|
||||
names.add("Jack");
|
||||
names.add("Sadie");
|
||||
names.add("Toby");
|
||||
names.add("Chloe");
|
||||
names.add("Cody");
|
||||
names.add("Bailey");
|
||||
names.add("Buster");
|
||||
names.add("Lola");
|
||||
names.add("Duke");
|
||||
names.add("Zoe");
|
||||
names.add("Cooper");
|
||||
names.add("Abby");
|
||||
names.add("Riley");
|
||||
names.add("Ginger");
|
||||
names.add("Harley");
|
||||
names.add("Roxy");
|
||||
names.add("Bear");
|
||||
names.add("Gracie");
|
||||
names.add("Tucker");
|
||||
names.add("Coco");
|
||||
names.add("Murphy");
|
||||
names.add("Sasha");
|
||||
names.add("Lucky");
|
||||
names.add("Lily");
|
||||
names.add("Oliver");
|
||||
names.add("Angel");
|
||||
names.add("Sam");
|
||||
names.add("Princess");
|
||||
names.add("Oscar");
|
||||
names.add("Emma");
|
||||
names.add("Teddy");
|
||||
names.add("Annie");
|
||||
names.add("Winston");
|
||||
names.add("Rosie");
|
||||
}
|
||||
|
||||
public static List<String> getBreeds() {
|
||||
return breeds;
|
||||
}
|
||||
|
||||
public static List<String> getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
public static String getRandomBreed() {
|
||||
return breeds.get(ThreadLocalRandom.current().nextInt(0, breeds.size() - 1));
|
||||
}
|
||||
|
||||
public static String getRandomName() {
|
||||
return names.get(ThreadLocalRandom.current().nextInt(0, names.size() - 1));
|
||||
}
|
||||
|
||||
public static Date getRandomDoB() {
|
||||
GregorianCalendar gc = new GregorianCalendar();
|
||||
|
||||
int year = ThreadLocalRandom.current().nextInt(
|
||||
Calendar.getInstance().get(Calendar.YEAR) - 15,
|
||||
Calendar.getInstance().get(Calendar.YEAR)
|
||||
);
|
||||
|
||||
gc.set(Calendar.YEAR, year);
|
||||
|
||||
int dayOfYear = ThreadLocalRandom.current().nextInt(1, gc.getActualMaximum(Calendar.DAY_OF_YEAR));
|
||||
|
||||
gc.set(Calendar.DAY_OF_YEAR, dayOfYear);
|
||||
return gc.getTime();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"version": "2.0",
|
||||
"extensionBundle": {
|
||||
"id": "Microsoft.Azure.Functions.ExtensionBundle",
|
||||
"version": "[3.*, 4.0.0)"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user