Skip to content
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

JavaScript web service chapter #89

Merged
merged 24 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a8c750b
Added first iteration of JavaScript web service chapter
sverhoeven Jun 14, 2020
14b7445
CI for javascript web service
sverhoeven Jun 14, 2020
5d5363b
Tabs not spaces
sverhoeven Jun 14, 2020
1a8b6a2
Typo
sverhoeven Jun 14, 2020
f646ad3
Merge remote-tracking branch 'origin/develop' into 83-js-web-service
sverhoeven Jun 15, 2020
feb9694
Standardize headings + use openapi instead of swagger
sverhoeven Jun 15, 2020
a04bc44
Enable emoij
sverhoeven Jun 15, 2020
9598415
Re-use JSON schema snippets + misc edits
sverhoeven Jun 15, 2020
6786b57
Added OpenAPI web service using fastify-oas chapter
sverhoeven Jun 15, 2020
1697cd2
Typo
sverhoeven Jun 15, 2020
71fb87a
Install fastify-oas in CI
sverhoeven Jun 15, 2020
7d8dd88
Capture web assembly logs in artifact
sverhoeven Jun 15, 2020
759333a
Added Long running task with worker threads chapter
sverhoeven Jun 15, 2020
ec2b192
Updated changelog
sverhoeven Jun 16, 2020
a5c2164
Add files for JavaScript threaded web service
sverhoeven Jun 16, 2020
bf6daba
Proof read Accessing C++ function with Emscripten chapter
sverhoeven Jun 16, 2020
a6b246b
Proofread Long running task with worker threads
sverhoeven Jun 16, 2020
a7674c9
Test invalid request + standardize curls
sverhoeven Jun 16, 2020
a017493
Snippet before
sverhoeven Jun 16, 2020
906eccb
Dont import wasm in parent thread
sverhoeven Jun 16, 2020
f37019d
Dont detect duplicates in webassembly/webservice.js,webassembly/opena…
sverhoeven Jun 16, 2020
0347565
Dont forget Node.JS
sverhoeven Jun 16, 2020
c639118
Add pro/cons for each way
sverhoeven Jun 18, 2020
1cbde84
Add screenshots for Python web application
sverhoeven Jun 18, 2020
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
35 changes: 31 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,45 @@ jobs:
- name: Build WebAssembly module
run: make build-wasm

- name: Test from command line
run: make test-wasm-cli

- name: Start web server for hosting files in background
run: make host-webassembly-files 2>&1 | tee ./web-server.log &
run: make host-webassembly-files 2>&1 | tee ./run-host-service.log &

- name: Run tests
run: make test-webassembly

- name: Upload log of web server
- name: Install Node.js dependencies
run: make js-deps

- name: Start web service in background
run: make run-js-webservice 2>&1 | tee ./run-web-service.log &

- name: Test web service
run: make test-js-webservice

- name: Test web service with invalid request
run: make test-js-webservice-invalid || test $? -eq 2

- name: Start OpenAPI web service in background
run: make run-js-openapi 2>&1 | tee ./run-openapi-web-service.log &

- name: Test OpenAPI web service
run: make test-js-openapi

- name: Start OpenAPI threaded web service in background
run: make run-js-threaded 2>&1 | tee ./run-threaded-web-service.log &

- name: Test OpenAPI threaded web service
run: make test-js-threaded

- name: Upload log of services
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: webassembly-service-log
path: ./web-server.log
name: webassembly-service-logs
path: ./run-*.log
react:
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

