Skip to content

Commit

Permalink
Merge pull request #11 from tareknaser/backend
Browse files Browse the repository at this point in the history
Implement Backend Service and Compile Button
  • Loading branch information
salaheldinsoliman authored May 26, 2024
2 parents a8b93ce + f1aa147 commit c927d6b
Show file tree
Hide file tree
Showing 28 changed files with 7,497 additions and 2,399 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.github
node_modules
packages/*/node_modules
target
Dockerfile
52 changes: 52 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Docker CI

on:
push:
branches:
- main

jobs:
build:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v4

# Needed for cargo-make
- name: Set up Rust
uses: dtolnay/rust-toolchain@stable

- name: Install cargo make
run: cargo install cargo-make

- name: get Sysbox
run: wget https://downloads.nestybox.com/sysbox/releases/v0.6.1/sysbox-ce_0.6.1-0.linux_amd64.deb

- name: Install Sysbox
run: sudo apt-get install ./sysbox-ce_0.6.1-0.linux_amd64.deb

- name: Configure Sysbox runtime
run: sudo cp sysbox/daemon.json /etc/docker/daemon.json

- name: Restart docker service
run: sudo systemctl restart docker.service

- name: Build docker
run: cargo make docker-build

- name: Run docker
run: cargo make docker-run

- name: Allow Docker image to boot up
uses: juliangruber/sleep-action@v1
with:
time: 120s

# Needed to run tests
- name: Install npm dependencies
run: cargo make deps-npm

- name: Test
run: cargo make test-app

- name: Show logs
run: cargo make docker-log
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,6 @@ $RECYCLE.BIN/
*.lnk


package-lock.json
package-lock.json

packages/_generated/*/src
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ opt-level = "z"

[workspace]
members = [
"crates/backend",
"crates/browser",
"crates/solang"
"crates/generate-bindings",
"crates/solang",
]
default-members = ["crates/browser"]
resolver = "1"
resolver = "1"
55 changes: 55 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Start from a rust base image
FROM rust:1.76.0 as base

# Set the current directory
WORKDIR /app

# Copy everthing that is not dockerignored to the image
COPY . .

# Start from base image
FROM base as builder

# Rust setup
RUN rustup toolchain install stable
RUN rustup toolchain install nightly-2024-02-04
RUN rustup target add wasm32-unknown-unknown
RUN cargo install cargo-make

# Install Node
RUN apt-get --yes update
RUN apt-get --yes upgrade
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION v18.16.1
RUN mkdir -p /usr/local/nvm && apt-get update && echo "y" | apt-get install curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION"
ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/bin
ENV PATH $NODE_PATH:$PATH

# Install dependencies
RUN cargo make deps-wasm
RUN cargo make deps-npm

# Build
RUN cargo make build-server
RUN cargo make build-bindings
RUN cargo make build-app
RUN cargo make build-backend


# Final image

# Start from a base image (comes with docker)
FROM nestybox/ubuntu-jammy-systemd-docker:latest

# Copy the built files
COPY --from=builder /app/packages/app/dist /app/packages/app/dist
COPY --from=builder /app/target/release/backend /app/target/release/backend

# Startup scripts
COPY sysbox/on-start.sh /usr/bin
RUN chmod +x /usr/bin/on-start.sh

# Entrypoint
ENTRYPOINT [ "on-start.sh" ]
59 changes: 54 additions & 5 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@
default_to_workspace = false
skip_core_tasks = true

[tasks.deps]
[tasks.deps-wasm]
script = '''
cargo install wasm-bindgen-cli
'''

[tasks.deps-npm]
script = '''
npm install
'''

[tasks.deps]
dependencies = ["deps-wasm", "deps-npm"]

[tasks.build-server]
script = '''
cargo build --release --target wasm32-unknown-unknown
wasm-bindgen --out-dir ./packages/app/assets/wasm --target web --typescript ./target/wasm32-unknown-unknown/release/demo_lsp_browser.wasm
'''

[tasks.build-bindings]
script = '''
cargo run -p generate-bindings -- --target $(pwd)/packages/_generated/commontypes/src
'''

[tasks.build-app]
script = '''
npm run build --workspace=packages/app
'''

[tasks.build-backend]
script = '''
cargo build -p backend --release
'''

[tasks.build]
dependencies = ["build-server", "build-app"]
dependencies = ["build-server", "build-bindings", "build-app", "build-backend"]

[tasks.clean-server]
script = '''
cargo clean
'''

[tasks.clean-bindings]
script = '''
rm -rf packages/_generated/commontypes/src
'''

