-
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.
Merge pull request #34 from kcl-lang/feat-add-fmt-and-share-handler-i…
…n-pluto feat: add fmt and share handler in pluto kcl playground
- Loading branch information
Showing
48 changed files
with
8,677 additions
and
794 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
PLUTO_APP=true |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -19,3 +19,7 @@ zz_* | |
**/target/ | ||
**/.DS_Store | ||
**/.vscode | ||
node_modules | ||
|
||
# pluto | ||
.pluto/**/* |
This file was deleted.
Oops, something went wrong.
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,18 +1,10 @@ | ||
default: run | ||
|
||
run: | ||
go run main.go | ||
pluto run | ||
|
||
fmt: | ||
go fmt ./... | ||
|
||
test: | ||
go test ./... | ||
|
||
api: | ||
curl http://localhost:2023/-/play/compile -X POST --data '{"body":"a=5 + 5"}' -H "content-type:application/json" | ||
curl http://localhost:2023/-/play/fmt?body="a=1" -X POST -H "content-type:application/json" | ||
python3 -m pip install black && python3 -m black . | ||
|
||
image: | ||
docker build . -t kcllang/kcl-playground | ||
docker push kcllang/kcl-playground | ||
deps: | ||
python3 -m pip install -r requirements | ||
npm install | ||
npm install -g pluto |
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,15 +1,28 @@ | ||
# KCL Playground | ||
|
||
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgit.luolix.top%2Fkcl-lang%2Fkcl-playground.svg?type=shield)](https://app.fossa.com/projects/git%2Bgit.luolix.top%2Fkcl-lang%2Fkcl-playground?ref=badge_shield) | ||
|
||
## Dependencies | ||
|
||
+ Docker | ||
+ Python 3.7+ | ||
+ Node.js | ||
|
||
## Quick Start | ||
|
||
- `go get kcl-lang.io/kcl-playground` | ||
- `kcl-playground` or `go run main.go` | ||
```shell | ||
python -m pip install -r requirements | ||
npm install | ||
npm install -g pluto | ||
pluto run | ||
``` | ||
|
||
## Screenshot | ||
|
||
![](screenshot.jpg) | ||
|
||
## License | ||
|
||
Apache License Version 2.0 | ||
|
||
|
||
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgit.luolix.top%2Fkcl-lang%2Fkcl-playground.svg?type=large)](https://app.fossa.com/projects/git%2Bgit.luolix.top%2Fkcl-lang%2Fkcl-playground?ref=badge_large) | ||
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgit.luolix.top%2Fkcl-lang%2Fkcl-playground.svg?type=large)](https://app.fossa.com/projects/git%2Bgit.luolix.top%2Fkcl-lang%2Fkcl-playground?ref=badge_large) |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import json | ||
import re | ||
import hashlib | ||
from pluto_client import Website, KVStore, Router, HttpRequest, HttpResponse | ||
import kcl_lib.api as kcl_api | ||
import tempfile | ||
|
||
color_pattern = re.compile(r"\x1b\[[0-9;]+m") | ||
api = kcl_api.API() | ||
router = Router("router") | ||
store = KVStore("store") | ||
website = Website("./website", "kcl-playground") | ||
website.addEnv("BACKEND_URL", router.url()) | ||
|
||
|
||
def run_code(code: str) -> kcl_api.ExecProgram_Result: | ||
with tempfile.NamedTemporaryFile(suffix=".k") as temp_file: | ||
temp_file.write(code.encode()) | ||
temp_file.seek(0) | ||
args = kcl_api.ExecProgram_Args(k_filename_list=[temp_file.name]) | ||
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 str(result.formatted, encoding="utf-8") | ||
|
||
|
||
def compile_handler(req: HttpRequest) -> HttpResponse: | ||
code = req.body["body"] | ||
try: | ||
result = run_code(code) | ||
except Exception as err: | ||
return HttpResponse( | ||
status_code=200, | ||
body=json.dumps( | ||
{ | ||
"errors": color_pattern.sub("", str(err).removeprefix("ERROR:")), | ||
} | ||
), | ||
) | ||
if result.err_message: | ||
return HttpResponse( | ||
status_code=200, | ||
body=json.dumps( | ||
{ | ||
"errors": color_pattern.sub("", result.err_message), | ||
} | ||
), | ||
) | ||
else: | ||
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({"body": id})) | ||
|
||
|
||
def query_handler(req: HttpRequest) -> HttpResponse: | ||
id = req.query["id"] | ||
return HttpResponse(status_code=200, body=json.dumps({"body": store.get(id)})) | ||
|
||
|
||
router.post("/-/play/compile", compile_handler) | ||
router.post("/-/play/fmt", fmt_handler) | ||
router.post("/-/play/share", share_handler) | ||
router.get("/-/play/query", query_handler) |
Oops, something went wrong.