Skip to content

Commit

Permalink
Merge pull request #30 from mmomtchev/nodejs
Browse files Browse the repository at this point in the history
Node.js services support through `libnode`
  • Loading branch information
gfenoy committed May 26, 2023
2 parents 164b9f5 + af06a4e commit 1bb4ce4
Show file tree
Hide file tree
Showing 24 changed files with 1,451 additions and 33 deletions.
34 changes: 30 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ARG RUN_DEPS=" \
python3 \
r-base \
python3-pip\
libnode93 \
"
RUN set -ex \
&& apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
Expand All @@ -39,6 +40,7 @@ RUN set -ex \
&& add-apt-repository ppa:ubuntugis/ppa \
&& apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 \
&& add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' \
&& add-apt-repository ppa:mmomtchev/libnode \
\
&& apt-get install -y $RUN_DEPS \
\
Expand Down Expand Up @@ -94,12 +96,16 @@ ARG BUILD_DEPS=" \
librabbitmq-dev \
libkrb5-dev \
nlohmann-json-dev \
libnode-dev \
node-addon-api \
nodejs \
libaprutil1-dev\
"
WORKDIR /zoo-project
COPY . .

RUN set -ex \
&& curl -fsSL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
\
&& make -C ./thirds/cgic206 libcgic.a \
Expand All @@ -114,10 +120,12 @@ RUN set -ex \
#&& sed "s:-ljson-c:-Wl,-rpath,/usr/local/lib /usr/local/lib/libjson-c.so.5 :g" -i configure.ac \
&& autoconf \
&& find /usr -name otbWrapperApplication.h \
# Comment the line bellow if you don't want to use rabbitmq
&& ./configure --with-rabbitmq=yes --with-python=/usr --with-pyvers=3.6 --with-js=/usr --with-mapserver=/usr --with-ms-version=7 --with-json=/usr --with-r=/usr --with-db-backend --prefix=/usr --with-otb=/usr/ --with-itk=/usr --with-otb-version=6.6 --with-itk-version=4.12 --with-saga=/usr --with-saga-version=7.2 --with-wx-config=/usr/bin/wx-config \
# Uncomment the line bellow if you don't want to use rabbitmq
#&& ./configure --with-python=/usr --with-pyvers=3.6 --with-js=/usr --with-mapserver=/usr --with-ms-version=7 --with-json=/usr --with-r=/usr --prefix=/usr --with-otb=/usr/ --with-itk=/usr --with-otb-version=6.6 --with-itk-version=4.12 --with-saga=/usr --with-saga-version=7.2 --with-wx-config=/usr/bin/wx-config \
&& ./configure --with-rabbitmq=yes --with-python=/usr --with-pyvers=3.6 \
--with-nodejs=/usr --with-mapserver=/usr --with-ms-version=7 \
--with-json=/usr --with-r=/usr --with-db-backend --prefix=/usr \
--with-otb=/usr/ --with-itk=/usr --with-otb-version=6.6 \
--with-itk-version=4.12 --with-saga=/usr \
--with-saga-version=7.2 --with-wx-config=/usr/bin/wx-config \
&& make -j4 \
&& make install \
\
Expand All @@ -128,6 +136,8 @@ RUN set -ex \
&& cp ../zoo-services/utils/open-api/cgi-env/* /usr/lib/cgi-bin/ \
&& cp ../zoo-services/hello-py/cgi-env/* /usr/lib/cgi-bin/ \
&& cp ../zoo-services/hello-js/cgi-env/* /usr/lib/cgi-bin/ \
&& cp -r ../zoo-services/hello-nodejs/cgi-env/* /usr/lib/cgi-bin/ \
&& cp ../zoo-services/linestringDem/* /usr/lib/cgi-bin/ \
&& cp ../zoo-services/hello-r/cgi-env/* /usr/lib/cgi-bin/ \
&& cp ../zoo-api/js/* /usr/lib/cgi-bin/ \
&& cp ../zoo-api/r/minimal.r /usr/lib/cgi-bin/ \
Expand All @@ -145,6 +155,11 @@ RUN set -ex \
msgfmt $i -o /usr/local/share/locale/$(echo $i| sed "s:./locale/po/::g;s:.po::g")/LC_MESSAGES/zoo-kernel.mo ; \
done \
\
&& npm -g install gdal-async --build-from-source --shared_gdal \
&& npm -g install proj4 \
&& npm -g install bower \
&& npm -g install wps-js-52-north \
&& ( cd /usr/lib/cgi-bin/hello-nodejs && npm install ) \
#&& for lang in fr_FR ; do msgcat $(find ../zoo-services/ -name "${lang}.po") -o ${lang}.po ; done \
&& for lang in fr_FR ; do\
find ../zoo-services/ -name "${lang}*" ; \
Expand Down Expand Up @@ -212,6 +227,8 @@ ARG BUILD_DEPS=" \
libxml2-dev \
libxslt1-dev \
libcgal-dev \
libnode-dev \
node-addon-api \
"
WORKDIR /zoo-project
COPY ./zoo-project/zoo-services ./zoo-project/zoo-services
Expand All @@ -230,6 +247,9 @@ COPY --from=builder1 /zoo-project/zoo-project/zoo-kernel/service.h /zoo-project/
COPY --from=builder1 /zoo-project/zoo-project/zoo-kernel/service_internal.h /zoo-project/zoo-project/zoo-kernel/service_internal.h
COPY --from=builder1 /zoo-project/zoo-project/zoo-kernel/version.h /zoo-project/zoo-project/zoo-kernel/version.h

# Node.js global node_modules
COPY --from=builder1 /usr/lib/node_modules/ /usr/lib/node_modules/

RUN set -ex \
&& apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
\
Expand Down Expand Up @@ -299,13 +319,16 @@ ARG RUN_DEPS=" \
#Uncomment the lines below to add debuging \
#valgrind \
#gdb \
libnode93 \
"
ARG BUILD_DEPS=" \
make \
g++ \
gcc \
libgdal-dev \
python3-dev \
libnode-dev \
node-addon-api \
"
# For Azure use, uncomment bellow
#ARG SERVER_URL="http://zooprojectdemo.azurewebsites.net/"
Expand Down Expand Up @@ -338,6 +361,9 @@ COPY --from=builder1 /zoo-project/docker/.htaccess /var/www/html/.htaccess
COPY --from=builder1 /zoo-project/docker/default.conf /000-default.conf
COPY --from=builder1 /zoo-project/zoo-project/zoo-services/utils/open-api/server/publish.py /usr/lib/cgi-bin/publish.py

# Node.js global node_modules
COPY --from=builder1 /usr/lib/node_modules/ /usr/lib/node_modules/

# From optional zoo modules
COPY --from=builder2 /usr/lib/cgi-bin/ /usr/lib/cgi-bin/
COPY --from=builder1 /zoo-project/docker/oas.cfg /usr/lib/cgi-bin/
Expand Down
19 changes: 19 additions & 0 deletions Dockerfile-libnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM ubuntu:bionic-20201119 AS base

ARG NODE_VERSION=16.15.1

RUN set -ex && \
DEBIAN_FRONTEND=noninteractive apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y install python3 g++-8 make python3-pip wget

RUN wget https://github.com/nodejs/node/archive/refs/tags/v${NODE_VERSION}.tar.gz && \
mkdir -p /usr/src && \
tar -C /usr/src -zxvf v${NODE_VERSION}.tar.gz && \
cd /usr/src/node-${NODE_VERSION} && \
CXX=g++-8 ./configure --shared --prefix=/usr

RUN cd /usr/src/node-${NODE_VERSION} && make -j4

RUN cd /usr/src/node-${NODE_VERSION} && make install && \
install out/Release/node /usr/bin && \
cd /usr/lib && ln -s libnode.so.93 libnode.so
69 changes: 68 additions & 1 deletion docs/services/howtos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Sample ZOO C# Services Provider
:language: csharp
:lines: 24-100

Javascript
Javascript (SpiderMonkey)
----------

ZOO API
Expand Down Expand Up @@ -463,6 +463,73 @@ Sample ZOO Javascript Services Provider
return Array(3,outputs);
}
Javascript (Node.js)
----------

Environment
*********

The Node.js services environment offers a fully functional Node.js environment.
The ``gdal-async``, ``proj4`` and ``wps-js-52-north`` modules are supported
out of the box and can be imported/required. Loading of external CJS and ES6
modules installed from the NPM registry in a ``node_modules`` sub-directory is
also supported when this directory is present in the same directory as the
service JavaScript file.


Javascript ZCFG requirements
**********************************

.. Note:: For each Service provided by your ZOO Javascript Services Provider, the ZCFG File
must be named the same as the exported Javascript function name (also the case of
characters is important).

The ZCFG file should contain the following :

serviceType
NodeJS
serviceProvider
The name of the JavaScript file to use as a ZOO Service Provider. For instance, if your
script, located in the same directory as your ZOO Kernel, was named ``my_module.js`` then
you should use ``my_module.js``.
jsModuleType (optional, default is ``CJS``)
The module type used, ``CJS`` for CommonJS or ``ES6`` for ECMAScript 6 modules. CommonJS
modules should contain a ``modules.export = { serviceName: entryFunction }`` while
ECMAScript 6 modules should contain ``export serviceName``.
inspector (optional, default ``false``)
Enables the built-in debugger. When enabled the service won't run until a V8 Inspector
Protocol has connected to port 9229.


Javascript Data Structure used
********************************

The three parameters of the function are passed to the JavaScript function as Object. The main
entry point function should return one of the predefined values: ``SERVICE_ACCEPTED``,
``SERVICE_STARTED``, ``SERVICE_PAUSED``, ``SERVICE_SUCCEEDED`` or ``SERVICE_FAILED``.

Sample ZOO Node.js Javascript Services Provider
***********************************************

.. code-block:: javascript
function hellojs(conf,inputs,outputs){
outputs["result"]["value"]="Hello "+inputs["S"]["value"]+" from JS World !";
return SERVICE_SUCCEEDED;
}
module.exports = hellojs;
Using the Integrated Remote Debugger
************************************

The Node.js runtime supports debugging of Javascript services directly in their
normal execution environment. The debugger is enabled by setting ``inspector`` to ``true``
in the service configuration file. Once enabled, the debugger will automatically pause
the service upon launching it and it will wait for a V8 Inspector Protocol compliant client
to connect to port 9229. The Chrome browser includes one such client that is accessible
by typing ``chrome://inspect`` in the URL bar. Another popular debugging client is
Visual Studio Code.

R
-
Expand Down
21 changes: 21 additions & 0 deletions testing/requests/linestring.xml

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion zoo-project/zoo-api/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ JAVA_FILES=../../zoo-kernel/${JAVA_FILE}
ifeq ($(JS_ENABLED),-DUSE_JS)
JS_FILES=../../zoo-kernel/${JS_FILE},
endif
ifeq ($(NODEJS_ENABLED),-DUSE_NODEJS)
NODEJS_FILES=../../zoo-kernel/${NODEJS_FILE},
endif
ifeq ($(MS_FILE),service_internal_ms.o)
MS_FILES=../../zoo-kernel/${MS_FILE},
endif
Expand All @@ -33,7 +36,7 @@ zoo-api.o: zoo-api.c org_zoo_project_ZOO.h
gcc -fPIC -c zoo-api.c ${JAVACFLAGS} ${XML2CFLAGS} -I../../zoo-kernel/ -I../../../thirds/cgic206/

libZOO.${ext}: zoo-api.o
gcc zoo-api.o -shared -o libZOO.${ext} -Wl,${YAML_FILES}../../zoo-kernel/lex.cr.o,../../zoo-kernel/main_conf_read.tab.o,../../zoo-kernel/lex.sr.o,../../zoo-kernel/service_conf.tab.o,../../zoo-kernel/service_internal.o,../../zoo-kernel/server_internal.o,../../zoo-kernel/response_print.o,${JAVA_FILES},${MS_FILES}${JS_FILES}../../zoo-kernel/ulinet.o ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS} ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} -lcrypto ${MS_LDFLAGS} ${MACOS_LD_FLAGS} -lcurl -lfcgi ${MACOS_LD_NET_FLAGS} -lzoo_service ${YAML_LDFLAGS}
gcc zoo-api.o -shared -o libZOO.${ext} -Wl,${YAML_FILES}../../zoo-kernel/lex.cr.o,../../zoo-kernel/main_conf_read.tab.o,../../zoo-kernel/lex.sr.o,../../zoo-kernel/service_conf.tab.o,../../zoo-kernel/service_internal.o,../../zoo-kernel/server_internal.o,../../zoo-kernel/response_print.o,${JAVA_FILES},${MS_FILES}${JS_FILES}${NODEJS_FILES}../../zoo-kernel/ulinet.o ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS} ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} ${NODEJSLDFLAGS} -lcrypto ${MS_LDFLAGS} ${MACOS_LD_FLAGS} -lcurl -lfcgi ${MACOS_LD_NET_FLAGS} -lzoo_service ${YAML_LDFLAGS}

clean:
rm -f *${ext} *o *.h *class org/zoo_project/*.class
5 changes: 4 additions & 1 deletion zoo-project/zoo-api/java/makefile.vc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ MS_FILE=..\..\zoo-kernel/$(MS_FILE)
!IFDEF JS_FILE
JS_FILE=..\..\zoo-kernel/$(JS_FILE)
!ENDIF
!IFDEF NODEJS_FILE
NODEJS_FILE=..\..\zoo-kernel/$(NODEJS_FILE)
!ENDIF

all: libZOO.dll

Expand All @@ -29,7 +32,7 @@ zoo-api.o: zoo-api.c ZOO.h
$(CPP) /c $(CFLAGS) zoo-api.c -I../../zoo-kernel/ -I../../../thirds/cgic206/

libZOO.dll: zoo-api.o
link /dll /out:libZOO.dll zoo-api.obj ..\..\zoo-kernel\libzoo_service.lib ..\..\zoo-kernel\server_internal.obj ..\..\zoo-kernel\caching.obj ..\..\zoo-kernel\ulinet.obj ..\..\zoo-kernel\response_print.obj ..\..\zoo-kernel\service_json.obj ..\..\zoo-kernel\main_conf_read.tab.obj ..\..\zoo-kernel\service_conf.tab.obj ..\..\zoo-kernel\lex.cr.obj ..\..\zoo-kernel\lex.sr.obj $(JAVA_FILE) $(MS_FILE) $(JS_FILE) $(JSONC_LIB) $(LDFLAGS) /FORCE:MULTIPLE
link /dll /out:libZOO.dll zoo-api.obj ..\..\zoo-kernel\libzoo_service.lib ..\..\zoo-kernel\server_internal.obj ..\..\zoo-kernel\caching.obj ..\..\zoo-kernel\ulinet.obj ..\..\zoo-kernel\response_print.obj ..\..\zoo-kernel\service_json.obj ..\..\zoo-kernel\main_conf_read.tab.obj ..\..\zoo-kernel\service_conf.tab.obj ..\..\zoo-kernel\lex.cr.obj ..\..\zoo-kernel\lex.sr.obj $(JAVA_FILE) $(MS_FILE) $(JS_FILE) $(NODEJS_FILE) $(JSONC_LIB) $(LDFLAGS) /FORCE:MULTIPLE

clean:
erase -f *dll *lib *o *.h *class
Expand Down

0 comments on commit 1bb4ce4

Please sign in to comment.