Skip to content

Commit

Permalink
Static homepage and file proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasRos committed Sep 16, 2019
1 parent e0acf68 commit 06fcc98
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
54 changes: 40 additions & 14 deletions classes/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,48 @@ private function executeJsonRPC(RequestInterface $request) {
*/
public function execute(RequestInterface $request) {
$path = rtrim($request->getUri()->getBasePath().$request->getUri()->getPath(), '/');
switch ($path) {
case "":
case "/":
// Display homepage
$file = ($this->container
->get(InteractiveRunController::class)
->isEnabled()) ? 'app.html' : 'app_disabled.html';

return $this->generateResponse(file_get_contents(__DIR__.'/../static/'.$file));
case "/run":
// Run interactive code request
return $this->container
->get(InteractiveRunController::class)
->handle($request, $this);
case "/uploadTestenv":
// Upload into test environment (if enabled)
return $this->container
->get(UploadController::class)
->handle($request);
default:
if (file_exists(__DIR__.'/../static'.$path)) {
// Proxy for static files
$filename = realpath(__DIR__.'/../static'.$path);
$response = (new Response)->write(file_get_contents($filename));
switch (pathinfo($filename, PATHINFO_EXTENSION)) {
case "css":
return $response->withHeader('Content-Type', 'text/css');
case "js":
return $response->withHeader('Content-Type', 'application/javascript');
default:
return $response;
}

}

if ($path == '/uploadTestenv') {
return $this->container
->get(UploadController::class)
->handle($request);
} elseif ($path == '/run') {
return $this->container
->get(InteractiveRunController::class)
->handle($request, $this);
}

$coid = COIDParser::fromString(substr($path, 1));
$this->loadRunClass($coid, $request);
$coid = COIDParser::fromString(substr($path, 1));
$this->loadRunClass($coid, $request);

return $this->getAuthenticationMiddleware()
->process($request, $this);
// Process a standard request for a phpMAE class
return $this->getAuthenticationMiddleware()
->process($request, $this);
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions classes/InteractiveRunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function __construct(ClassRepository $classRepository,
$this->container = $container;
}

public function isEnabled() {
return ($this->container->has('interactive_run')
&& $this->container->get('interactive_run') === true);
}

private function createTemporaryClassObject() {
$graph = new Graph;
$type = new Node($graph, 'coid://phpmae.cloudobjects.io/Class');
Expand All @@ -35,8 +40,7 @@ private function createTemporaryClassObject() {
}

public function handle(RequestInterface $request, Engine $engine) {
if (!$this->container->has('interactive_run')
|| $this->container->get('interactive_run') !== true)
if (!$this->isEnabled())
throw new PhpMAEException("This is not an environment with interactive run enabled.");

if ($request->getMethod() != 'POST')
Expand Down
11 changes: 11 additions & 0 deletions static/app_disabled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>phpMAE</title>
<link rel="stylesheet" type="text/css" href="css/pure-min.css" />
</head>
<body>
<h1>phpMAE</h1>
<p>This is a <a href="https://github.com/CloudObjects/phpMAE">phpMAE</a> instance.</p>
</body>
</html>

0 comments on commit 06fcc98

Please sign in to comment.