Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. <?php namespace Np\Api\Middleware;
  2.  
  3. use Closure;
  4. use Illuminate\Foundation\Application;
  5. use October\Rain\Support\Facades\Flash;
  6. use Illuminate\Support\Facades\DB;
  7. use Np\Structure\Models\Site;
  8. use Np\Structure\Models\Domain;
  9. use Np\Structure\Models\Cluster;
  10. use Np\Structure\Classes\DatabaseConnection;
  11.  
  12. class InitializedTenantForApi
  13. {
  14. /**
  15. * The Laravel Application
  16. *
  17. * @var Application
  18. */
  19. protected $app;
  20.  
  21. /**
  22. * Create a new middleware instance.
  23. *
  24. * @param Application $app
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. //$this->app = $app;
  30. }
  31. /**
  32. * Handle an incoming request.
  33. *
  34. * @param \Illuminate\Http\Request $request
  35. * @param \Closure $next
  36. * @return mixed
  37. */
  38.  
  39. public function handle($request, Closure $next)
  40. {
  41. $host = $request->input('host');
  42. if (!isset($host) || is_null($host)) {
  43. return response(['error' => 'host parameter is missing.'], 400);
  44. }
  45. $this->initDB($host);
  46. return $next($request);
  47. }
  48.  
  49. public function initDB($host)
  50. {
  51.  
  52. // find domain
  53. $domain = Domain::where('fqdn', $host)->first();
  54.  
  55. if(is_null($domain) || !isset($domain))
  56. return response(['error' => 'host not found.']);
  57.  
  58. $key = 'database.connections.tenant';
  59. $db = config($key);
  60. $db['host'] = $domain->site->cluster->host;
  61. $db['username'] = $domain->site->cluster->username;
  62. $db['password'] = $domain->site->cluster->password;
  63. $db['database'] = $domain->site->db->id;
  64. config([$key => $db]);
  65.  
  66. // $notices = Notice::all();
  67.  
  68. // echo '<pre>';
  69. // print_r($notices);
  70. // echo '</pre>';
  71. // exit;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement