From 19aaaa8d6be3e11d6b4aa24631d1578aeb559708 Mon Sep 17 00:00:00 2001 From: Vangie Du Date: Mon, 2 Mar 2020 19:03:59 +0800 Subject: [PATCH] =?UTF-8?q?mongodb=20=E7=A4=BA=E4=BE=8B=20(#5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mongodb示例 Co-authored-by: tianxiaodan <50935943+txd12@users.noreply.github.com> --- README.md | 6 + rds-mongodb/.gitignore | 2 + rds-mongodb/Makefile | 32 +++ rds-mongodb/README.md | 74 ++++++ rds-mongodb/bin/start_mongo.sh | 15 ++ rds-mongodb/dotnetcore/Program.cs | 35 +++ rds-mongodb/dotnetcore/mongodb.csproj | 12 + rds-mongodb/java/pom.xml | 34 +++ .../java/src/main/java/example/App.java | 54 ++++ rds-mongodb/nodejs/index.js | 39 +++ rds-mongodb/nodejs/package-lock.json | 149 +++++++++++ rds-mongodb/nodejs/package.json | 14 + rds-mongodb/php/Funfile | 7 + rds-mongodb/php/composer.json | 11 + rds-mongodb/php/composer.lock | 86 +++++++ rds-mongodb/php/index.php | 23 ++ rds-mongodb/python/index.py | 15 ++ rds-mongodb/python/requirements.txt | 1 + rds-mongodb/template.override.yml | 24 ++ rds-mongodb/template.yml | 241 ++++++++++++++++++ rds-redis/python/index.py | 2 +- rds-redis/template.override.yml | 2 +- 22 files changed, 876 insertions(+), 2 deletions(-) create mode 100644 rds-mongodb/.gitignore create mode 100644 rds-mongodb/Makefile create mode 100644 rds-mongodb/README.md create mode 100755 rds-mongodb/bin/start_mongo.sh create mode 100644 rds-mongodb/dotnetcore/Program.cs create mode 100644 rds-mongodb/dotnetcore/mongodb.csproj create mode 100644 rds-mongodb/java/pom.xml create mode 100644 rds-mongodb/java/src/main/java/example/App.java create mode 100644 rds-mongodb/nodejs/index.js create mode 100644 rds-mongodb/nodejs/package-lock.json create mode 100644 rds-mongodb/nodejs/package.json create mode 100644 rds-mongodb/php/Funfile create mode 100644 rds-mongodb/php/composer.json create mode 100644 rds-mongodb/php/composer.lock create mode 100644 rds-mongodb/php/index.php create mode 100644 rds-mongodb/python/index.py create mode 100644 rds-mongodb/python/requirements.txt create mode 100644 rds-mongodb/template.override.yml create mode 100644 rds-mongodb/template.yml diff --git a/README.md b/README.md index ed4fbc3..bc69d41 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,9 @@ * [Java 示例工程](rds-redis/java) * [PHP 示例工程](rds-redis/php) * [.NetCore 示例工程](rds-redis/dotnetcore) +* [MongoDB 数据库](rds-mongodb) + * [Nodejs 示例工程](rds-mongodb/nodejs) + * [Python 示例工程](rds-mongodb/python) + * [Java 示例工程](rds-mongodb/java) + * [PHP 示例工程](rds-mongodb/php) + * [.NetCore 示例工程](rds-mongodb/dotnetcore) diff --git a/rds-mongodb/.gitignore b/rds-mongodb/.gitignore new file mode 100644 index 0000000..7af690a --- /dev/null +++ b/rds-mongodb/.gitignore @@ -0,0 +1,2 @@ +php/extension/ +php/vendor/ \ No newline at end of file diff --git a/rds-mongodb/Makefile b/rds-mongodb/Makefile new file mode 100644 index 0000000..d72f60e --- /dev/null +++ b/rds-mongodb/Makefile @@ -0,0 +1,32 @@ +STACK_NAME ?= mongo-test + +start_mongo: + bin/start_mongo.sh + +stop_mongo: + docker stop mongo_for_fc + +clean: + docker rm -f mongo_for_fc + +build: + IGNORE_TPL_VALIDATION=true fun build + +deploy: + fun package + fun deploy --use-ros --stack-name $(STACK_NAME) + +invoke_java: + IGNORE_TPL_VALIDATION=true fun invoke java + +invoke_nodejs: + IGNORE_TPL_VALIDATION=true fun invoke nodejs + +invoke_dotnetcore: + IGNORE_TPL_VALIDATION=true fun invoke dotnetcore + +invoke_python: + IGNORE_TPL_VALIDATION=true fun invoke nodejs + +invoke_php: + IGNORE_TPL_VALIDATION=true fun invoke php \ No newline at end of file diff --git a/rds-mongodb/README.md b/rds-mongodb/README.md new file mode 100644 index 0000000..5e6898d --- /dev/null +++ b/rds-mongodb/README.md @@ -0,0 +1,74 @@ +# 函数计算访问 MongoDB 示例集 + +## 依赖工具 + +本项目是在 MacOS 下开发的,涉及到的工具是平台无关的,对于 Linux 和 Windows 桌面系统应该也同样适用。在开始本例之前请确保如下工具已经正确的安装,更新到最新版本,并进行正确的配置。 + +* [Docker](https://www.docker.com/) +* [Funcraft](https://github.com/aliyun/fun) + +Fun 工具依赖于 docker 来模拟本地环境。 + +对于 MacOS 用户可以使用 [homebrew](https://brew.sh/) 进行安装: + +```bash +brew cask install docker +brew tap vangie/formula +brew install fun +``` + +Windows 和 Linux 用户安装请参考: + +1. https://github.com/aliyun/fun/blob/master/docs/usage/installation.md + +安装好后,记得先执行 `fun config` 初始化一下配置。 + +**注意**, 如果你已经安装过了 funcraft,确保 funcraft 的版本在 3.5.3 以上。 + +```bash +$ fun --version +3.5.3 +``` + +## 初始化 + +```bash +git clone http://github.com/vangie/fc-db +cd fc-db/rds-mongodb +``` + +## 构建 + +```bash +make build +``` + +## 部署 + +```bash +make deploy +``` + +## 测试 + +```bash +# java +make invoke_java + +# nodejs +make invoke_nodejs + +# python +make invoke_python + +# .NET +make invoke_dotnetcore +``` + +## 示例工程源码 + +* [Nodejs 示例工程](nodejs) +* [Python 示例工程](python) +* [Java 示例工程](java) +* [Dotnetcore 示例工程](dotnetcore) +* [PHP 示例工程](php) diff --git a/rds-mongodb/bin/start_mongo.sh b/rds-mongodb/bin/start_mongo.sh new file mode 100755 index 0000000..e5bc7bb --- /dev/null +++ b/rds-mongodb/bin/start_mongo.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +CONTAINER_NAME=mongo_for_fc +USERNAME=mongoadmin +PASSWORD=secret + +if [ ! "$(docker ps -a | grep $CONTAINER_NAME)" ]; then + docker run --name $CONTAINER_NAME \ + -e MONGO_INITDB_ROOT_USERNAME=$USERNAME \ + -e MONGO_INITDB_ROOT_PASSWORD=$PASSWORD \ + -p 27017:27017 \ + mongo +else + docker start $CONTAINER_NAME +fi \ No newline at end of file diff --git a/rds-mongodb/dotnetcore/Program.cs b/rds-mongodb/dotnetcore/Program.cs new file mode 100644 index 0000000..f703adf --- /dev/null +++ b/rds-mongodb/dotnetcore/Program.cs @@ -0,0 +1,35 @@ +using System; +using System.IO; +using System.Text; +using Aliyun.Serverless.Core; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace mongodb +{ + public class MongoDBHandler + { + private static String dbName = Environment.GetEnvironmentVariable("MONGO_DATABASE"); + private static String connectionUrl = Environment.GetEnvironmentVariable("MONGO_URL"); + public Stream Handler(Stream input, IFcContext context) + { + var client = new MongoClient(connectionUrl); + var database = client.GetDatabase(dbName); + var collection = database.GetCollection("fc_col"); + var document = new BsonDocument + { + { "DEMO", "FC" }, + { "MSG", "Hello FunctionCompute For MongoDB" } + }; + collection.InsertOne(document); + var doc = collection.Find(new BsonDocument{{ "DEMO", "FC"}}).FirstOrDefault(); + Console.WriteLine(doc.ToString()); + + byte[] docBytes = Encoding.UTF8.GetBytes(doc.ToString()); + MemoryStream output = new MemoryStream(); + output.Write(docBytes, 0, docBytes.Length); + return output; + } + + } +} diff --git a/rds-mongodb/dotnetcore/mongodb.csproj b/rds-mongodb/dotnetcore/mongodb.csproj new file mode 100644 index 0000000..cfc9522 --- /dev/null +++ b/rds-mongodb/dotnetcore/mongodb.csproj @@ -0,0 +1,12 @@ + + + + Library + netcoreapp2.1 + + + + + + + diff --git a/rds-mongodb/java/pom.xml b/rds-mongodb/java/pom.xml new file mode 100644 index 0000000..5c52542 --- /dev/null +++ b/rds-mongodb/java/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + example + fc-java-mongo + jar + 1.0-SNAPSHOT + fc-java-mongo + + + + junit + junit + 3.8.1 + test + + + com.aliyun.fc.runtime + fc-java-core + 1.3.0 + + + org.mongodb + mongo-java-driver + 3.12.1 + + + + + 1.8 + 1.8 + true + + diff --git a/rds-mongodb/java/src/main/java/example/App.java b/rds-mongodb/java/src/main/java/example/App.java new file mode 100644 index 0000000..e759b75 --- /dev/null +++ b/rds-mongodb/java/src/main/java/example/App.java @@ -0,0 +1,54 @@ +package example; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import com.aliyun.fc.runtime.Context; +import com.aliyun.fc.runtime.StreamRequestHandler; +import org.bson.BsonDocument; +import org.bson.BsonString; +import org.bson.Document; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoClientURI; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoDatabase; + +public class App implements StreamRequestHandler { + + private static String dbName = System.getenv("MONGO_DATABASE"); + private static String url = System.getenv("MONGO_URL"); + + @Override + public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { + + MongoClient mongoClient = new MongoClient(new MongoClientURI(url)); + + try { + MongoDatabase database = mongoClient.getDatabase(dbName); + MongoCollection collection = database.getCollection("fc_col"); + Document doc = new Document(); + String demoName = "FC"; + doc.append("DEMO", "FC"); + doc.append("MSG", "Hello FunctionCompute For MongoDB"); + collection.insertOne(doc); + System.out.println("insert document: " + doc); + + BsonDocument filter = new BsonDocument(); + filter.append("DEMO", new BsonString(demoName)); + MongoCursor cursor = collection.find(filter).iterator(); + + while (cursor.hasNext()) { + Document document = cursor.next(); + System.out.println("find document: " + document); + outputStream.write(document.toString().getBytes()); + } + } finally { + mongoClient.close(); + } + } + +} diff --git a/rds-mongodb/nodejs/index.js b/rds-mongodb/nodejs/index.js new file mode 100644 index 0000000..dd50122 --- /dev/null +++ b/rds-mongodb/nodejs/index.js @@ -0,0 +1,39 @@ +'use strict'; +const MongoClient = require('mongodb').MongoClient; +const assert = require('assert'); + +exports.handler = async (event, context, callback) => { + + const dbName = process.env.MONGO_DATABASE; + const url = process.env.MONGO_URL; + + const client = new MongoClient(url); + + try { + await client.connect(); + const db = client.db(dbName); + + const demoName = "FC"; + const doc = { + "DEMO": demoName, + "MSG": "Hello FunctionCompute For MongoDB" + }; + + let col = db.collection('fc_col'); + + let r = await col.insertOne(doc); + assert.equal(1, r.insertedCount); + + const filter = { "DEMO": demoName }; + let res = await col.findOne(filter); + + console.log(res); + callback(null, res); + + } catch (err) { + console.log(err.stack); + callback(err, "Error"); + } finally { + client.close(); + } +}; \ No newline at end of file diff --git a/rds-mongodb/nodejs/package-lock.json b/rds-mongodb/nodejs/package-lock.json new file mode 100644 index 0000000..81ec6e0 --- /dev/null +++ b/rds-mongodb/nodejs/package-lock.json @@ -0,0 +1,149 @@ +{ + "name": "nodejs-mongo", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bl": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.0.tgz", + "integrity": "sha512-wbgvOpqopSr7uq6fJrLH8EsvYMJf9gzfo2jCsL2eTy75qXPukA4pCgHamOQkZtY5vmfVtjB+P3LNlMHW5CEZXA==", + "requires": { + "readable-stream": "^2.3.5", + "safe-buffer": "^5.1.1" + } + }, + "bson": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.3.tgz", + "integrity": "sha512-TdiJxMVnodVS7r0BdL42y/pqC9cL2iKynVwA0Ho3qbsQYr428veL3l7BQyuqiw+Q5SqqoT0m4srSY/BlZ9AxXg==" + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "denque": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", + "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, + "mongodb": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.3.tgz", + "integrity": "sha512-II7P7A3XUdPiXRgcN96qIoRa1oesM6qLNZkzfPluNZjVkgQk3jnQwOT6/uDk4USRDTTLjNFw2vwfmbRGTA7msg==", + "requires": { + "bl": "^2.2.0", + "bson": "^1.1.1", + "denque": "^1.4.1", + "require_optional": "^1.0.1", + "safe-buffer": "^5.1.2", + "saslprep": "^1.0.0" + } + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "^2.0.0", + "semver": "^5.1.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, + "safe-buffer": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz", + "integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg==" + }, + "saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "requires": { + "memory-pager": "^1.0.2" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + } + } +} diff --git a/rds-mongodb/nodejs/package.json b/rds-mongodb/nodejs/package.json new file mode 100644 index 0000000..cbc27d4 --- /dev/null +++ b/rds-mongodb/nodejs/package.json @@ -0,0 +1,14 @@ +{ + "name": "nodejs-mongo", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "mongodb": "^3.5.3" + } +} diff --git a/rds-mongodb/php/Funfile b/rds-mongodb/php/Funfile new file mode 100644 index 0000000..cc53b00 --- /dev/null +++ b/rds-mongodb/php/Funfile @@ -0,0 +1,7 @@ +RUNTIME php7.2 + +# https://github.com/doctrine/DoctrineMongoDBBundle/issues/450 +RUN apt-get update && apt-get install -y libcurl4-openssl-dev pkg-config libssl-dev +RUN pecl install mongodb +RUN mkdir -p /code/extension && mv /usr/local/lib/php/extensions/*/mongodb.so /code/extension +RUN echo "extension=/code/extension/mongodb.so" > /code/extension/mongodb.ini \ No newline at end of file diff --git a/rds-mongodb/php/composer.json b/rds-mongodb/php/composer.json new file mode 100644 index 0000000..b92b09b --- /dev/null +++ b/rds-mongodb/php/composer.json @@ -0,0 +1,11 @@ +{ + "require": { + "mongodb/mongodb": "^1.6" + }, + "repositories": { + "packagist": { + "type": "composer", + "url": "https://packagist.phpcomposer.com" + } + } +} diff --git a/rds-mongodb/php/composer.lock b/rds-mongodb/php/composer.lock new file mode 100644 index 0000000..2d2365a --- /dev/null +++ b/rds-mongodb/php/composer.lock @@ -0,0 +1,86 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "c1d60b8b919ea95715022c9a284d8e95", + "packages": [ + { + "name": "mongodb/mongodb", + "version": "1.6.0", + "source": { + "type": "git", + "url": "https://github.com/mongodb/mongo-php-library.git", + "reference": "dc43ba25fb593d6a2988e6a535b6f5386eda5b15" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mongodb/mongo-php-library/zipball/dc43ba25fb593d6a2988e6a535b6f5386eda5b15", + "reference": "dc43ba25fb593d6a2988e6a535b6f5386eda5b15", + "shasum": "" + }, + "require": { + "ext-hash": "*", + "ext-json": "*", + "ext-mongodb": "^1.7", + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7.27 || ^6.4 || ^8.3", + "sebastian/comparator": "^1.0 || ^2.0 || ^3.0", + "squizlabs/php_codesniffer": "^3.4", + "symfony/phpunit-bridge": "^4.4@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6.x-dev" + } + }, + "autoload": { + "psr-4": { + "MongoDB\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Andreas Braun", + "email": "andreas.braun@mongodb.com" + }, + { + "name": "Jeremy Mikola", + "email": "jmikola@gmail.com" + }, + { + "name": "Katherine Walker", + "email": "katherine.walker@mongodb.com" + } + ], + "description": "MongoDB driver library", + "homepage": "https://jira.mongodb.org/browse/PHPLIB", + "keywords": [ + "database", + "driver", + "mongodb", + "persistence" + ], + "time": "2020-02-04T18:16:35+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/rds-mongodb/php/index.php b/rds-mongodb/php/index.php new file mode 100644 index 0000000..5ecfe78 --- /dev/null +++ b/rds-mongodb/php/index.php @@ -0,0 +1,23 @@ +$db_name->fc_col; + + $result = $collection->insertOne(['DEMO' => 'FC', 'MSG' => 'Hello FunctionCompute For MongoDB']); + echo "Inserted with Object ID '{$result->getInsertedId()}'", "\n"; + $cursor = $collection->find(['DEMO' => 'FC']); + $result = ''; + foreach ($cursor as $entry) { + echo json_encode($entry->getArrayCopy()), "\n"; + $result = $result . json_encode($entry->getArrayCopy()) . "\n"; + } + return $result; +} \ No newline at end of file diff --git a/rds-mongodb/python/index.py b/rds-mongodb/python/index.py new file mode 100644 index 0000000..fbb3209 --- /dev/null +++ b/rds-mongodb/python/index.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +import os +from pymongo import MongoClient + +def handler(event, context): + dbName = os.environ['MONGO_DATABASE'] + url = os.environ['MONGO_URL'] + + client = MongoClient(url) + + col = client[dbName]['fc_col'] + col.insert_one(dict(DEMO="FC", MSG="Hello FunctionCompute For MongoDB")) + doc=col.find_one(dict(DEMO="FC")) + print ('find documents:'+ str(doc)) + return str(doc) \ No newline at end of file diff --git a/rds-mongodb/python/requirements.txt b/rds-mongodb/python/requirements.txt new file mode 100644 index 0000000..8c7d698 --- /dev/null +++ b/rds-mongodb/python/requirements.txt @@ -0,0 +1 @@ +pymongo \ No newline at end of file diff --git a/rds-mongodb/template.override.yml b/rds-mongodb/template.override.yml new file mode 100644 index 0000000..e0dce51 --- /dev/null +++ b/rds-mongodb/template.override.yml @@ -0,0 +1,24 @@ +ROSTemplateFormatVersion: '2015-09-01' +Transform: 'Aliyun::Serverless-2018-04-03' +Resources: + fc-mongo: + nodejs: + Properties: + EnvironmentVariables: + MONGO_DATABASE: admin + MONGO_URL: mongodb://mongoadmin:secret@host.docker.internal:27017/admin + python: + Properties: + EnvironmentVariables: + MONGO_DATABASE: admin + MONGO_URL: mongodb://mongoadmin:secret@host.docker.internal:27017/admin + java: + Properties: + EnvironmentVariables: + MONGO_DATABASE: admin + MONGO_URL: mongodb://mongoadmin:secret@host.docker.internal:27017/admin + php: + Properties: + EnvironmentVariables: + MONGO_DATABASE: admin + MONGO_URL: mongodb://mongoadmin:secret@host.docker.internal:27017/admin \ No newline at end of file diff --git a/rds-mongodb/template.yml b/rds-mongodb/template.yml new file mode 100644 index 0000000..3680f0d --- /dev/null +++ b/rds-mongodb/template.yml @@ -0,0 +1,241 @@ +ROSTemplateFormatVersion: '2015-09-01' +Transform: 'Aliyun::Serverless-2018-04-03' +Parameters: + Password: + Type: String + Default: masteruser_123 + VpcName: + Type: String + Default: myVPC + CidrBlock: + Type: String + Default: 192.168.0.0/16 + DBName: + Type: String + Default: admin +Mappings: + RegionMap: + cn-shanghai: + ZoneId: cn-shanghai-e + cn-hangzhou: + ZoneId: cn-hangzhou-g + cn-qingdao: + ZoneId: cn-qingdao-c + cn-beijing: + ZoneId: cn-beijing-c + cn-zhangjiakou: + ZoneId: cn-zhangjiakou-b + cn-huhehaote: + ZoneId: cn-huhehaote-a + cn-shenzhen: + ZoneId: cn-shenzhen-d + cn-hongkong: + ZoneId: cn-hongkong-c + ap-southeast-1: + ZoneId: ap-southeast-1a + ap-southeast-2: + ZoneId: ap-southeast-2a + ap-southeast-5: + ZoneId: ap-southeast-5a + ap-northeast-1: + ZoneId: ap-northeast-1a + eu-central-a: + ZoneId: eu-central-a + us-west-1: + ZoneId: us-west-1a + us-east-1: + ZoneId: us-east-1a + ap-south-1: + ZoneId: ap-south-1a +Resources: + EcsVpc: + Type: 'ALIYUN::ECS::VPC' + Properties: + CidrBlock: + Ref: CidrBlock + VpcName: + Ref: VpcName + VSwitch1: + Type: 'ALIYUN::ECS::VSwitch' + Properties: + VpcId: + 'Fn::GetAtt': + - EcsVpc + - VpcId + ZoneId: + 'Fn::FindInMap': + - RegionMap + - Ref: 'ALIYUN::Region' + - ZoneId + CidrBlock: 192.168.1.0/24 + VSwitchName: VSwitch1-x + SecurityGroup_default: + Type: 'ALIYUN::ECS::SecurityGroup' + Properties: + VpcId: + 'Fn::GetAtt': + - EcsVpc + - VpcId + SecurityGroupName: AliyunECS_SecurityGroup1 + SecurityGroupIngress: + - PortRange: 22/22 + Priority: 1 + SourceCidrIp: 0.0.0.0/0 + IpProtocol: tcp + NicType: internet + SecurityGroupEgress: + - PortRange: '-1/-1' + Priority: 1 + IpProtocol: all + DestCidrIp: 0.0.0.0/0 + NicType: internet + - PortRange: '-1/-1' + Priority: 1 + IpProtocol: all + DestCidrIp: 0.0.0.0/0 + NicType: intranet + Mongo: + Type: 'ALIYUN::MONGODB::Instance' + Properties: + EngineVersion: '4.2' + DBInstanceClass: dds.mongo.mid + DBInstanceStorage: 10 + VpcId: + 'Fn::GetAtt': + - EcsVpc + - VpcId + ZoneId: + 'Fn::FindInMap': + - RegionMap + - Ref: 'ALIYUN::Region' + - ZoneId + VSwitchId: + 'Fn::GetAtt': + - VSwitch1 + - VSwitchId + SecurityIPArray: '192.168.1.0/24' + AccountPassword: + Ref: Password + ChargeType: PostPaid + NetworkType: VPC + DatabaseNames: + Ref: DBName + fc-mongo: + Type: 'Aliyun::Serverless::Service' + Properties: + Description: This is FC service for mongo + VpcConfig: + VpcId: + 'Fn::GetAtt': + - EcsVpc + - VpcId + VSwitchIds: + - 'Fn::GetAtt': + - VSwitch1 + - VSwitchId + SecurityGroupId: + 'Fn::GetAtt': + - SecurityGroup_default + - SecurityGroupId + java: + Type: 'Aliyun::Serverless::Function' + Properties: + Handler: 'example.App::handleRequest' + Runtime: java8 + Timeout: 60 + MemorySize: 512 + CodeUri: java/ + EnvironmentVariables: + MONGO_URL: + 'Fn::GetAtt': + - Mongo + - ConnectionURI + MONGO_DATABASE: + Ref: DBName + nodejs: + Type: 'Aliyun::Serverless::Function' + Properties: + Handler: index.handler + Runtime: nodejs10 + CodeUri: nodejs/ + MemorySize: 128 + Timeout: 15 + EnvironmentVariables: + MONGO_URL: + 'Fn::GetAtt': + - Mongo + - ConnectionURI + MONGO_DATABASE: + Ref: DBName + python: + Type: 'Aliyun::Serverless::Function' + Properties: + Handler: 'index.handler' + Runtime: python3 + Timeout: 10 + MemorySize: 128 + CodeUri: python/ + EnvironmentVariables: + MONGO_URL: + 'Fn::GetAtt': + - Mongo + - ConnectionURI + MONGO_DATABASE: + Ref: DBName + PYTHONPATH: /code/.fun/python/lib/python/site-packages + php: + Type: 'Aliyun::Serverless::Function' + Properties: + Handler: 'index.handler' + Runtime: php7.2 + Timeout: 10 + MemorySize: 128 + CodeUri: 'php' + EnvironmentVariables: + MONGO_URL: + 'Fn::GetAtt': + - Mongo + - ConnectionURI + MONGO_DATABASE: + Ref: DBName + dotnetcore: + Type: 'Aliyun::Serverless::Function' + Properties: + Handler: 'mongodb::mongodb.MongoDBHandler::Handler' + Runtime: dotnetcore2.1 + Timeout: 60 + MemorySize: 512 + CodeUri: ./dotnetcore + EnvironmentVariables: + MONGO_URL: + 'Fn::GetAtt': + - Mongo + - ConnectionURI + MONGO_DATABASE: + Ref: DBName +Outputs: + VpcId: + Value: + 'Fn::GetAtt': + - EcsVpc + - VpcId + SecurityGroupId: + Value: + 'Fn::GetAtt': + - SecurityGroup_default + - SecurityGroupId + VSwitchId: + Value: + 'Fn::GetAtt': + - VSwitch1 + - VSwitchId + MongoInstanceId: + Value: + 'Fn::GetAtt': + - Mongo + - DBInstanceId + MongoConnectionURI: + Value: + 'Fn::GetAtt': + - Mongo + - ConnectionURI \ No newline at end of file diff --git a/rds-redis/python/index.py b/rds-redis/python/index.py index c74eba0..de0a646 100644 --- a/rds-redis/python/index.py +++ b/rds-redis/python/index.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -import os,sys +import os import redis def handler(event, context): diff --git a/rds-redis/template.override.yml b/rds-redis/template.override.yml index d326ac6..57ebc64 100644 --- a/rds-redis/template.override.yml +++ b/rds-redis/template.override.yml @@ -1,7 +1,7 @@ ROSTemplateFormatVersion: '2015-09-01' Transform: 'Aliyun::Serverless-2018-04-03' Resources: - redis: + fc-redis: nodejs: Properties: EnvironmentVariables: