Laravel Judge0
Judge0 API integration for running/judging
code with different languages
use Mouadbnl\Judge0\Models\Submission;
$submission = Submission::create([
'language_id' => 54, // C++ (GCC 9.2.0)
'source_code' =>'
#include
#include
using namespace std;
int main(){
string s;
cin >> s;
cout << "the value you entered is : " << s;
return 0;
}
'
])
->setInput('judge0')
->setExpectedOutput('the value you entered is : judge0')
->setTimeLimit(1) // seconds
->setMemoryLimitInMegabytes(256)
->submit();
Installation
You can install the package via composer:
composer require mouadbnl/laravel-judge0
You can publish and run the migrations with:
php artisan vendor:publish --provider="Mouadbnl\Judge0\Judge0ServiceProvider" --tag="judge0-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Mouadbnl\Judge0\Judge0ServiceProvider" --tag="judge0-config"
Usage
First add Submitter
trait to the User Model.
use Mouadbnl\Judge0\Traits\Submitter;
class User extends Authenticatable
{
use Submitter;
/** **/
}
This create a polymorphique relationship between the User model and the Submission model provided by this package. You can let the User make a submission using the follwing code:
$user = User::firstOrFail();
$user->submissions()->create([
'language_id' => 54, // C++ (GCC 9.2.0)
'source_code' =>'
#include
#include
using namespace std;
int main(){
string s;
cin >> s;
cout << "the value you entered is : " << s;
return 0;
}
'
])
->setInput('judge0')
->setExpectedOutput('the value you entered is : judge0')
->setTimeLimit(1) // seconds
->setMemoryLimitInMegabytes(256)
->submit();
Define the API to use
This package provides two drivers to connect to judge0 API
Instance Driver
This allows you to connect to a judge0 Docker instance. to do so please define the JUDGE0_BASE_URI
which is the url to your instance and JUDGE0_KEY
which it the API key to authenticate in your environment variables
or .env
file.
JUDGE0_BASE_URI=localhost:2358
JUDGE0_KEY=yout_key
Rapidapi Driver
This allows you to connect to the the Judge0 app on RapidAPI. For this you need to define the following in your environment variables
or .env
file.
JUDGE0_RAPIDAPI_BASE_URI
the rapidapi url to make the requests to, which it by defaulthttps://judge0-ce.p.rapidapi.com
.JUDGE0_RAPIDAPI_HOST
, by default it isjudge0-ce.p.rapidapi.com
.JUDGE0_RAPIDAPI_KEY
yout rapidapi key.
JUDGE0_RAPIDAPI_BASE_URI=https://judge0-ce.p.rapidapi.com
JUDGE0_RAPIDAPI_HOST=judge0-ce.p.rapidapi.com
JUDGE0_RAPIDAPI_KEY=yout_key
Testing
composer test
Credits
- Mouad Benali
- Spatie For the skeleton and Package Training
- All Contributors
License
The MIT License (MIT). Please see License File for more information.