[tasks.clean-app]
script = '''
rm -rf packages/app/dist
Expand All @@ -43,9 +65,36 @@ cargo +nightly fmt --all

[tasks.run]
script = '''
npm run app --workspace=packages/app
./target/release/backend --frontend_folder packages/app/dist --port 9000
'''

[tasks.test]
[tasks.test-backend]
command = "cargo"
args = ["test", "--target", "x86_64-unknown-linux-gnu"]
args = ["test", "--target", "x86_64-unknown-linux-gnu"]

[tasks.test-app]
script = '''
npm run test --workspace=packages/app
'''


[tasks.docker-build]
script = '''
docker build -t solang-playground .
'''

[tasks.docker-run]
script = '''
docker run \
--runtime=sysbox-runc \
--name playground \
--detach \
--volume /tmp:/tmp \
--publish 9000:9000 \
solang-playground
'''

[tasks.docker-log]
script = '''
docker logs playground
'''
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Solang Playground - A Solidity web editor for [Hyperledger Solang](https://github.com/hyperledger/solang)


Welcome to Solang Playground, a Solidity web editor for that enables editing, compiling, deploying and interacting with Solidity smart contracts on Solana or Polkadot.



## Demo

You can experiment with a live demo of the example server integrated with an in-browser editor here:
Expand All @@ -25,7 +22,20 @@ cargo make build
cargo make run
```

## Project Structure

This repository has two main parts:

- **`crates`**: Rust code, including:
- `backend`: Actix Web server for the frontend.
- `generate_bindings`: Generates TypeScript bindings for API endpoints.
- `solang`: Contains the `solang-parser`
- `browser`: Contains the monaco editor web server
- **`packages`**: TypeScript project for the frontend app, served by the Rust backend.

## Docker Setup

Our Dockerfile for Solang Playground relies on Nestybox's Sysbox runtime. This helps compile Solang Smart Contracts within a protected Docker environment. We also use a multi-stage build process to improve image size.

## Roadmap and Status

Expand All @@ -34,13 +44,11 @@ cargo make run
| Milestone | Related Feature | Status |
| ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | ----------- |
| Compile Solang language server to WASM, and integrate it to a Monaco editor. | Allow editing Solidity source files in the browser, with smarts provided from the server (Diagnostics, code completion, etc..) | Completed |
| Host Solang on a backend service | Allow compiling smart contracts on the web editor | In progress |
| Support Polkadot API | Allow the deployment and interaction with Solidity contracts on Polkadot | Not Started |
| Host Solang on a backend service | Allow compiling smart contracts on the web editor | Completed |
| Support Polkadot API | Allow the deployment and interaction with Solidity contracts on Polkadot | In progress |
| IDE Improvements | Improve developer experience when trying out the IDE, making it a more attractive option for Solidity devs | Not started |




## Acknowledgments

This project started out as a fork of https://github.com/silvanshade/tower-lsp-web-demo. [Darin Morrison](https://github.com/silvanshade) created a demo project where an example tower-lsp language server was compiled to WASM and integrated in a Monaco web editor.
- This project started out as a fork of https://github.com/silvanshade/tower-lsp-web-demo. [Darin Morrison](https://github.com/silvanshade) created a demo project where an example tower-lsp language server was compiled to WASM and integrated in a Monaco web editor.
- The structure of `solang-playground` is significantly inspired by `ink-playground`. This includes, but is not limited to, the implementation of running the Solang compiler functionality in a sandboxed environment.
23 changes: 23 additions & 0 deletions crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "backend"
version = "0.1.0"
edition = "2021"


[dependencies]
serde = { version = "1.0", features = ["derive"] }
clap = { version = "4.5", features = ["derive", "env"] }
serde_json = "1.0"
tempfile = "3.10"
tokio = { version = "1.28.2", features = [
"macros",
"time",
"process",
"rt-multi-thread",
] }
anyhow = "1.0"
log = "0.4"

typescript-type-def = "0.5"
actix-web = "4.5"
actix-files = "0.6"
20 changes: 20 additions & 0 deletions crates/backend/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use clap::Parser;

/// Command line options for the backend
#[derive(Parser, Clone)]
pub struct Opts {
#[arg(
short = 'p',
long = "port",
default_value = "8080",
env = "PORT",
help = "Port to listen on"
)]
pub port: u16,

#[arg(long = "host", default_value = "localhost", env = "HOST", help = "Host to listen on")]
pub host: String,

#[arg(long = "frontend_folder", help = "Path to the frontend folder")]
pub frontend_folder: Option<String>,
}
5 changes: 5 additions & 0 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod cli;
mod services;

pub use cli::Opts;
pub use services::{route_compile, CompilationRequest, CompilationResult};
Loading

0 comments on commit c927d6b

Please sign in to comment.