Skip to content

Commit

Permalink
Merge pull request #66 from Moros1138/make-ready-for-subpath-and-use-…
Browse files Browse the repository at this point in the history
…redis

Make ready for subpath and use redis
  • Loading branch information
Moros1138 authored May 17, 2024
2 parents 1b90db0 + c564f21 commit ad2ca8e
Show file tree
Hide file tree
Showing 45 changed files with 21,120 additions and 82 deletions.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.git
.env
database/database.sqlite
node_modules
vendor
docker-compose.yml
storage/app/workspaces
storage/app/compilerCache
storage/logs/laravel.log
tests/Feature/test_*
15 changes: 8 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_NAME=PGEtinker
APP_ENV=local
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_TIMEZONE=UTC
Expand All @@ -17,11 +17,12 @@ COMPILER_ENVIRONMENT=nsjail
COMPILER_TIMEOUT=10
COMPILER_CODE_PROCESSING_TIMEOUT=5
COMPILER_REMOTE_INCLUDE_CACHING=true
COMPILER_NSJAIL_CFG=/third_party/nsjail-emscripten.cfg

BCRYPT_ROUNDS=12

LOG_CHANNEL=stack
LOG_STACK=single
LOG_STACK=errorlog
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

Expand All @@ -40,11 +41,11 @@ QUEUE_CONNECTION=database
CACHE_STORE=database
CACHE_PREFIX=

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
REDIS_URL=
REDIS_HOST=127.0.0.1
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_PORT=6379

VITE_APP_NAME="${APP_NAME}"

Expand Down
16 changes: 8 additions & 8 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ APP_FAKER_LOCALE=en_US
APP_MAINTENANCE_DRIVER=file
APP_MAINTENANCE_STORE=database

COMPILER_CACHING=true
COMPILER_CACHING=false
COMPILER_ENVIRONMENT=nsjail
COMPILER_TIMEOUT=10
COMPILER_CODE_PROCESSING_TIMEOUT=5
COMPILER_REMOTE_INCLUDE_CACHING=true
COMPILER_REMOTE_INCLUDE_CACHING=false
COMPILER_NSJAIL_CFG=/third_party/nsjail-emscripten-ci.cfg


BCRYPT_ROUNDS=12

LOG_CHANNEL=stack
LOG_STACK=single
LOG_STACK=errorlog
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

Expand All @@ -42,11 +42,11 @@ QUEUE_CONNECTION=database
CACHE_STORE=database
CACHE_PREFIX=

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
REDIS_URL=
REDIS_HOST=127.0.0.1
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_PORT=6379

VITE_APP_NAME="${APP_NAME}"

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. Each batch

It is a summary of changes that would be pertinent to the end user of the PGEtinker website. For a comprehensive history of changes made to the project, please refer to the repository's commit history.

## 2024-05-17

- Added support to deploy on subpaths
- Added updated version of PGE, Extensions, and Utlities

## 2024-05-14

