Code Execution API (Code Run)
Relevant source files
The following files were used as context for generating this wiki page:
- src/main/java/org/example/coding_convention/code_run/controller/CodeController.java
- src/main/java/org/example/coding_convention/file/service/FileService.java
- src/main/java/org/example/coding_convention/file/model/FilesDto.java
- src/main/java/org/example/coding_convention/websocket/editor/EditorDto.java
- src/main/java/org/example/coding_convention/project/model/Project.java
- README.md
Introduction
The Code Execution API (Code Run) module is a component of the PhanesEditor backend designed to handle the verification and execution of code within the web-based editor environment. While the current implementation primarily serves as a placeholder for code execution logic, it integrates with the broader system's project management and file handling capabilities to facilitate real-time collaboration.
The module's primary scope involves receiving code execution requests via REST endpoints and providing feedback on the input's validity. In the context of the PhanesEditor, this feature works alongside WebSocket-based real-time code synchronization to ensure that the code being executed reflects the latest changes made by collaborators within a specific project.
Sources: README.md:12-18, src/main/java/org/example/coding_convention/code_run/controller/CodeController.java:11-18
API Architecture and Endpoints
The Code Execution functionality is exposed through a REST controller that handles basic verification. The system is designed to eventually support multiple languages as defined in the project entity.
Code Controller Structure
The CodeController defines the entry point for execution requests. Currently, it includes a simple diagnostic endpoint to verify that the execution service is reachable.
| Endpoint | Method | Description | Response Type |
|---|---|---|---|
/code/run | GET | Validates input and triggers code execution logic. | BaseResponse<String> |
Sources: src/main/java/org/example/coding_convention/code_run/controller/CodeController.java:11-15
Supporting Languages
The execution environment is conceptually linked to the Project entity, which supports the following programming languages:
- JAVASCRIPT
- JAVA
- PYTHON
- C
Sources: src/main/java/org/example/coding_convention/project/model/Project.java:34-37
Data Interaction Flow
Before code can be executed, it must be retrieved from storage (S3 or local) or updated via the real-time editor. The following diagram illustrates how the system retrieves code content for processing.
The diagram shows the synchronous response from the Code Controller and the underlying data retrieval flow from S3 used to fetch code content.
Sources: src/main/java/org/example/coding_convention/code_run/controller/CodeController.java:13-16, src/main/java/org/example/coding_convention/file/service/FileService.java:94-131
Real-time Code Synchronization (WebSocket)
Code execution in PhanesEditor relies on the latest state of the code, which is managed through WebSocket events. This ensures that the code being run is the same code visible to all members of a project.
Code Update Logic
The system uses an EditorDto.Code structure to handle incremental updates to the code text before an execution request is finalized.
The class diagram illustrates the data structure used to transmit code changes across the WebSocket channel, which facilitates real-time execution prep.
Sources: src/main/java/org/example/coding_convention/websocket/editor/EditorDto.java:7-22
Content Persistence
When a user prepares to run code, the system can persist the current editor state to S3 to ensure the execution engine has access to the most recent file version.
- Request Object:
FilesDto.ContentUpdateReq - Validation: Ensures
fileName,fileContents, andprojectIdare present. - Storage Path: Derived from the S3 URL registered in the database.
Sources: src/main/java/org/example/coding_convention/file/model/FilesDto.java:14-30, src/main/java/org/example/coding_convention/file/service/FileService.java:45-66
Summary
The Code Execution API provides a foundation for running multi-language code within the PhanesEditor ecosystem. It currently serves as a verification layer that operates in tandem with the FileService for content retrieval and the WebSocket-based editor for real-time synchronization. By integrating with project-level language settings and S3 storage, the module ensures that code execution is consistent with the collaborative environment's state.
Sources: README.md:46-51, src/main/java/org/example/coding_convention/code_run/controller/CodeController.java:14-16