Skip to content

Commit

Permalink
Serve Vue app in internal_dashboard.blade.php from public/build folder
Browse files Browse the repository at this point in the history
  • Loading branch information
mechelon committed Sep 20, 2024
1 parent 8caace4 commit 76250d3
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Factory as LoopFactory;
use React\EventLoop\LoopInterface;
use Symfony\Component\Routing\Route;

class Factory
{
Expand Down Expand Up @@ -144,6 +145,8 @@ protected function addRoutes()
{
$this->router->get('/', DashboardController::class);

$this->router->addPublicFilesystem();

$this->router->get('/api/tunnels', GetTunnelsController::class);
$this->router->post('/api/tunnel', CreateTunnelController::class);
$this->router->get('/api/logs', LogController::class);
Expand Down
24 changes: 19 additions & 5 deletions app/Client/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ public function handle(Request $request, ConnectionInterface $httpConnection)
{
$httpConnection->send(respond_html(
$this->getBlade($httpConnection, 'client.internal_dashboard', [
'user' => Client::$user,
'subdomains' => Client::$subdomains,
'max_logs' => config()->get('expose.max_logged_requests', 10),
])
'user' => Client::$user,
'subdomains' => Client::$subdomains,
'max_logs' => config()->get('expose.max_logged_requests', 10),

));
'jsFile' => $this->getJsFilePath(),
'cssFile' => $this->getCssFilePath(),
])
));
}

private function getJsFilePath() {
return "/files/build/internal-dashboard/assets/" . collect(scandir(app()->basePath() . "/public/build/internal-dashboard/assets/"))->filter(function($file) {
return str($file)->startsWith("index-") && str($file)->endsWith(".js");
})->first();
}

private function getCssFilePath() {
return "/files/build/internal-dashboard/assets/" . collect(scandir(app()->basePath() . "/public/build/internal-dashboard/assets/"))->filter(function($file) {
return str($file)->startsWith("index-") && str($file)->endsWith(".css");
})->first();
}
}
41 changes: 41 additions & 0 deletions app/Client/Http/Controllers/FileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Client\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;
use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\Response;

class FileController extends Controller
{
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$file = $request->get("path");

$filePath = app()->basePath() . "/public/" . $file;

if(!file_exists($filePath)) {
$httpConnection->send(Message::toString(new Response(
404,
['Content-Type' => 'text/html'],
"File not found"
)));
return;
}

$file = file_get_contents($filePath);
$contentType = mime_content_type($filePath);

if(str($filePath)->endsWith(".css")) {
$contentType = "text/css";
}

$httpConnection->send(Message::toString(new Response(
200,
['Content-Type' => $contentType],
$file
)));
}
}
9 changes: 9 additions & 0 deletions app/Http/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use App\Client\Http\Controllers\FileController;

class RouteGenerator
{
Expand Down Expand Up @@ -55,6 +56,14 @@ public function addSymfonyRoute(string $name, Route $route)
$this->routes->add($name, $route);
}

public function addPublicFilesystem() {
$this->routes->add('get-files', new Route(
'/files/{path}',
['_controller' => FileController::class],
['path' => '.*']
));
}

protected function getRoute(string $method, string $uri, $action, string $condition = ''): Route
{
return new Route($uri, ['_controller' => app($action)], [], [], null, [], [$method], $condition);
Expand Down
1 change: 1 addition & 0 deletions public/build/internal-dashboard/assets/index-8zj_ClGA.css

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions public/build/internal-dashboard/assets/index-CZHspyA5.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions public/build/internal-dashboard/build/assets/index-mleR6s9-.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions public/build/internal-dashboard/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + TS</title>
<script type="module" crossorigin src="/assets/index-mleR6s9-.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CusWAS5C.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
1 change: 1 addition & 0 deletions public/build/internal-dashboard/build/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions public/build/internal-dashboard/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Expose Dashboard</title>
<script type="module" crossorigin src="/assets/index-CZHspyA5.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-8zj_ClGA.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
1 change: 1 addition & 0 deletions public/build/internal-dashboard/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 9 additions & 4 deletions resources/views/client/internal_dashboard.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link rel="stylesheet" href="{{ asset('build/assets/[your-css-file].css') }}">
<title>Internal Dashboard</title>

<link href="{{ $cssFile }}" rel="stylesheet">
</head>

<body>
<div id="vue-dashboard"></div>
<div id="internalDashboard">
</div>

<script src="{{ asset('build/assets/[your-js-file].js') }}"></script>
<script src="{{ $jsFile }}"></script>
</body>

</html>

0 comments on commit 76250d3

Please sign in to comment.