Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the Endpoints Frameworks v2 Echo Sample #284

Merged
merged 12 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appengine/endpoints-frameworks-v2/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
swagger.json
77 changes: 77 additions & 0 deletions appengine/endpoints-frameworks-v2/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# App Engine Standard & Google Cloud Endpoints Frameworks & Java

This sample demonstrates how to use Google Cloud Endpoints Frameworks using
Java on App Engine Standard.

## Adding the project ID to the sample API code

You must add the project ID obtained when you created your project to the
sample's `pom.xml` before you can deploy the code.

To add the project ID:

0. Edit the file `pom.xml`.

0. For `<endpoints.project.id>`, replace the value `YOUR_PROJECT_ID` with
your project ID.

0. Save your changes.

## Building the sample project

To build the project:

mvn clean package

## Generating the swagger.json file

To generate the required configuration file `swagger.json`:

0. Download and unzip the [Endpoints Framework tools
package](http://search.maven.org/remotecontent?filepath=com/google/endpoints/endpoints-framework-tools/2.0.0-beta.7/endpoints-framework-tools-2.0.0-beta.7.zip).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll ok this if you insist, however:

  1. You could do this from maven and then add a a -Pproperty or -Dflag that causes maven to execute this. There are examples of this runlocal or HelloWorldWrite that would be a better experience for first time users.

It would also be a bit more sustainable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I think we will go with what we have for now. I will create a bug to explore this solution and update the sample.

0. Invoke the Endpoints Tool using this command:

path/to/endpoints-framework-tools-2.0.0-beta.7/bin/endpoints-framework-tools get-swagger-doc \
-h <PROJECT_ID>.appspot.com \
-w target/echo-1.0-SNAPSHOT com.example.echo.Echo

Replace`<PROJECT_ID>` with your project ID.

## Deploying the sample API to App Engine

To deploy the sample API:

0. Invoke the `gcloud` command to deploy the API configuration file:

gcloud beta service-management deploy swagger.json

0. Deploy the API implementation code by invoking:

mvn appengine:update

The first time you upload a sample app, you may be prompted to authorize the
deployment. Follow the prompts: when you are presented with a browser window
containing a code, copy it to the terminal window.

0. Wait for the upload to finish.

## Sending a request to the sample API

After you deploy the API and its configuration file, you can send requests
to the API.

To send a request to the API, from a command line, invoke the following `cURL`
command:

curl \
-H "Content-Type: application/json" \
-X POST \
-d '{"message":"echo"}' \
https://$PROJECT_ID.appspot.com/_ah/api/echo/v1/echo

You will get a 200 response with the following data:

{
"message": "echo"
}
104 changes: 104 additions & 0 deletions appengine/endpoints-frameworks-v2/backend/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!--
Copyright 2016 Google Inc. All Rights Reserved.

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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<groupId>com.example.echo</groupId>
<artifactId>echo</artifactId>

<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../../..</relativePath>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<endpoints.project.id>YOUR_PROJECT_ID</endpoints.project.id>
</properties>

<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.endpoints</groupId>
<artifactId>endpoints-framework</artifactId>
<version>2.0.0-beta.7</version>
</dependency>
</dependencies>

<build>
<!-- for hot reload of the web application-->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
<goal>display-plugin-updates</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.sdk.version}</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
<!-- address>0.0.0.0</address>
<port>8080</port -->
<!-- Comment in the below snippet to enable local debugging with a remove debugger
like those included with Eclipse or IntelliJ -->
<!-- jvmFlags>
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
</jvmFlags -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2016 Google Inc.
*
* 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.example.echo;

import com.google.api.server.spi.auth.common.User;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;

/** The Echo API which Endpoints will be exposing. */
@Api(
name = "echo",
version = "v1",
namespace =
@ApiNamespace(
ownerDomain = "echo.example.com",
ownerName = "echo.example.com",
packagePath = ""
)
)
public class Echo {
/**
* Echoes the received message back.
*
* Note that name is specified and will override the default name of "{class name}.{method
* name}". For example, the default is "echo.echo".
*
* Note that httpMethod is not specified. This will default to a reasonable HTTP method
* depending on the API method name. In this case, the HTTP method will default to POST.
*/
@ApiMethod(name = "echo")
public Message echo(Message message) {
return message;
}

/**
* Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP
* 401.
*
* Note that name is not specified. This will default to "{class name}.{method name}". For
* example, the default is "echo.getUserEmail".
*
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due
* to the API method name. httpMethod is added here for example purposes.
*/
@ApiMethod(httpMethod = ApiMethod.HttpMethod.GET)
public Email getUserEmail(User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException();
}

Email response = new Email();
response.setEmail(user.getEmail());
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016 Google Inc.
*
* 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.example.echo;

/** The email bean that will be used in the getUserEmail response. */
public class Email {
private String email;

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2016 Google Inc.
*
* 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.example.echo;

/** The message bean that will be used in the echo request and response. */
public class Message {

private String message;

public String getMessage() {
return this.message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016 Google Inc.
*
* 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.example.echo;

import com.google.api.server.spi.ServiceException;

/**
* Throw this when the user is unauthorized.
*
* Note that this must inherit from ServiceException.
*/
public class UnauthorizedException extends ServiceException {
public UnauthorizedException() {
super(401, "Unauthorized");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 Google Inc. All Rights Reserved.

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.
-->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>${endpoints.project.id}</application>
<version>1</version>
<threadsafe>true</threadsafe>

<basic-scaling>
<max-instances>2</max-instances>
</basic-scaling>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>
Loading