Last active
April 4, 2016 14:51
-
-
Save aregee/ace3b798bb4b7f8c8bb2e3f318087dd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var orm = appRequire('orm'); | |
var _ = require('lodash'); | |
var db = orm.table; | |
module.exports = function(teteris) { | |
teteris('socialAuth').on('error', (e) => { | |
console.log('WORKER ERROR', e); | |
}); | |
teteris('socialAuth').on('request', (inp, rep) => { | |
console.log("Received worker: ", inp); | |
var socialtype = inp.args.data.social.type; | |
var socialprofile = inp.args.data.social.profile; | |
var token = inp.args.token; | |
db("users").find("email", socialprofile.email) | |
.then((user) => { | |
if (user) { | |
var data = {}; | |
data = { | |
social_accounts: {}, | |
imported_data: {}, | |
full_name: socialprofile.first_name, | |
email: socialprofile.email, | |
username: socialprofile.email | |
}; | |
var social = {}; | |
social[socialtype] = socialprofile.id; | |
var socialImported = {}; | |
socialImported[socialtype] = socialprofile; | |
_.extend(data, { | |
social_accounts: social | |
}, { | |
imported_data: socialImported | |
}); | |
_.merge(user, data); | |
var userData = user; | |
return db("users").update(user.id, userData); | |
} else { | |
return db("users").createBySocialId({ | |
social_type: socialtype, | |
social_id: socialprofile.id, | |
profile: socialprofile | |
}); | |
} | |
}).then((user) => { | |
orm.cache.set(token, user.id, sessionlifetime) | |
.then(function(contents) { | |
user.access_token = token; | |
rep.end(user); | |
}); | |
}).catch((err) => { | |
throw err; | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
|-- config | |
| |-- config.js | |
| |-- index.js | |
| `-- properties.js | |
|-- engine | |
| |-- clustering.js | |
| `-- index.js | |
|-- orm | |
| |-- index.js | |
| `-- tables | |
| |-- product_category.js | |
| |-- product.js | |
| |-- products.js | |
| |-- cities.js | |
| |-- countries.js | |
| |-- index.js | |
| `-- users.js | |
|-- teteris | |
| |-- CoreService.js | |
| |-- index.js | |
| |-- services | |
| | |-- dbcache.js | |
| | |-- dbinsert.js | |
| | |-- dbsocialauth.js | |
| | |-- dbstats.js | |
| | |-- dbtableitem.js | |
| | |-- dbtables.js | |
| | `-- index.js | |
| |-- teterisBroker.js | |
| |-- teteris.js | |
| `-- teterisWorker.js | |
`-- workers | |
|-- dbcache.js | |
|-- dbinzert.js | |
|-- dbstats.js | |
|-- dbtableitem.js | |
|-- dbtables.js | |
|-- index.js | |
`-- teterisSociallogin.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var Promise = require('bluebird'); | |
var _ = require('lodash'); | |
var config = appRequire('config'); | |
var Service = require('./teterisWorker'); | |
var Broker = require('./teterisBroker'); | |
var CoreService = require('./CoreService'); | |
module.exports = teteris; | |
function teteris(service, args) { | |
var worker = teteris.services[service]; | |
return worker; | |
}; | |
teteris.loadServices = function() { | |
throw 'override loadServices, and load services in it'; | |
}; | |
teteris.core = require('pigato'); | |
teteris.listen = function(opts) { | |
teteris.broker = Broker(teteris.core, opts); | |
return teteris.broker.start(); | |
}; | |
teteris.coreServices = function(opts) { | |
teteris.coreServices = CoreService(teteris.core, opts); | |
return teteris.coreServices.start(); | |
}; | |
teteris.setup = function() { | |
teteris.services = {}; | |
teteris.Service = function(opts) { | |
teteris.services[opts.serviceName] = Service(teteris, opts); | |
return teteris; | |
}; | |
teteris.loadServices(teteris); | |
// return a promise which ensures that all registered workers for services are started | |
return Promise.all(_.map(teteris.services, function(service) { | |
return service.start(); | |
})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment