Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace Np\Api\Middleware;
- use Closure;
- use Illuminate\Foundation\Application;
- use October\Rain\Support\Facades\Flash;
- use Illuminate\Support\Facades\DB;
- use Np\Structure\Models\Site;
- use Np\Structure\Models\Domain;
- use Np\Structure\Models\Cluster;
- use Np\Structure\Classes\DatabaseConnection;
- class InitializedTenantForApi
- {
- /**
- * The Laravel Application
- *
- * @var Application
- */
- protected $app;
- /**
- * Create a new middleware instance.
- *
- * @param Application $app
- * @return void
- */
- public function __construct()
- {
- //$this->app = $app;
- }
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle($request, Closure $next)
- {
- $host = $request->input('host');
- if (!isset($host) || is_null($host)) {
- return response(['error' => 'host parameter is missing.'], 400);
- }
- $this->initDB($host);
- return $next($request);
- }
- public function initDB($host)
- {
- // find domain
- $domain = Domain::where('fqdn', $host)->first();
- if(is_null($domain) || !isset($domain))
- return response(['error' => 'host not found.']);
- $key = 'database.connections.tenant';
- $db = config($key);
- $db['host'] = $domain->site->cluster->host;
- $db['username'] = $domain->site->cluster->username;
- $db['password'] = $domain->site->cluster->password;
- $db['database'] = $domain->site->db->id;
- config([$key => $db]);
- // $notices = Notice::all();
- // echo '<pre>';
- // print_r($notices);
- // echo '</pre>';
- // exit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement