diff --git a/cors/cors-java/.editorconfig b/cors/cors-java/.editorconfig new file mode 100644 index 0000000..e9d0038 --- /dev/null +++ b/cors/cors-java/.editorconfig @@ -0,0 +1,12 @@ +[*] +charset=utf-8 +end_of_line=lf +insert_final_newline=true +trim_trailing_whitespace = true +indent_style=space +indent_size=2 + + +[*.java] +indent_style=space +indent_size=4 diff --git a/cors/cors-java/.sbtopts b/cors/cors-java/.sbtopts new file mode 100644 index 0000000..00f1e60 --- /dev/null +++ b/cors/cors-java/.sbtopts @@ -0,0 +1,4 @@ +-J-Xms512M +-J-Xmx4096M +-J-Xss2M +-J-XX:MaxMetaspaceSize=1024M diff --git a/cors/cors-java/LICENSE b/cors/cors-java/LICENSE new file mode 100644 index 0000000..a12e20f --- /dev/null +++ b/cors/cors-java/LICENSE @@ -0,0 +1,15 @@ +This software is licensed under the Apache 2 license, quoted below. + +Copyright 2016 Lightbend Inc. [http://www.lightbend.com] + +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. diff --git a/cors/cors-java/README.md b/cors/cors-java/README.md new file mode 100644 index 0000000..446b5b7 --- /dev/null +++ b/cors/cors-java/README.md @@ -0,0 +1,9 @@ +# CORS recipe for Lagom's Javadsl + + +``` +curl -H "Access-Control-Request-Method: GET" \ + -H "Access-Control-Request-Headers: origin, x-requested-with" \ + -H "Origin: http://www.some-domain.com" \ + -X OPTIONS http://localhost:9000/api/hello/123 -v +``` diff --git a/cors/cors-java/build.sbt b/cors/cors-java/build.sbt new file mode 100644 index 0000000..ae8bb87 --- /dev/null +++ b/cors/cors-java/build.sbt @@ -0,0 +1,26 @@ +organization in ThisBuild := "com.lightbend.lagom.recipes" +version in ThisBuild := "1.0-SNAPSHOT" + +// the Scala version that will be used for cross-compiled libraries +scalaVersion in ThisBuild := "2.11.8" + +lazy val `cors-java` = (project in file(".")) + .aggregate(`cors-java-api`, `cors-java-impl`) + +lazy val `cors-java-api` = (project in file("cors-java-api")) + .settings( + libraryDependencies ++= Seq( + lagomJavadslApi + ) + ) + +lazy val `cors-java-impl` = (project in file("cors-java-impl")) + .enablePlugins(LagomJava) + .settings( + libraryDependencies ++= Seq( + ) + ) + .dependsOn(`cors-java-api`) + +lagomCassandraEnabled in ThisBuild := false +lagomKafkaEnabled in ThisBuild := false diff --git a/cors/cors-java/cors-java-api/src/main/java/com/lightbend/lagom/recipes/corsjava/api/CorsjavaService.java b/cors/cors-java/cors-java-api/src/main/java/com/lightbend/lagom/recipes/corsjava/api/CorsjavaService.java new file mode 100644 index 0000000..7819f7a --- /dev/null +++ b/cors/cors-java/cors-java-api/src/main/java/com/lightbend/lagom/recipes/corsjava/api/CorsjavaService.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 Lightbend Inc. + */ +package com.lightbend.lagom.recipes.corsjava.api; + +import akka.NotUsed; +import com.lightbend.lagom.javadsl.api.Descriptor; +import com.lightbend.lagom.javadsl.api.Service; +import com.lightbend.lagom.javadsl.api.ServiceCall; + +import static com.lightbend.lagom.javadsl.api.Service.named; +import static com.lightbend.lagom.javadsl.api.Service.pathCall; + +public interface CorsjavaService extends Service { + + /** + * Example: curl http://localhost:9000/api/hello/Alice + */ + ServiceCall hello(String id); + + @Override + default Descriptor descriptor() { + return named("corsjava").withCalls( + pathCall("/api/hello/:id", this::hello) + ).withAutoAcl(true); + } + +} diff --git a/cors/cors-java/cors-java-impl/src/main/java/com/lightbend/lagom/recipes/corsjava/impl/CorsjavaModule.java b/cors/cors-java/cors-java-impl/src/main/java/com/lightbend/lagom/recipes/corsjava/impl/CorsjavaModule.java new file mode 100644 index 0000000..b8be801 --- /dev/null +++ b/cors/cors-java/cors-java-impl/src/main/java/com/lightbend/lagom/recipes/corsjava/impl/CorsjavaModule.java @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2016 Lightbend Inc. + */ +package com.lightbend.lagom.recipes.corsjava.impl; + +import com.google.inject.AbstractModule; +import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport; +import com.lightbend.lagom.recipes.corsjava.api.CorsjavaService; + +/** + * The module that binds the CorsjavaService so that it can be served. + */ +public class CorsjavaModule extends AbstractModule implements ServiceGuiceSupport { + @Override + protected void configure() { + bindService(CorsjavaService.class, CorsjavaServiceImpl.class); + } +} diff --git a/cors/cors-java/cors-java-impl/src/main/java/com/lightbend/lagom/recipes/corsjava/impl/CorsjavaServiceImpl.java b/cors/cors-java/cors-java-impl/src/main/java/com/lightbend/lagom/recipes/corsjava/impl/CorsjavaServiceImpl.java new file mode 100644 index 0000000..89efc83 --- /dev/null +++ b/cors/cors-java/cors-java-impl/src/main/java/com/lightbend/lagom/recipes/corsjava/impl/CorsjavaServiceImpl.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2016 Lightbend Inc. + */ +package com.lightbend.lagom.recipes.corsjava.impl; + +import akka.NotUsed; +import com.lightbend.lagom.javadsl.api.ServiceCall; +import com.lightbend.lagom.recipes.corsjava.api.CorsjavaService; + +import javax.inject.Inject; +import java.util.concurrent.CompletableFuture; + +/** + * Implementation of the CorsjavaService. + */ +public class CorsjavaServiceImpl implements CorsjavaService { + + + @Inject + public CorsjavaServiceImpl() { + } + + @Override + public ServiceCall hello(String id) { + return request -> CompletableFuture.completedFuture(id); + } + + +} diff --git a/cors/cors-java/cors-java-impl/src/main/resources/application.conf b/cors/cors-java/cors-java-impl/src/main/resources/application.conf new file mode 100644 index 0000000..1145ea2 --- /dev/null +++ b/cors/cors-java/cors-java-impl/src/main/resources/application.conf @@ -0,0 +1,6 @@ +# +# Copyright (C) 2016 Lightbend Inc. +# +play.crypto.secret=whatever +play.modules.enabled += com.lightbend.lagom.recipes.corsjava.impl.CorsjavaModule + diff --git a/cors/cors-java/project/build.properties b/cors/cors-java/project/build.properties new file mode 100644 index 0000000..05610ec --- /dev/null +++ b/cors/cors-java/project/build.properties @@ -0,0 +1,4 @@ +# +# Copyright (C) 2016 Lightbend Inc. +# +sbt.version=0.13.15 diff --git a/cors/cors-java/project/plugins.sbt b/cors/cors-java/project/plugins.sbt new file mode 100644 index 0000000..971111f --- /dev/null +++ b/cors/cors-java/project/plugins.sbt @@ -0,0 +1,4 @@ +// The Lagom plugin +addSbtPlugin("com.lightbend.lagom" % "lagom-sbt-plugin" % "1.3.7") +// Needed for importing the project into Eclipse +addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")