- Added view counter to shared codes
Expand Down
47 changes: 25 additions & 22 deletions app/Http/Controllers/CodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -37,7 +38,7 @@ function Share(Request $request)
$share = Code::where("hash", $result["hash"])->first();
if($share != null)
{
$result["shareURL"] = $request->root() . "/s/" . $share->slug;
$result["shareURL"] = env("APP_URL") . "/s/" . $share->slug;
unset($result["hash"]);

return response($result, $result["statusCode"])->header("Content-Type", "application/json");
Expand Down Expand Up @@ -67,7 +68,7 @@ function Share(Request $request)

if($share->save())
{
$result["shareURL"] = $request->root() . "/s/" . $slug;
$result["shareURL"] = env("APP_URL") . "/s/" . $slug;
unset($result["hash"]);

return response($result, $result["statusCode"])->header("Content-Type", "application/json");
Expand Down Expand Up @@ -119,26 +120,21 @@ function compileCode($code)

if(env("COMPILER_CACHING", false))
{
if(Storage::directoryMissing("compilerCache"))
{
Storage::makeDirectory("compilerCache");
}

if(Storage::directoryMissing("remoteIncludeCache"))
{
Storage::makeDirectory("remoteIncludeCache");
}

if(Storage::fileExists("compilerCache/{$hashedCode}"))
$cachedCode = Redis::get("compiler_{$hashedCode}");

if(isset($cachedCode))
{
Log::debug("Compile: cache hit", ["hashedCode" => $hashedCode]);

$html = Storage::get("compilerCache/{$hashedCode}");

$compiler = new Compiler();
$compiler->deserialize($cachedCode);

return [
"statusCode" => 200,
"statusCode" => $compiler->getStatus(),
"hash" => $hashedCode,
"html" => $html,
"html" => $compiler->getHtml(),
"stdout" => $compiler->getOutput(),
"stderr" => $compiler->getErrorOutput(),
];
}

Expand Down Expand Up @@ -167,10 +163,11 @@ function compileCode($code)

if($compiler->build())
{
Storage::put(
"compilerCache/{$hashedCode}",
$compiler->getHtml()
);
if(env("COMPILER_CACHING", false))
{
Redis::set("compiler_{$hashedCode}", $compiler->serialize());
}


return [
"statusCode" => 200,
Expand All @@ -180,10 +177,16 @@ function compileCode($code)
"stderr" => $compiler->getErrorOutput(),
];
}


if(env("COMPILER_CACHING", false))
{
Redis::set("compiler_{$hashedCode}", $compiler->serialize());
}

return [
"statusCode" => 400,
"hash" => $hashedCode,
"html" => $compiler->getHtml(),
"stdout" => $compiler->getOutput(),
"stderr" => $compiler->getErrorOutput(),
];
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -20,5 +21,6 @@ public function register(): void
public function boot(): void
{
//
URL::forceRootUrl(env("APP_URL"));
}
}
5 changes: 4 additions & 1 deletion build-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ source "/opt/emsdk/emsdk_env.sh"
echo "Building libraries"
embuilder build libpng sdl2_mixer

echo "Building v0.1 library objects"
echo "Building v0.01 library objects"
(cd third_party/v0.01; bash build.sh)

echo "Building v0.02 library objects"
(cd third_party/v0.02; bash build.sh)
101 changes: 60 additions & 41 deletions pgetinker/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use stdClass;

class Compiler
{
Expand Down Expand Up @@ -42,6 +44,42 @@ public function __construct()
$this->logger = new Logger("compiler");
return $this;
}

public function serialize()
{
$object = new stdClass();

$object->code = $this->code;
$object->compilerCommand = $this->compilerCommand;
$object->compilerExitCode = $this->compilerExitCode;
$object->environmentVariables = $this->environmentVariables;
$object->errors = $this->errors;
$object->html = $this->html;
$object->linkerCommand = $this->linkerCommand;
$object->linkerExitCode = $this->linkerExitCode;
$object->linkerInputFiles = $this->linkerInputFiles;
$object->output = $this->output;

return json_encode($object, JSON_PRETTY_PRINT);
}

public function deserialize(string $json)
{
$object = json_decode($json, false);

$this->code = $object->code;
$this->compilerCommand = $object->compilerCommand;
$this->compilerExitCode = $object->compilerExitCode;
$this->environmentVariables = $object->environmentVariables;
$this->errors = $object->errors;
$this->html = $object->html;
$this->linkerCommand = $object->linkerCommand;
$this->linkerExitCode = $object->linkerExitCode;
$this->linkerInputFiles = $object->linkerInputFiles;
$this->logger = null;
$this->output = $object->output;
$this->workingDirectory = "";
}

public function setCode(string $code)
{
Expand Down Expand Up @@ -70,6 +108,12 @@ public function getHtml()
return $this->html;
}

public function getStatus()
{
return ($this->compilerExitCode == 0 && $this->linkerExitCode == 0) ?
200 : 400;
}

private function processCodeAbsoluteOrRelativePaths($index)
{
// filter include macros with an absolute or relative path, naughty naughty
Expand Down Expand Up @@ -154,22 +198,20 @@ private function processCodeRemoteInclude($index)

if(env("COMPILER_REMOTE_INCLUDE_CACHING", false))
{
$remoteIncludeCache = Redis::get("remote_include_{$hashedUrl}");

// if we have a cached version of the url's contents, don't pull it
if(
Storage::fileExists("remoteIncludeCache/{$hashedUrl}") &&
Storage::fileExists("remoteIncludeCache/{$hashedUrl}.time")
)
if(isset($remoteIncludeCache))
{
$this->logger->info("remote include cache hit");

$requestTime = floatval(Storage::get("remoteIncludeCache/{$hashedUrl}.time"));
$remoteIncludeCache = json_decode($remoteIncludeCache, false);

// just because it's cached, doesn't mean you get to compile faster!
usleep($requestTime * 1000000);

usleep(floatval($remoteIncludeCache->time) * 1000000);
file_put_contents(
"{$this->workingDirectory}/{$potentialFilename}",
Storage::get("remoteIncludeCache/{$hashedUrl}")
$remoteIncludeCache->content
);

$this->code[$index] = '#include "' . $potentialFilename .'"';
Expand Down Expand Up @@ -254,8 +296,13 @@ private function processCodeRemoteInclude($index)
if(env("COMPILER_REMOTE_INCLUDE_CACHING", false))
{
$this->logger->info("caching remotely included source file: $potentialFilename");
Storage::put("remoteIncludeCache/{$hashedUrl}", $response->body());
Storage::put("remoteIncludeCache/{$hashedUrl}.time", $requestDuration);

$remoteIncludeCache = new stdClass();

$remoteIncludeCache->time = $requestDuration;
$remoteIncludeCache->content = $response->body();

Redis::set("remote_include_{$hashedUrl}", json_encode($remoteIncludeCache, JSON_PRETTY_PRINT));
}

$this->code[$index] = '#include "' . $potentialFilename .'"';
Expand Down Expand Up @@ -296,7 +343,7 @@ private function processCode()

private function prepareEnvironment()
{
$version = "v0.01";
$version = "v0.02";

$compilerEnvironment = env("COMPILER_ENVIRONMENT", "local");

Expand Down Expand Up @@ -467,36 +514,8 @@ private function link()

private function cleanUp()
{
$this->logger->info("cleanUp called");

$this->logger->info("OUTPUT:\n\n" . $this->getOutput() . "\n\nERROR:\n\n" . $this->getErrorOutput());

if(env("FILESYSTEM_DISK") == "s3")
{
// convert workingDirectory to laravel disk path
$prefix = dirname($this->workingDirectory);
$this->workingDirectory = str_replace("{$prefix}/", "", $this->workingDirectory);

Log::info("uploading files to remote disk.");

// get the local files
$files = Storage::disk("local")->files($this->workingDirectory);

// create the s3 directory
Storage::makeDirectory($this->workingDirectory);

for($i = 0; $i < count($files); $i++)
{
// copy the file from the localDisk to the s3
Storage::put(
$files[$i],
Storage::disk("local")->get($files[$i])
);
}

// remove the local files
Storage::disk("local")->deleteDirectory($this->workingDirectory);
}
Log::info("Compile: finished disgracefully");
}

public function build()
Expand Down
2 changes: 1 addition & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ class PGEtinker
if(this.monacoModelIntellisense === null)
{
this.monacoModelIntellisense = monaco.editor.createModel("", "cpp", monaco.Uri.parse("inmemory://pgetinker.h"));
axios.get("/api/model/v0.01").then((response) =>
axios.get("/api/model/v0.02").then((response) =>
{
this.monacoModelIntellisense.setValue(response.data);
});
Expand Down
Loading

0 comments on commit ad2ca8e

Please sign in to comment.