-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added MongoDB example #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
keys.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## MongoDB on Google App Engine | ||
|
||
> [MongoDB][1] is an open source (AGPL licensed), NoSQL document database. | ||
|
||
Read the [MongoDB on App Engine Tutorial][2] for how to run and deploy this sample | ||
app. | ||
|
||
You can also read the [node_mongodb documentation][3]. | ||
|
||
[1]: http://mongodb.org/ | ||
[2]: https://cloud.google.com/nodejs/resources/databases/mongo | ||
[3]: http://mongodb.github.io/node-mongodb-native/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2015, 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. | ||
# | ||
# [START app_yaml] | ||
runtime: nodejs | ||
vm: true | ||
env_variables: | ||
PORT: 8080 | ||
# [END app_yaml] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "appengine-mongo", | ||
"description": "An example of using MongoDB with Node.js on Google App Engine.", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"engines": { | ||
"node": "~0.12.7" | ||
}, | ||
"scripts": { | ||
"start": "node server.js", | ||
"deploy": "gcloud preview app deploy app.yaml" | ||
}, | ||
"dependencies": { | ||
"nconf": "^0.8.0", | ||
"mongodb": "^2.0.48" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2015, 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. | ||
|
||
'use strict'; | ||
|
||
var mongodb = require('mongodb'); | ||
var http = require('http'); | ||
var nconf = require('nconf'); | ||
|
||
// read in keys and secrets. You can store these in a variety of ways. | ||
// I like to use a keys.json file that is in the .gitignore file, | ||
// but you can also store them in environment variables | ||
nconf.argv().env().file('keys.json'); | ||
|
||
// Connect to a MongoDB server provisioned over at | ||
// MongoLab. See the README for more info. | ||
var uri = 'mongodb://' + | ||
nconf.get('mongoUser') + ':' + | ||
nconf.get('mongoPass') + '@' + | ||
nconf.get('mongoHost') + ':' + | ||
nconf.get('mongoPort') + '/' + | ||
nconf.get('mongoDatabase'); | ||
|
||
mongodb.MongoClient.connect(uri, function(err, db) { | ||
|
||
if (err) throw err; | ||
|
||
// Create a simple little server. | ||
http.createServer(function(req, res) { | ||
|
||
console.log("test"); | ||
console.log(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably don't need these :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, remove these console.logs |
||
|
||
// Track every IP that has visited this site | ||
var collection = db.collection('IPs'); | ||
|
||
var ip = {"address": req.connection.remoteAddress}; | ||
|
||
collection.insert(ip, function(err, result) { | ||
|
||
if (err) throw err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that'll crash the process, won't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failing to connect to Mongo on startup should crash the process, at least for this sample. The is consistent with how we've done things in a few other places. |
||
|
||
// push out a range | ||
var iplist = ''; | ||
collection.find().toArray(function(err, data) { | ||
if (err) throw err; | ||
console.log('listing data...\n'); | ||
data.forEach(function(ip) { | ||
console.log('IP: ' + ip.address + '\n'); | ||
iplist += ip.address + '; '; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation |
||
}); | ||
|
||
res.writeHead(200, { | ||
'Content-Type': 'text/plain' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more indendation |
||
}); | ||
res.end(iplist); | ||
}); | ||
}) | ||
}).listen(process.env.PORT || 8080);; | ||
console.log('started web process'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of wed, 4.2.1 is now the default version on the image. Next wed it will be 4.2.2. Maybe more of a @jmdobry question, but we should do a pass to bring this all up to 4.x.