Skip to content

feat: Add WebAssembly PoC #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.git
/.github
/_examples
/docs
/tools
/wasm
/web
/www
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
Expand Down Expand Up @@ -42,3 +43,16 @@ jobs:
run: go build -v ./...
- name: Test
run: go test -v ./...
build-wasm:
name: Build WebAssembly
runs-on: ubuntu-latest
needs: [lint]
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.24
- name: Build
run: make wasm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: OverScry CD (Deploy Documentation)
name: OverScry CD (Deploy Website)

on:
push:
Expand All @@ -16,8 +16,8 @@ concurrency:
cancel-in-progress: false

jobs:
deploy-documentation:
name: Deploy documentation
deploy-website:
name: Deploy website
runs-on: ubuntu-latest
steps:
- name: Checkout source code
Expand All @@ -26,13 +26,13 @@ jobs:
uses: peaceiris/actions-mdbook@v2
with:
mdbook-version: "latest"
- name: Build Book
run: cd docs && mdbook build
- name: Build Website
run: make website
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: docs/book
path: www
- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v4
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Go workspace file
go.work
Expand All @@ -23,3 +23,10 @@ go.work.sum

# macOS .DS_Store files
.DS_Store

# Build files
dist/
www/

# WebAssembly file
web/wasm/overscry.wasm
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ FROM golang:1.24.1-bookworm AS builder
COPY --from=base / /
WORKDIR /app
ADD . /app
RUN go build -ldflags '-s -w'
RUN make cli-docker

FROM base
COPY --from=builder /app/overscry /
COPY --from=builder /app/dist/overscry /
ENTRYPOINT ["./overscry"]
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: cli cli-docker wasm website install

cli:
go build -o dist/overscry .

cli-docker:
go build -o dist/overscry -ldflags '-s -w' .

wasm:
GOOS=js GOARCH=wasm go build -o dist/overscry.wasm ./wasm
cp dist/overscry.wasm web/wasm
cp "$(shell go env GOROOT)/lib/wasm/wasm_exec.js" web/wasm

website:
$(MAKE) wasm
-mkdir www && mkdir www/wasm
cp web/wasm/* www/wasm
cd web/book && mdbook build
cp -R web/book/book/ www

install:
go install .
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cli

import (
"fmt"
"runtime"

"github.com/kkrypt0nn/overscry/cli/cmd"
"github.com/kkrypt0nn/overscry/core"
)

func Main() {
core.Logger.Println(fmt.Sprintf("%s v%s ${fg:gray}(built for %s on %s)${effect:reset}\n", core.Name, core.Version, runtime.GOOS, runtime.GOARCH))
cmd.Execute()
}
2 changes: 1 addition & 1 deletion core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package core

const (
Name = "OverScry"
Version = "0.0.8-dev"
Version = "0.0.9-dev"
Author = "Krypton (root@krypton.ninja)"
Website = "https://overscry.krypton.ninja"
)
11 changes: 2 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package main

import (
"fmt"
"runtime"

"github.com/kkrypt0nn/overscry/cmd"
"github.com/kkrypt0nn/overscry/core"
)
import "github.com/kkrypt0nn/overscry/cli"

func main() {
core.Logger.Println(fmt.Sprintf("%s v%s ${fg:gray}(built for %s on %s)${effect:reset}\n", core.Name, core.Version, runtime.GOOS, runtime.GOARCH))
cmd.Execute()
cli.Main()
}
9 changes: 9 additions & 0 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ func LoadSettings(fileName string) (*Settings, error) {

return &s, nil
}

func LoadSettingsFromString(yml string) (*Settings, error) {
var s Settings
err := yaml.Unmarshal([]byte(yml), &s)
if err != nil {
return nil, err
}
return &s, nil
}
32 changes: 32 additions & 0 deletions wasm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build js && wasm

package main

import (
"fmt"
"syscall/js"

"github.com/kkrypt0nn/overscry/models"
"github.com/kkrypt0nn/overscry/settings"
)

func parseYAML(this js.Value, args []js.Value) any {
if len(args) < 1 {
return js.ValueOf("Missing YAML input")
}

yamlStr := args[0].String()

s, err := settings.LoadSettingsFromString(yamlStr)
if err != nil {
return js.ValueOf("Error parsing YAML: " + err.Error())
}

res := fmt.Sprintf(models.JSON_OUTPUT, s.Node.ToOQL())
return js.ValueOf(res)
}

func main() {
js.Global().Set("parseYAMLToOQL", js.FuncOf(parseYAML))
select {}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions web/wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>OverScry WebAssembly</title>
</head>
<body>
<h1>OverScry WebAssembly</h1>

<textarea
id="yaml-input"
rows="10"
cols="80"
placeholder="Enter your YAML data here..."
></textarea>
<button onclick="run()">Generate query</button>

<pre id="output"></pre>

<script src="wasm_exec.js"></script>
<script>
const go = new Go();

async function run() {
const input = document.getElementById("yaml-input").value;
const result = await window.parseYAMLToOQL(input);
document.getElementById("output").textContent = result;
}

WebAssembly.instantiateStreaming(
fetch("overscry.wasm"),
go.importObject,
).then((result) => {
go.run(result.instance);
});
</script>
</body>
</html>
Loading