-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add fmt and share handler in pluto
Signed-off-by: peefy <[email protected]>
- Loading branch information
Showing
4 changed files
with
56 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,71 @@ | ||
import json | ||
from pluto_client import Website, Router, HttpRequest, HttpResponse | ||
import hashlib | ||
from pluto_client import Website, KVStore, Router, HttpRequest, HttpResponse | ||
import kcl_lib.api as kcl_api | ||
|
||
api = kcl_api.API() | ||
website = Website("./website", "kcl-playground") | ||
router = Router("router") | ||
store = KVStore("store") | ||
|
||
website.addEnv("BACKEND_URL", router.url()) | ||
|
||
|
||
def run_code(code: str) -> kcl_api.ExecProgram_Result: | ||
args = kcl_api.ExecProgram_Args(k_filename_list=["test.k"], k_code_list=[code]) | ||
api = kcl_api.API() | ||
result = api.exec_program(args) | ||
return result | ||
|
||
|
||
def fmt_code(code: str) -> str: | ||
args = kcl_api.FormatCode_Args(source=code) | ||
result = api.format_code(args) | ||
return result.formatted | ||
|
||
|
||
def compile_handler(req: HttpRequest) -> HttpResponse: | ||
code = req.body["body"] | ||
result = run_code(code) | ||
if result.err_message: | ||
return HttpResponse(status_code=200, body=json.dumps({ | ||
"errors": result.err_message, | ||
})) | ||
return HttpResponse( | ||
status_code=200, | ||
body=json.dumps( | ||
{ | ||
"errors": result.err_message, | ||
} | ||
), | ||
) | ||
else: | ||
return HttpResponse(status_code=200, body=json.dumps({ | ||
"events": [{ | ||
"message": result.yaml_result, | ||
"kind": "stdout", | ||
}], | ||
})) | ||
return HttpResponse( | ||
status_code=200, | ||
body=json.dumps( | ||
{ | ||
"events": [ | ||
{ | ||
"message": result.yaml_result, | ||
"kind": "stdout", | ||
} | ||
], | ||
} | ||
), | ||
) | ||
|
||
|
||
def fmt_handler(req: HttpRequest) -> HttpResponse: | ||
code = req.body["body"] | ||
result = fmt_code(code) | ||
return HttpResponse(status_code=200, body=json.dumps({"body": result})) | ||
|
||
|
||
def share_handler(req: HttpRequest) -> HttpResponse: | ||
code = req.body["body"] | ||
sha1 = hashlib.sha1() | ||
sha1.update(code.encode("utf-8")) | ||
id = sha1.hexdigest() | ||
store.set(id, code) | ||
return HttpResponse(status_code=200, body=json.dumps({"id": id})) | ||
|
||
|
||
router.post("/-/play/compile", compile_handler) | ||
router.post("/-/play/fmt", fmt_handler) | ||
router.post("/-/play/share", share_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pluto_client | ||
kcl_lib==0.9.0b1 | ||
kcl_lib==0.9.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters