Skip to content
Browse files

moved the examples into their own directories, and made the mongo dri…

…ver overwrite an existing key instead of adding a new one. Also trying to fix the heroku port issue.
  • Loading branch information...
1 parent 218322c commit 9abf02bc880154b44b305e9c83e026132ac19c31 @treygriffith committed Mar 18, 2013
View
40 drivers/mongo.js
@@ -12,7 +12,7 @@ mongooseTypes.loadTypes(mongoose, 'email');
*/
function MongoKDb(url, collectionName) {
- mongoose.connect(url);
+ this.mongoose = mongoose.connect(url);
var key = mongoose.Schema({
uid: mongoose.SchemaTypes.Email,
@@ -72,25 +72,41 @@ MongoKDb.prototype.find = function(domain, callback) {
MongoKDb.prototype.add = function(email, keytext, callback) {
var parts = email.split('@'),
user = parts[0],
- domain = parts[1];
+ domain = parts[1],
+ Model = this.model;
- var key = new this.model({
- uid:email,
- domain:domain,
- user:user,
- keytext:keytext
- });
+ this.collection.findOne({uid:email}, function(err, key) {
- key.save(function(err, key) {
if(err) {
callback(err);
return;
}
- callback(null, {
- uid: key.email,
- keytext: key.keytext
+
+ if(!key) {
+ key = new Model({
+ uid:email,
+ domain:domain,
+ user:user
+ });
+ }
+
+ key.keytext = keytext;
+
+ key.save(function(err, key) {
+ if(err) {
+ callback(err);
+ return;
+ }
+ callback(null, {
+ uid: key.email,
+ user: user,
+ domain: domain,
+ keytext: key.keytext
+ });
});
+
});
+
};
View
0 example/app.js → example/fs/app.js
File renamed without changes.
View
0 example/keys/example.com/alice → example/fs/keys/example.com/alice
File renamed without changes.
View
14 example/mongo/app.js
@@ -0,0 +1,14 @@
+var aks = require('../');
+
+var db = require('./config/db');
+
+var mongoUrl = 'mongodb://' + db.user + ':' + db.pass + '@' + db.host + ':' + db.port + '/' + db.name;
+
+var mongoKeyDatabase = new aks.drivers.Mongo(mongoUrl, 'keys'); // the fs driver is for demonstration only
+
+var server = new aks.Server(mongoKeyDatabase, {
+ trustProxy: true
+});
+
+server.listen(process.env.PORT || 1337);
+console.log('Authoritative Key Server listening on port ' + (process.env.PORT || 1337));
View
4 index.js
@@ -180,8 +180,8 @@ function AKS(db, options) {
return this;
}
-AKS.prototype.listen = function(port, args) {
- if(typeof port === 'number') {
+AKS.prototype.listen = function(port, host, callback) {
+ if(parseInt(port,10).toString() === port) {
args = Array.prototype.slice.call(arguments, 1);
} else {
port = 11371;

0 comments on commit 9abf02b

Please sign in to comment.
Something went wrong with that request. Please try again.