Skip to content

Kod Çalıştırma Motoru (Code Execution Engine)

Piston API ile güvenli kod çalıştırma, coding exercises sistemi, test case yönetimi ve öğrenci çözümleri.

Achidemy, interaktif kodlama egzersizleri için Piston API tabanlı bir kod çalıştırma motoru kullanır. Öğrenciler kod yazar, test eder ve gerçek zamanlı geri bildirim alır. Bu sayfa kod çalıştırma altyapısını, coding exercises veritabanı şemasını ve GraphQL mutation’larını açıklar.

Dosya: app/lib/code-engine.ts

Endpoint: https://emkc.org/api/v2/piston/execute

Desteklenen Diller:

  • JavaScript, Python, Java, C++, C#, Go, Rust, TypeScript, PHP, Ruby, Swift, Kotlin, vb.
executeCode(
language: string,
code: string,
input: string = "",
options?: { runTimeout?: number; compileTimeout?: number }
): Promise<ExecutionResult>

Parametreler:

ParametreTipAçıklama
languagestringDil kodu (örn: javascript, python, java)
codestringÇalıştırılacak kod
inputstringstdin input (opsiyonel)
options.runTimeoutnumberÇalıştırma timeout (ms, varsayılan: 10000)
options.compileTimeoutnumberDerleme timeout (ms, varsayılan: 15000)

Dönüş Değeri: ExecutionResult

interface ExecutionResult {
success: boolean;
stdout: string | null;
stderr: string | null;
output: string | null;
error: string | null;
exitCode: number | null;
signal: string | null;
}
  • Sandbox: Piston API her kod çalıştırmayı izole bir sandbox’ta yürütür
  • Timeout: Çalıştırma ve derleme için timeout limitleri
  • Resource Limits: CPU ve memory limitleri Piston tarafından uygulanır

Dosya: app/db/schema.ts

Tablo: coding_exercises

AlanTipAçıklama
iduuidPrimary key
section_iduuid (FK → sections)Hangi bölüme ait
titletextEgzersiz başlığı
descriptiontextAçıklama (markdown desteklenir)
languagetextProgramlama dili (javascript, python, vb.)
initial_codetextÖğrenciye verilen başlangıç kodu
solution_codetextÇözüm kodu (eğitmen için)
hintstextİpuçları (opsiyonel)
orderintegerBölüm içindeki sıra

Tablo: coding_exercise_test_cases

AlanTipAçıklama
iduuidPrimary key
exercise_iduuid (FK → coding_exercises)Hangi egzersize ait
inputtextTest input’u (öğrenci kodunun sonuna eklenecek çağrı kodu)
expected_outputtextBeklenen çıktı
descriptiontextTest açıklaması (örn: “Boş array için”)
hinttextTest geçemediğinde öğrenciye verilecek özel ipucu
is_hiddenbooleanGizli test mi? (öğrenci göremez, sadece geçti/geçmedi görür)
orderintegerTest sırası

Örnek Test Case:

  • input: console.log(sum(5, 10))
  • expected_output: 15
  • hint: “Toplama fonksiyonunuzun parametreleri doğru mu?“

Tablo: user_exercise_submissions

AlanTipAçıklama
iduuidPrimary key
user_idtext (FK → users)Öğrenci ID
exercise_iduuid (FK → coding_exercises)Hangi egzersiz
submitted_codetextÖğrencinin gönderdiği kod
passedbooleanTüm testler geçti mi?
passed_testsintegerGeçen test sayısı
total_testsintegerToplam test sayısı
execution_timeintegerÇalıştırma süresi (ms)
submitted_attimestampGönderim zamanı

Dosya: app/graphql/schema.ts

mutation SubmitExercise($exerciseId: ID!, $code: String!) {
submitExercise(exerciseId: $exerciseId, code: $code) {
passed
passedTests
totalTests
executionTime
testResults {
testCaseId
input
expected
actual
passed
error
hint
}
}
}
  1. Egzersiz ve test case’leri çek: exerciseId ile coding_exercises ve coding_exercise_test_cases sorgulanır
  2. Her test case için kod çalıştır:
    • input değeri öğrenci kodunun sonuna eklenir (örn: code + "\n" + testCase.input)
    • executeCode(exercise.language, fullCode) çağrılır
    • stdout ile expected_output karşılaştırılır
  3. Sonuçları kaydet: user_exercise_submissions tablosuna kayıt
  4. Test sonuçlarını döndür: Her test için passed, actual, error, hint bilgileri
interface TestCaseResult {
testCaseId: string;
input: string | null;
expected: string;
actual: string;
passed: boolean;
error: string | null;
hint: string | null; // Test geçmediyse ve hint varsa
}

Dosya: app/components/learn/CodingWorkspace.tsx

  • Monaco Editor: Kod editörü (VS Code editörü)
  • Test Çalıştırma: “Run” butonu ile kod çalıştırma
  • Test Sonuçları: Her test için geçti/geçmedi, çıktı, hata mesajı gösterimi
  • Hint Gösterimi: Test geçmediğinde ipucu gösterimi
  • Otomatik Kaydetme: Kod değişiklikleri localStorage’a kaydedilir
  • Tamamlanma Durumu: Tüm testler geçtiğinde egzersiz tamamlanır
<CodingWorkspace
exercise={exercise}
courseId={courseId}
initialCompleted={false}
onComplete={() => {/* egzersiz tamamlandı */}}
onNext={() => {/* sonraki egzersize geç */}}
/>

Dosya: app/components/instructor/TestCaseManager.tsx

Eğitmenler coding exercise oluştururken test case’leri ekleyebilir:

  • Public Test Cases: Öğrenci görebilir (açık testler)
  • Hidden Test Cases: Öğrenci göremez, sadece geçti/geçmedi görür (gizli testler)
  • Hint Ekleme: Her test case için özel ipucu eklenebilir
  • Input/Output: Test input’u ve beklenen çıktı tanımlanır

İşlemVarsayılanMaksimum
Çalıştırma10 saniye30 saniye
Derleme15 saniye30 saniye
  • Piston API rate limit’leri uygulanır
  • Aynı anda çok fazla istek gönderilmemelidir
  • Moderation: Gönderilen kod içeriği moderation.ts ile kontrol edilebilir (gelecekte)
  • Malicious Code: Piston sandbox içinde çalıştığı için sistem güvenliği korunur

  • app/lib/code-engine.ts — Piston API entegrasyonu
  • app/graphql/schema.tssubmitExercise mutation
  • app/components/learn/CodingWorkspace.tsx — Öğrenci kod editörü
  • app/components/instructor/TestCaseManager.tsx — Test case yönetimi
  • app/db/schema.ts — Veritabanı şeması (coding_exercises, coding_exercise_test_cases, user_exercise_submissions)