Skip to content

Commit

Permalink
Merge pull request #34 from kcl-lang/feat-add-fmt-and-share-handler-i…
Browse files Browse the repository at this point in the history
…n-pluto

feat: add fmt and share handler in pluto kcl playground
  • Loading branch information
Peefy authored Jul 8, 2024
2 parents e80f00d + 6d70443 commit f7ac52d
Show file tree
Hide file tree
Showing 48 changed files with 8,677 additions and 794 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PLUTO_APP=true
38 changes: 0 additions & 38 deletions .github/workflows/cla.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ zz_*
**/target/
**/.DS_Store
**/.vscode
node_modules

# pluto
.pluto/**/*
21 changes: 0 additions & 21 deletions Dockerfile

This file was deleted.

20 changes: 6 additions & 14 deletions Makefile
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
21 changes: 17 additions & 4 deletions README.md
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)
92 changes: 92 additions & 0 deletions app/main.py
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)
41 changes: 0 additions & 41 deletions go.mod

This file was deleted.

98 changes: 0 additions & 98 deletions go.sum

This file was deleted.

Loading

0 comments on commit f7ac52d

Please sign in to comment.