Skip to content

Commit

Permalink
Make hello-world-lambda example
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Mar 7, 2018
1 parent d9550ba commit 2878d88
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ plugins {

apply plugin: 'jp.classmethod.aws.lambda'

mainClassName = "example.UpperCaseTitleFunction"
mainClassName = "example.HelloWorldFunction"
applicationDefaultJvmArgs = [""]
jar {
manifest {
attributes 'Main-Class': "example.UpperCaseTitleFunction"
attributes 'Main-Class': "example.HelloWorldFunction"
}
}
repositories {
Expand All @@ -41,7 +41,7 @@ dependencies {
runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.9.1'


testCompile "io.micronaut:http-client:${projectVersion}"
testCompile "io.micronaut:function-client:${projectVersion}"
testRuntime "io.micronaut:http-server-netty:${projectVersion}"
testRuntime "io.micronaut:function-web:${projectVersion}"

Expand All @@ -56,13 +56,11 @@ shadowJar {
}

task deploy(type: AWSLambdaMigrateFunctionTask, dependsOn: shadowJar) {
functionName = "particle-function"
// To deploy MicronautRequestStreamHandler uncomment function-aws dependency
// handler = "io.micronaut.function.aws.MicronautRequestStreamHandler"
handler = "example.UpperCaseTitleFunction::toUpperCase"
functionName = "hello-world"
handler = "example.HelloWorldFunction::toUpperCase"
role = "arn:aws:iam::${aws.accountId}:role/lambda_basic_execution"
runtime = Runtime.Java8
zipFile = shadowJar.archivePath
memorySize = 512
memorySize = 192
timeout = 60
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
package example

import groovy.transform.CompileStatic
import io.micronaut.context.annotation.Value

import javax.inject.Singleton
import javax.validation.constraints.NotNull

/**
* @author Graeme Rocher
* @since 1.0
*/
@Singleton
@CompileStatic
class BookService {
class HelloService {

@Value('${hello.greeting:Hello}')
String greeting = "Hello"

Book toUpperCase(Book book) {
String title = book.title
if(title != null) {
book.title = book.title.toUpperCase()
}
return book
Message hello(@NotNull Person person) {
return new Message(text: "$greeting $person.name!")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package example

import groovy.transform.Field

import javax.inject.Inject

/**
* @author Graeme Rocher
* @since 1.0
*/

@Field BookService bookService
@Field @Inject HelloService helloService

Book toUpperCase(Book book) {
bookService.toUpperCase(book)
Message hello(Person person) {
helloService.hello(person)
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
/*
* Copyright 2017 original authors
*
* Copyright 2018 original 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.
* limitations under the License.
*/
package example

import groovy.transform.EqualsAndHashCode

/**
* @author Graeme Rocher
* @author graemerocher
* @since 1.0
*/
@EqualsAndHashCode
class Book {
String title
class Message {
String text
}
24 changes: 24 additions & 0 deletions examples/hello-world-lambda/src/main/groovy/example/Person.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2018 original 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 example

/**
* @author graemerocher
* @since 1.0
*/
class Person {
String name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2018 original 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 example

import io.micronaut.function.client.FunctionClient
import io.reactivex.Single

import javax.inject.Named
import javax.validation.constraints.NotNull

/**
* @author graemerocher
* @since 1.0
*/
@FunctionClient
interface HelloClient {

@Named("hello-world")
Single<Message> hello(@NotNull Person person)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,24 @@ import spock.lang.Specification
* @author Graeme Rocher
* @since 1.0
*/
class UpperCaseTitleFunctionSpec extends Specification {
class HelloWorldFunctionSpec extends Specification {

void "run function directly"() {
expect:
new UpperCaseTitleFunction()
.toUpperCase(new Book(title: "The Stand")) == new Book(title: "THE STAND")
new HelloWorldFunction()
.hello(new Person(name: "Fred")).text == "Hello Fred!"
}

void "run function as REST service"() {
given:
EmbeddedServer server = ApplicationContext.run(EmbeddedServer)
HttpClient client = server.getApplicationContext().createBean(HttpClient, server.getURL())
HelloClient client = server.getApplicationContext().getBean(HelloClient)

when:
Book book = client.toBlocking()
.retrieve(HttpRequest.POST("/upper-case-title", new Book(title: "The Stand")), Book)

Message message = client.hello(new Person(name: "Fred")).blockingGet()

then:
book.title == "THE STAND"
message.text == "Hello Fred!"

cleanup:
if(server != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import javax.inject.Singleton;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -42,12 +43,18 @@ public class DefaultFunctionDiscoveryClient implements FunctionDiscoveryClient {
private final DiscoveryClient discoveryClient;
private final Map<String, FunctionDefinition> functionDefinitionMap;

public DefaultFunctionDiscoveryClient(DiscoveryClient discoveryClient, FunctionDefinition...definitions) {
public DefaultFunctionDiscoveryClient(DiscoveryClient discoveryClient, FunctionDefinitionProvider[] providers, FunctionDefinition...definitions) {
this.discoveryClient = discoveryClient;
this.functionDefinitionMap = new HashMap<>(definitions.length);
for (FunctionDefinition definition : definitions) {
functionDefinitionMap.put(definition.getName(), definition);
}
for (FunctionDefinitionProvider provider : providers) {
Collection<FunctionDefinition> functionDefinitions = provider.getFunctionDefinitions();
for (FunctionDefinition definition : functionDefinitions) {
functionDefinitionMap.put(definition.getName(), definition);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2018 original 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 io.micronaut.function.client;

import java.util.Collection;

/**
* Interface for a type that provides function definitions
*
* @author graemerocher
* @since 1.0
*/
public interface FunctionDefinitionProvider {

/**
* @return The {@link FunctionDefinition} instances
*/
Collection<FunctionDefinition> getFunctionDefinitions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2018 original 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 io.micronaut.function.client.local;

import io.micronaut.context.annotation.Requires;
import io.micronaut.function.LocalFunctionRegistry;
import io.micronaut.function.client.FunctionDefinition;
import io.micronaut.function.client.FunctionDefinitionProvider;
import io.micronaut.runtime.server.EmbeddedServer;

import javax.inject.Singleton;
import java.net.URI;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author graemerocher
* @since 1.0
*/
@Requires(beans = {EmbeddedServer.class, LocalFunctionRegistry.class})
@Singleton
public class LocalFunctionDefinitionProvider implements FunctionDefinitionProvider {

private final EmbeddedServer embeddedServer;
private final LocalFunctionRegistry localFunctionRegistry;

public LocalFunctionDefinitionProvider(EmbeddedServer embeddedServer, LocalFunctionRegistry localFunctionRegistry) {
this.embeddedServer = embeddedServer;
this.localFunctionRegistry = localFunctionRegistry;
}

@Override
public Collection<FunctionDefinition> getFunctionDefinitions() {
Map<String, URI> availableFunctions = localFunctionRegistry.getAvailableFunctions();
return availableFunctions.entrySet().stream().map((Function<Map.Entry<String, URI>, FunctionDefinition>) stringURIEntry -> new FunctionDefinition() {
@Override
public String getName() {
return stringURIEntry.getKey();
}

@Override
public Optional<URI> getURI() {
return Optional.of(embeddedServer.getURI().resolve(stringURIEntry.getValue()));
}
}).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ else if(Supplier.class.isAssignableFrom(declaringType)) {
*
* @return A map of functions
*/
@Override
public Map<String, URI> getAvailableFunctions() {
return availableFunctions;
return Collections.unmodifiableMap(availableFunctions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import io.micronaut.inject.ExecutableMethod;

import javax.inject.Singleton;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.net.URI;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -85,6 +83,11 @@ public DefaultLocalFunctionRegistry(MediaTypeCodec...decoders) {
.findFirst();
}

@Override
public Map<String, URI> getAvailableFunctions() {
return Collections.emptyMap();
}

@SuppressWarnings("unchecked")
@Override
public <T> Optional<ExecutableMethod<Supplier<T>, T>> findSupplier(String name) {
Expand Down
Loading

0 comments on commit 2878d88

Please sign in to comment.