├── .gcloudignore ├── Dockerfile ├── scripts.sh ├── package.json ├── .gitignore ├── CONTRIBUTING.md ├── src ├── index.js ├── firestore.js ├── maps.js ├── tasks.js └── index.html ├── README.md └── LICENSE /.gcloudignore: -------------------------------------------------------------------------------- 1 | # This file specifies files that are *not* uploaded to Google Cloud Platform 2 | # using gcloud. It follows the same syntax as .gitignore, with the addition of 3 | # "#!include" directives (which insert the entries of the given .gitignore-style 4 | # file at that point). 5 | # 6 | # For more information, run: 7 | # $ gcloud topic gcloudignore 8 | # 9 | .gcloudignore 10 | 11 | # If you would like to upload your .git directory, .gitignore file or files 12 | # from your .gitignore file, remove the corresponding line 13 | # below: 14 | .git 15 | .gitignore 16 | 17 | node_modules -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Use the official Node.js 12 image. 2 | # https://hub.docker.com/_/node 3 | FROM node:12 4 | # Create and change to the app directory. 5 | WORKDIR /usr/src/app 6 | # Copy application dependency manifests to the container image. 7 | # A wildcard is used to ensure both package.json AND package-lock.json are copied. 8 | # Copying this separately prevents re-running npm install on every code change. 9 | COPY package*.json ./ 10 | # Install production dependencies. 11 | RUN npm install --only=production 12 | # Copy local code to the container image. 13 | COPY . . 14 | # Run the web service on container startup. 15 | CMD [ "npm", "start" ] -------------------------------------------------------------------------------- /scripts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euxo pipefail 3 | 4 | # Deploys the application to Google Cloud Functions 5 | deploy_to_functions() { 6 | echo 'Deploying to Cloud Functions...'; 7 | gcloud functions deploy tasks-pizza \ 8 | --trigger-http \ 9 | --runtime=nodejs10 \ 10 | --env-vars-file=.env.yaml 11 | echo "Deployed $(gcloud functions describe tasks-pizza --format 'value(httpsTrigger.url)')" 12 | } 13 | 14 | # Deploys the application to Google Cloud Run 15 | deploy_to_run() { 16 | echo 'Deploying to Cloud Run...'; 17 | gcloud components install beta 18 | GCP_PROJECT=$(gcloud config list --format 'value(core.project)' 2>/dev/null) 19 | gcloud config set run/region us-central1 20 | gcloud config set run/platform managed 21 | gcloud builds submit --tag gcr.io/$GCP_PROJECT/tasks-pizza 22 | gcloud beta run deploy tasks-pizza \ 23 | --image gcr.io/$GCP_PROJECT/tasks-pizza \ 24 | --allow-unauthenticated 25 | echo "Deployed $(gcloud beta run routes describe tasks-pizza --platform managed --format 'value(status.address.url)')" 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tasks-pizza", 3 | "version": "1.0.0", 4 | "description": "A sample application using Cloud Tasks and Google Maps (to find pizza).", 5 | "main": "src/index.js", 6 | "private": true, 7 | "scripts": { 8 | "start": "FUNCTION_SOURCE=src npx @google-cloud/functions-framework", 9 | "functions": "source scripts.sh && deploy_to_functions", 10 | "cloudrun": "source scripts.sh && deploy_to_run" 11 | }, 12 | "dependencies": { 13 | "@google-cloud/firestore": "^2.3.0", 14 | "@google-cloud/functions-framework": "^1.3.2", 15 | "@google-cloud/tasks": "^1.4.0", 16 | "@google/maps": "^0.5.5", 17 | "dotenv": "^8.1.0", 18 | "express": "^4.17.1", 19 | "firebase-admin": "^8.6.0", 20 | "node-fetch": "^2.6.0" 21 | }, 22 | "devDependencies": { 23 | "@types/express": "^4.17.1", 24 | "@types/google__maps": "^0.5.8" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "git+https://github.com/GoogleCloudPlatform/tasks-pizza.git" 29 | }, 30 | "author": "Grant Timmerman", 31 | "homepage": "https://github.com/GoogleCloudPlatform/tasks-pizza#readme" 32 | } 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | # for local testing with bash 59 | .env 60 | # for gcloud 61 | .env.yaml 62 | 63 | # next.js build output 64 | .next 65 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | ### Before you contribute 9 | Before we can use your code, you must sign the 10 | [Google Individual Contributor License Agreement](https://cla.developers.google.com/about/google-individual) 11 | (CLA), which you can do online. The CLA is necessary mainly because you own the 12 | copyright to your changes, even after your contribution becomes part of our 13 | codebase, so we need your permission to use and distribute your code. We also 14 | need to be sure of various other things—for instance that you'll tell us if you 15 | know that your code infringes on other people's patents. You don't have to sign 16 | the CLA until after you've submitted your code for review and a member has 17 | approved it, but you must do it before we can put your code into our codebase. 18 | Before you start working on a larger contribution, you should get in touch with 19 | us first through the issue tracker with your idea so that we can help out and 20 | possibly guide you. Coordinating up front makes it much easier to avoid 21 | frustration later on. 22 | 23 | ### Code reviews 24 | All submissions, including submissions by project members, require review. We 25 | use Github pull requests for this purpose. 26 | 27 | ### The small print 28 | Contributions made by corporations are covered by a different agreement than 29 | the one above, the 30 | [Software Grant and Corporate Contributor License Agreement](https://cla.developers.google.com/about/google-corporate). -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * The main GCF function using the Functions Framework. 17 | * @see https://github.com/GoogleCloudPlatform/functions-framework-nodejs 18 | */ 19 | const path = require('path'); 20 | require('dotenv').config(); 21 | 22 | // Express Routing 23 | const express = require('express'); 24 | const app = express(); 25 | const routes = { 26 | '/tasks/start': require('./tasks').start, // Creates ~13k Cloud Tasks 27 | '/tasks/listnames': require('./tasks').listnames, // Lists expected names of Tasks 28 | '/maps/add': require('./maps').add, // Adds 1 city record to the database 29 | '/maps/get': require('./maps').get, // Gets 1 city record from the database 30 | '/maps/listnames': require('./maps').listnames, // List the names of all city records 31 | '/maps/key': (req, res) => res.send({key: process.env.KEY}), // API KEY for frontend 32 | '/target': require('./maps').add, // Tasks target, query and add a city record 33 | '/web': (req, res) => res.sendFile(path.join(__dirname, 'index.html')) 34 | }; 35 | Object.entries(routes).map(([route, func]) => app.use(route, func)); 36 | app.use('/', (req, res) => res.send(Object.keys(routes))); // default 37 | 38 | // Export the Express app to the Functions Framework 39 | exports.function = app; -------------------------------------------------------------------------------- /src/firestore.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * Firestore Methods 17 | * - All location data is stored in Firestore 18 | * - Every location is 1 Firestore Document that contains Maps API results 19 | * @see https://firebase.google.com/docs/firestore/quickstart 20 | * @see https://cloud.google.com/firestore/quotas 21 | */ 22 | const COLLECTION = 'tasks-pizza'; 23 | 24 | // Setup Firestore with default credentials 25 | const admin = require('firebase-admin'); 26 | admin.initializeApp({ 27 | credential: admin.credential.applicationDefault() 28 | }); 29 | const db = admin.firestore(); 30 | 31 | // Lists all stored locations 32 | module.exports.getLocations = async () => { 33 | const docs = await db.collection(COLLECTION).listDocuments(); 34 | return docs.map(d => d.id); 35 | } 36 | 37 | // Get a single location 38 | module.exports.getLocation = async (location) => { 39 | const doc = await db.collection(COLLECTION).doc(location).get(); 40 | const data = doc.data(); 41 | return data || { error: 'No data for this location found.'}; 42 | } 43 | 44 | /** 45 | * Store the location data 46 | * @param {string} location The name of the location 47 | * @param {object} locationData The data about the location 48 | */ 49 | module.exports.storeLocation = async (location, locationData) => { 50 | return await db.collection(COLLECTION).doc(location).set(locationData); 51 | } 52 | -------------------------------------------------------------------------------- /src/maps.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * Methods for interacting with the Google Maps API 17 | * All methods require the environment variable `KEY`. 18 | */ 19 | const KEY = process.env.KEY; 20 | const maps = require('@google/maps').createClient({ 21 | key: KEY, 22 | Promise, 23 | }); 24 | const firestore = require('./firestore'); 25 | 26 | if (!KEY) { 27 | throw new Error('Missing `KEY` environment variable for Google Maps.'); 28 | } 29 | 30 | /** 31 | * Gets a photo given a photo reference hash. 32 | * @see https://developers.google.com/places/web-service/photos 33 | */ 34 | async function getPhoto(photoreference) { 35 | const photo = await maps.placesPhoto({ 36 | photoreference, 37 | maxheight: 1600, 38 | maxwidth: 1600, 39 | }).asPromise(); 40 | return photo; 41 | } 42 | 43 | /** 44 | * Returns a Google Maps Places API result for the best pizza near a location/place. 45 | * @param {string} placesQuery The place query. 46 | * @returns {object} The Google Maps Places API result. 47 | */ 48 | const getPlace = async (placesQuery) => { 49 | // Get the lat,lng for the query 50 | const geocoding = await maps.geocode({ 51 | address: placesQuery, 52 | }).asPromise(); 53 | if (!geocoding.json.results) throw new Error('No results'); 54 | const {lat, lng} = geocoding.json.results[0].geometry.location; 55 | 56 | // Find the place 57 | // @see https://developers.google.com/places/web-service/search 58 | var places = await maps.placesNearby({ 59 | radius: 5000, // meters 60 | keyword: 'pizza', 61 | type: 'restaurant', 62 | location: [lat, lng], 63 | }).asPromise(); 64 | if (places.json.results.length === 0) return; // no results found 65 | const place = places.json.results[0]; 66 | return place; 67 | } 68 | 69 | // Gets Map data for a single id from the Firestore database. 70 | module.exports.get = async (req, res) => { 71 | const placeId = req.query.id; 72 | if (!placeId) return res.send({error: 'No ?id='}); 73 | const place = await firestore.getLocation(placeId); 74 | res.send(place); 75 | }; 76 | 77 | // Returns a list of map names in the Firestore database. 78 | module.exports.listnames = async (req, res) => { 79 | const locations = await firestore.getLocations(); 80 | res.send(locations); 81 | }; 82 | 83 | // Adds a specific map to the Firestore database. 84 | module.exports.add = async (req, res) => { 85 | // Only handle valid requests 86 | const id = req.query.id; 87 | if (!id) return res.send({error: 'No ?id='}); 88 | 89 | // Get the place 90 | const place = await getPlace(id); 91 | if (!place) return res.send({error: 'No results found.'}); // no results found. 92 | const placeData = { 93 | id, 94 | place, 95 | url: `https://www.google.com/maps/place/?q=place_id:${place.place_id}`, 96 | }; 97 | await firestore.storeLocation(id, placeData); 98 | res.send(placeData); 99 | }; 100 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ⚠️ **THIS REPO HAS BEEN ARCHIVED AND IS NO LONGER MAINTAINED.** ⚠️ 2 | 3 | # Cloud Tasks Pizza Map 4 | 5 | Google Maps of Earth with 16k pins 6 | 7 | > As featured on Medium: https://medium.com/p/db888675db71/ 8 | 9 | A sample application using Cloud Tasks and Google Maps to find the best pizza restaurants around 10 | the world. 11 | 12 | The program works in the following way: 13 | 14 | 1. Creates a Cloud Tasks Queue 15 | 1. Creates ~10,000 Cloud Tasks with different names of cities 16 | 1. Each Cloud Task triggers a Cloud Function. This Function does the following: 17 | 1. Looks up the best pizza restaurant in that city accoring to the Google Maps Places API 18 | 1. Stores the restaurant data in Firestore 19 | 20 | An application architecture diagram of the Cloud Tasks with an HTTP target of a Cloud Function. The Cloud Function uses the Google Maps Nearby/Places APIs for map data and Firestore to store location data. 21 | 22 | ## Setup 23 | 24 | This sample uses many APIs and a Firestore database 25 | that must be enable prior to running the application. 26 | 27 | ### Enable all APIs 28 | 29 | The following APIs are used in this app: 30 | 31 | - `Cloud Tasks API` 32 | - `Maps Places API` 33 | - `Maps Geocoding API` 34 | - `Firestore API` 35 | 36 | [Enable these APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudtasks.googleapis.com,firestore.googleapis.com,places-backend.googleapis.com,static-maps-backend.googleapis.com,geocoding-backend.googleapis.com 37 | ). 38 | 39 | ### Create a Firestore database 40 | 41 | [Create a `Firestore` database](https://firebase.google.com/docs/firestore/quickstart#create) with these configurations: 42 | - Mode: `Test mode` 43 | - Collection: `tasks-pizza`. 44 | 45 | ### Create an API Key 46 | 47 | [Create a Google Maps API key](https://developers.google.com/maps/documentation/javascript/get-api-key). 48 | Create an `.env` file with a Google Maps API key: 49 | 50 | ```sh 51 | KEY=AIeSyDh7ggKmvLzFAeq_ICGkO8ryvEMm3Nrde-z 52 | ``` 53 | 54 | ## Run the Functions Framework 55 | 56 | The [Functions Framework](https://github.com/GoogleCloudPlatform/functions-framework-nodejs) enables 57 | you to run a Google Cloud Function locally on your computer. This is very useful for testing our code. 58 | 59 | To test our server locally, follow these instructions using our `KEY` from the previous step: 60 | 61 | ```sh 62 | npm i 63 | KEY=??? npm start 64 | ``` 65 | 66 | Observe the Functions Framework starts a server and logs information: 67 | 68 | ``` 69 | npx: installed 52 in 8.771s 70 | Serving function... 71 | Function: function 72 | URL: http://localhost:8080/ 73 | ``` 74 | 75 | Go to URL specified to view all routes for the application. 76 | 77 | ## Deploy to Google Cloud 78 | 79 | You have two options for deploying this application to Google Cloud: 80 | 81 | - Cloud Functions 82 | - Cloud Run 83 | 84 | The `gcloud` commands for deploying to each target are in the `scripts.sh` file. 85 | 86 | ### Deploy to Google Cloud Function 87 | 88 | Deploy your function on Google Cloud Functions on runtime Node 10: 89 | 90 | ```sh 91 | npm run functions 92 | ``` 93 | 94 | ### Deploy to Google Cloud Run 95 | 96 | You can also deploy this application to Cloud Run by first building the container, then deploying: 97 | 98 | ```sh 99 | npm run cloudrun 100 | ``` 101 | 102 | ### GIF 103 | 104 | Here is a GIF of the applicaiton working at different Google Map zoom levels 105 | ![A Google Map with thousands of pins at different zoom levels](https://user-images.githubusercontent.com/744973/69364872-2af0b300-0c61-11ea-951f-20272657a9c8.gif) 106 | -------------------------------------------------------------------------------- /src/tasks.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | const fetch = require('node-fetch'); 16 | const {v2beta3} = require('@google-cloud/tasks'); // Must be v2beta3 or else tasks creation fails 17 | const client = new v2beta3.CloudTasksClient(); 18 | 19 | // Application configuration 20 | const PROJECT = process.env.PROJECT || 'serverless-com-demo'; 21 | const QUEUE = process.env.QUEUE || 'my-queue'; 22 | const LOCATION = process.env.LOCATION || 'us-central1'; 23 | 24 | // Construct the fully qualified queue name. 25 | const parent = client.queuePath(PROJECT, LOCATION, QUEUE); 26 | 27 | /** 28 | * Gets a list of cities. 29 | * @returns {string[]} A list of city names. 30 | */ 31 | async function getLocationNames() { 32 | const URL_LOCATIONS = 'https://api.github.com/gists/7100f98e7d3dd48b3c4d7cb85cfd313f'; // 13k locations 33 | // const URL_LOCATIONS = 'https://api.github.com/gists/b503eecba3c49198cc0447550cbe3ccb'; // 19 locations 34 | const cities = await fetch(URL_LOCATIONS); 35 | const json = await cities.json(); 36 | const content = json.files['cities.txt'].content; 37 | const contentItems = content.split('\n'); 38 | return contentItems; 39 | } 40 | 41 | /** 42 | * Creates a Task Queue 43 | * @param {string} queueName The Cloud Tasks queue name. 44 | */ 45 | async function createQueue(queueName) { 46 | const request = { 47 | parent: client.locationPath(PROJECT, LOCATION), 48 | queue: { 49 | name: client.queuePath(PROJECT, LOCATION, queueName), 50 | rate_limits: {}, 51 | retry_config: {}, 52 | }, 53 | }; 54 | const res = await client.createQueue(request); 55 | return res; 56 | } 57 | 58 | /** 59 | * Returns `true` if a queue with `queueName` exists. 60 | * @param {string} queueName The queue name to check. 61 | * @returns `true` if the queue exists. 62 | */ 63 | async function queueExists(queueName) { 64 | const queue = await client.getQueue({ 65 | name: client.queuePath(PROJECT, LOCATION, queueName) 66 | }); 67 | return queue.length > 0; 68 | } 69 | 70 | /** 71 | * Creates a named Cloud Task. 72 | * Note: The the placeName can't be added frequently as the Task de-duplication window is ~1h. 73 | * @param {string} placeName The name of the city/place to target for our HTTP request. 74 | * @see https://cloud.google.com/tasks/docs/quotas 75 | */ 76 | async function createTask(placeName) { 77 | const normalizedName = placeName.normalize('NFD') 78 | .replace(/[\u0300-\u036f]/g, '') // Remove accents 79 | .replace(/[^a-zA-Z]+/g, "-"); // Only keep [a-zA-Z] 80 | const name = client.taskPath(PROJECT, LOCATION, QUEUE, normalizedName); 81 | const task = { 82 | name, 83 | httpRequest: { 84 | httpMethod: 'GET', 85 | url: `https://${LOCATION}-${PROJECT}.cloudfunctions.net/tasks-pizza/target?id=${placeName}`, 86 | }, 87 | }; 88 | 89 | // Send create task request. 90 | const request = {parent, task}; 91 | try { 92 | const [response] = await client.createTask(request); 93 | console.log(`Created task ${response.name}`); 94 | } catch (e) { 95 | console.error(`ERROR: ${e.details} (${name})`); 96 | } 97 | } 98 | 99 | /** 100 | * Runs the program. 101 | * - Creates Cloud Tasks Queue if needed 102 | * - Creates N Cloud Tasks 103 | */ 104 | async function run() { 105 | console.log('Starting...'); 106 | 107 | console.log('Checking if queue exists...'); 108 | if (!(await queueExists(QUEUE))) { 109 | console.log(`Creating queue "${QUEUE}"...`); 110 | await createQueue(QUEUE); 111 | } else { 112 | console.log(`Queue "${QUEUE}" exists.`); 113 | } 114 | 115 | console.log('Creating Tasks...'); 116 | const locations = await getLocationNames(); 117 | for (const loc of locations) { 118 | await createTask(loc); 119 | } 120 | console.log('Done.'); 121 | } 122 | 123 | // Export routes for Express app 124 | module.exports.listnames = async (req, res) => { 125 | const locationList = await getLocationNames(); 126 | res.send(locationList); 127 | } 128 | module.exports.start = async (req, res) => { 129 | await run(); 130 | res.sendStatus(200); 131 | } 132 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | Cloud Tasks Pizza Map 8 | 19 | 20 | 21 |
22 | 119 | 120 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. --------------------------------------------------------------------------------