* JavaScript web service chapter ([#83](https://github.com/NLESC-JCER/cpp2wasm/issues/83))
* Screenshots for Python web application ([#32](https://github.com/NLESC-JCER/cpp2wasm/issues/32))
* Pros and cons for each way to make C++ code available as a web application or web wervice

## [0.4.0] - 2020-06-10

### Changed
Expand Down
41 changes: 38 additions & 3 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ To run the commands in the README.md the following items are required
1. GNU C++ compiler (`g++`) and `make`, install with `sudo apt install -y build-essential`
1. [Apache httpd server 2.4](http://httpd.apache.org/), install with `sudo apt install -y apache2`
1. Python development, install with `sudo apt install -y python3-dev`
1. [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html)
1. [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html), includes a Node.js installation
1. [Docker Engine](https://docs.docker.com/install/), setup so `docker` command can be run [without sudo](https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user).
1. [Perl](https://www.perl.org/), already installed on Linux

Expand All @@ -17,7 +17,7 @@ All the commands in the [README.md](README.md) and [CONTRIBUTING.md](CONTRIBUTIN

```{.makefile file=Makefile}
# this Makefile snippet is stored as Makefile
.PHONY: clean clean-compiled clean-entangled test all entangle entangle-list py-deps test-cgi test-cli test-py start-redis stop-redis run-webservice test-webservice run-celery-worker run-celery-webapp run-webapp build-wasm host-webassembly-files host-react-files test-webassembly test-react init-git-hook check
.PHONY: clean clean-compiled clean-entangled test all entangle entangle-list py-deps test-cgi test-cli test-py start-redis stop-redis run-webservice test-webservice run-celery-worker run-celery-webapp run-webapp build-wasm host-webassembly-files host-react-files test-webassembly test-react init-git-hook check test-wasm-cli npm-fopenapi-deps npm-fastify npm-openapi run-js-webservice test-js-webservice test-js-webservice-invalid test-js-openapi run-js-openapi test-js-openapi npm-threaded run-js-threaded test-js-threaded

UID := $(shell id -u)
# Prevent suicide by excluding Makefile
Expand Down Expand Up @@ -103,7 +103,7 @@ test-webservice:
run-celery-worker: flask/newtonraphsonpy.*.so
<<run-celery-worker>>

run-celery-webapp: flask/newtonraphsonpy.*.so
run-celery-webapp:
<<run-celery-webapp>>

build-wasm: webassembly/newtonraphsonwasm.js webassembly/newtonraphsonwasm.wasm
Expand All @@ -117,6 +117,9 @@ react/newtonraphsonwasm.wasm: webassembly/newtonraphsonwasm.wasm
react/newtonraphsonwasm.js: webassembly/newtonraphsonwasm.js
<<link-webassembly-js>>

test-wasm-cli: build-wasm
<<test-wasm-cli>>

host-webassembly-files: build-wasm
<<host-files>>

Expand All @@ -126,6 +129,38 @@ host-react-files: react/newtonraphsonwasm.js react/newtonraphsonwasm.wasm
test-webassembly:
<<test-webassembly>>

js-deps: npm-fastify npm-openapi npm-threaded

npm-fastify:
<<npm-fastify>>

npm-openapi:
<<npm-openapi>>

npm-threaded:
<<npm-threaded>>

run-js-webservice: build-wasm
<<run-js-webservice>>

test-js-webservice:
<<test-js-webservice>>

test-js-webservice-invalid:
<<test-js-webservice-invalid>>

run-js-openapi: build-wasm
<<run-js-openapi>>

test-js-openapi:
<<test-js-openapi>>

run-js-threaded: build-wasm
<<run-js-threaded>>

test-js-threaded:
<<test-js-threaded>>

react/worker.js:
<<link-worker>>

Expand Down
67 changes: 62 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# this Makefile snippet is stored as Makefile
.PHONY: clean clean-compiled clean-entangled test all entangle entangle-list py-deps test-cgi test-cli test-py start-redis stop-redis run-webservice test-webservice run-celery-worker run-celery-webapp run-webapp build-wasm host-webassembly-files host-react-files test-webassembly test-react init-git-hook check
.PHONY: clean clean-compiled clean-entangled test all entangle entangle-list py-deps test-cgi test-cli test-py start-redis stop-redis run-webservice test-webservice run-celery-worker run-celery-webapp run-webapp build-wasm host-webassembly-files host-react-files test-webassembly test-react init-git-hook check test-wasm-cli npm-fopenapi-deps npm-fastify npm-openapi run-js-webservice test-js-webservice test-js-webservice-invalid test-js-openapi run-js-openapi test-js-openapi npm-threaded run-js-threaded test-js-threaded

UID := $(shell id -u)
# Prevent suicide by excluding Makefile
Expand Down Expand Up @@ -81,25 +81,34 @@ run-webservice: openapi/newtonraphsonpy.*.so
python openapi/webservice.py

test-webservice:
curl -X POST "http://localhost:8080/api/newtonraphson" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"epsilon\":0.001,\"guess\":-20}"
curl --request POST \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--data '{"epsilon":0.001,"guess":-20}' \
http://localhost:8080/api/newtonraphson

run-celery-worker: flask/newtonraphsonpy.*.so
PYTHONPATH=openapi celery worker -A tasks
PYTHONPATH=flask celery worker -A tasks

run-celery-webapp: flask/newtonraphsonpy.*.so
run-celery-webapp:
python flask/webapp-celery.py

build-wasm: webassembly/newtonraphsonwasm.js webassembly/newtonraphsonwasm.wasm

webassembly/newtonraphsonwasm.js webassembly/newtonraphsonwasm.wasm:
emcc -Icli/ --bind -o webassembly/newtonraphsonwasm.js -s MODULARIZE=1 -s EXPORT_NAME=createModule webassembly/wasm-newtonraphson.cpp
emcc -Icli/ -o webassembly/newtonraphsonwasm.js \
-s MODULARIZE=1 -s EXPORT_NAME=createModule \
--bind webassembly/wasm-newtonraphson.cpp

react/newtonraphsonwasm.wasm: webassembly/newtonraphsonwasm.wasm
cd react && ln -s ../webassembly/newtonraphsonwasm.wasm . && cd -

react/newtonraphsonwasm.js: webassembly/newtonraphsonwasm.js
cd react && ln -s ../webassembly/newtonraphsonwasm.js . && cd -

test-wasm-cli: build-wasm
node webassembly/cli.js 0.01 -20

host-webassembly-files: build-wasm
python3 -m http.server 8000

Expand All @@ -109,6 +118,54 @@ host-react-files: react/newtonraphsonwasm.js react/newtonraphsonwasm.wasm
test-webassembly:
npx cypress run --config-file false --spec 'cypress/integration/webassembly/*_spec.js'

js-deps: npm-fastify npm-openapi npm-threaded

npm-fastify:
npm install --no-save fastify

npm-openapi:
npm install --no-save fastify-oas

npm-threaded:
npm install --no-save node-worker-threads-pool

run-js-webservice: build-wasm
node webassembly/webservice.js

test-js-webservice:
curl --request POST \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--data '{"epsilon":0.001,"guess":-20}' \
http://localhost:3000/api/newtonraphson

test-js-webservice-invalid:
wget --content-on-error --quiet --output-document=- \
--header="accept: application/json" \
--header="Content-Type: application/json" \
--post-data '{"epilon":0.001,"guess":-20}' \
http://localhost:3000/api/newtonraphson

run-js-openapi: build-wasm
node webassembly/openapi.js

test-js-openapi:
curl --request POST \
--header "Content-Type: application/json" \
--header "accept: application/json" \
--data '{"guess":-20, "epsilon":0.001}' \
http://localhost:3001/api/newtonraphson

run-js-threaded: build-wasm
node webassembly/webservice-threaded.js

test-js-threaded:
curl --request POST \
--header "Content-Type: application/json" \
--header "accept: application/json" \
--data '{"guess":-20, "epsilon":0.001}' \
http://localhost:3002/api/newtonraphson

react/worker.js:
cd react && ln -s ../webassembly/worker.js . && cd -

Expand Down
Loading