Glavin001 / sails-multitenancy-example

Example application using Sails (& Waterline) Multitenancy support. (Unmaintained, proof-of-concept)
MIT License
14 stars 4 forks source link

Not working on sails-mongo #1

Closed chipgata closed 9 years ago

chipgata commented 9 years ago

I try example with sails-mongo: 1.Install sails-mongo

sudo npm install sails-mongo

2.package.json

{
  "name": "sails-multitenancy-example",
  "private": true,
  "version": "0.0.0",
  "description": "a Sails application",
  "keywords": [],
  "dependencies": {
    "bcryptjs": "^2.1.0",
    "ejs": "~0.8.4",
    "grunt": "0.4.2",
    "grunt-contrib-clean": "~0.5.0",
    "grunt-contrib-coffee": "~0.10.1",
    "grunt-contrib-concat": "~0.3.0",
    "grunt-contrib-copy": "~0.5.0",
    "grunt-contrib-cssmin": "~0.9.0",
    "grunt-contrib-jst": "~0.6.0",
    "grunt-contrib-less": "0.11.1",
    "grunt-contrib-uglify": "~0.4.0",
    "grunt-contrib-watch": "~0.5.3",
    "grunt-sails-linker": "~0.9.5",
    "grunt-sync": "~0.0.4",
    "include-all": "~0.1.3",
    "passport": "^0.2.1",
    "passport-local": "^1.0.0",
    "rc": "~0.5.0",
    "sails": "git+https://github.com/Glavin001/sails.git",
    "sails-disk": "~0.10.0",
    "sails-generate-auth": "^0.2.0",
    "validator": "^3.27.0"
  },
  "scripts": {
    "start": "node app.js",
    "debug": "node debug app.js"
  },
  "main": "app.js",
  "repository": {
    "type": "git",
    "url": "git://github.com/glavin/sails-multitenancy-example2.git"
  },
  "author": "glavin",
  "license": ""
}

3.connection.js

someMongodbServer: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    // user: 'username',
    // password: 'password',
    // database: 'your_mongo_db_name_here'

    // === Multitenancy support ===
    isMultiTenant: true, // Enable Multi-Tenancy feature
    availableTenants: ['localhost:1337', 'tenant2', 'abc', 'xyz'], // Tenants that pass validation
    configForTenant: function(tenantId, config, cb) { // For Waterline
        // console.log('configForTenant', tenantId);
        // Validate Tenant
        if (config.availableTenants.indexOf(tenantId) !== -1) {
            // Tenant is allowed
            console.log(tenantId);
            config.fileName = tenantId;
            return cb(null, config);
        } else {
            // Tenant is not allowed
            return cb(new Error("Invalid tenant " + tenantId + "!"));
        }
    }
  },
  1. file policy tenant.js
module.exports = function(req, res, next) {
    var tenantId = req.headers.host;
    tenantId = tenantId.substring(0, tenantId.indexOf('.'));
    //tanantId = tenantId.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'');
    // Important: `req.session.tenant` is where you attach the tenant identifer
    req.session.tenant = tenantId;
    //console.log(req.session.tenant);
    // Policy complete, continue...
    next();
};

5.I use Nginx working reverse proxy mode for 2 host abc.xxx.com, xyz.xxx.com with config:

 location / {
            proxy_pass http://localhost:1337;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

Every things working ok, but database do not create and save, it not working.

Thanks you!

Glavin001 commented 9 years ago

I'll look into this now and possibly add some Unit Tests.

Glavin001 commented 9 years ago

I notice that you use config.fileName = tenantId; in your connections.js which is only applicable for when using sails-disk and not sails-mongo. This is likely the problem.

Glavin001 commented 9 years ago

Please see this branch for a working and tested demo using sails-mongo. Thanks for your interest and support.

See https://github.com/Glavin001/sails-multitenancy-example/tree/sails-mongo