Skip to content
Open
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
11 changes: 10 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ jobs:
- name: build
run: |
sudo apt update
sudo apt install libssl-dev libnghttp2-dev liblua5.4-dev libprotobuf-dev libprotoc-dev protobuf-compiler
sudo apt install libssl-dev libnghttp2-dev liblua5.4-dev libprotobuf-dev libprotoc-dev protobuf-compiler quickjs libquickjs
# Ubuntu packages libquickjs as a non-PIC static library, so JS is
# covered in a static libhv build instead of linking it into libhv.so.
make clean
./configure --disable-shared --with-http --with-mqtt --with-redis --with-js
make libhv hvjs unittest
bin/hvjs examples/js/sleep.js
make run-unittest
make clean
rm -f bin/hvjs bin/http_js_handler_test bin/http_js_redis_test bin/http_js_ws_test bin/http_js_mqtt_test
./configure --with-openssl --with-nghttp2 --with-kcp --with-mqtt --with-redis --with-lua --with-rpc
make libhv evpp
# hrpc = separate libhrpc (needs protobuf); apt installs protobuf under /usr
Expand Down
67 changes: 65 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ option(WITH_GNUTLS "with gnutls library" OFF)
option(WITH_MBEDTLS "with mbedtls library" OFF)

option(WITH_LUA "with lua library" OFF)
option(WITH_JS "with quickjs library" OFF)

option(WITH_KCP "compile event/kcp" OFF)

Expand Down Expand Up @@ -213,6 +214,50 @@ if(WITH_LUA)
endif()
endif()

if(WITH_JS)
add_definitions(-DWITH_JS)
find_path(QUICKJS_INCLUDE_DIR
NAMES quickjs.h
HINTS
${QUICKJS_ROOT}/include/quickjs
${QUICKJS_ROOT}/include
/opt/homebrew/opt/quickjs/include/quickjs
/opt/homebrew/opt/quickjs/include
/usr/local/opt/quickjs/include/quickjs
/usr/local/opt/quickjs/include
/usr/local/include/quickjs
/usr/local/include
/usr/include/quickjs
/usr/include)
find_library(QUICKJS_LIBRARY
NAMES quickjs libquickjs
HINTS
${QUICKJS_ROOT}/lib/quickjs
${QUICKJS_ROOT}/lib
/opt/homebrew/opt/quickjs/lib/quickjs
/opt/homebrew/opt/quickjs/lib
/usr/local/opt/quickjs/lib/quickjs
/usr/local/opt/quickjs/lib
/usr/local/lib/quickjs
/usr/local/lib
/usr/lib/quickjs
/usr/lib)
if(NOT QUICKJS_INCLUDE_DIR OR NOT QUICKJS_LIBRARY)
message(FATAL_ERROR "WITH_JS requires QuickJS. Set QUICKJS_ROOT or QUICKJS_INCLUDE_DIR and QUICKJS_LIBRARY.")
endif()
include_directories(${QUICKJS_INCLUDE_DIR})
set(LIBS ${LIBS} ${QUICKJS_LIBRARY})
if(WITH_EVPP AND WITH_HTTP AND WITH_HTTP_CLIENT)
add_definitions(-DHVJS_WITH_HTTP)
endif()
if(WITH_EVPP AND WITH_REDIS)
add_definitions(-DHVJS_WITH_REDIS)
endif()
if(WITH_EVPP AND WITH_MQTT)
add_definitions(-DHVJS_WITH_MQTT)
endif()
endif()

if(WIN32 OR MINGW)
add_definitions(-DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0600)
set(LIBS ${LIBS} secur32 crypt32 winmm iphlpapi ws2_32)
Expand Down Expand Up @@ -243,7 +288,7 @@ if(APPLE)
endif()

# see Makefile
set(ALL_SRCDIRS . base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt)
set(ALL_SRCDIRS . base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt js)
set(CORE_SRCDIRS . base ssl event)
if(WIN32 OR MINGW)
if(WITH_WEPOLL)
Expand Down Expand Up @@ -274,6 +319,9 @@ endif()
if(WITH_EVPP)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS} ${EVPP_HEADERS})
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} cpputil evpp)
if(WITH_JS)
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} js)
endif()
if(WITH_REDIS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${REDIS_HEADERS})
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} redis)
Expand All @@ -286,8 +334,14 @@ if(WITH_EVPP)
endif()
if(WITH_HTTP_SERVER)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
if(WITH_LUA OR WITH_JS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpScriptHandler.h)
endif()
if(WITH_LUA)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpScriptHandler.h http/server/HttpLuaHandler.h)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpLuaHandler.h)
endif()
if(WITH_JS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpJsHandler.h)
endif()
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
endif()
Expand All @@ -308,6 +362,15 @@ if(WITH_MQTT)
endif()

list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
if(NOT WITH_LUA)
list(FILTER LIBHV_SRCS EXCLUDE REGEX "(^|/)HttpLuaHandler\\.cpp$")
endif()
if(NOT WITH_JS)
list(FILTER LIBHV_SRCS EXCLUDE REGEX "(^|/)HttpJsHandler\\.cpp$")
endif()
if(NOT WITH_LUA AND NOT WITH_JS)
list(FILTER LIBHV_SRCS EXCLUDE REGEX "(^|/)HttpScriptHandler\\.cpp$")
endif()
if(WIN32)
set(CMAKE_RC_FLAGS_DEBUG -D_DEBUG)
configure_file(${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.rc.in ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.rc)
Expand Down
42 changes: 40 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include config.mk
include Makefile.vars

MAKEF=$(MAKE) -f Makefile.in
ALL_SRCDIRS=. base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt
ALL_SRCDIRS=. base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt js
CORE_SRCDIRS=. base ssl event
ifeq ($(WITH_KCP), yes)
CORE_SRCDIRS += event/kcp
Expand All @@ -29,6 +29,12 @@ LIBHV_SRCDIRS += cpputil
endif
endif

ifeq ($(WITH_JS), yes)
ifeq ($(WITH_EVPP), yes)
LIBHV_SRCDIRS += js
endif
endif

ifeq ($(WITH_EVPP), yes)
LIBHV_HEADERS += $(CPPUTIL_HEADERS) $(EVPP_HEADERS)
LIBHV_SRCDIRS += cpputil evpp
Expand All @@ -49,8 +55,14 @@ endif
ifeq ($(WITH_HTTP_SERVER), yes)
LIBHV_HEADERS += $(HTTP_SERVER_HEADERS)
LIBHV_SRCDIRS += http/server
ifneq ($(filter yes,$(WITH_LUA) $(WITH_JS)),)
LIBHV_HEADERS += http/server/HttpScriptHandler.h
endif
ifeq ($(WITH_LUA), yes)
LIBHV_HEADERS += http/server/HttpScriptHandler.h http/server/HttpLuaHandler.h
LIBHV_HEADERS += http/server/HttpLuaHandler.h
endif
ifeq ($(WITH_JS), yes)
LIBHV_HEADERS += http/server/HttpJsHandler.h
endif
endif

Expand Down Expand Up @@ -115,6 +127,11 @@ ifeq ($(WITH_EVPP), yes)
EXAMPLES += hvlua
endif
endif
ifeq ($(WITH_JS), yes)
ifeq ($(WITH_EVPP), yes)
EXAMPLES += hvjs
endif
endif

examples: $(EXAMPLES)
@echo "make examples done."
Expand Down Expand Up @@ -228,6 +245,9 @@ host: prepare
hvlua: prepare libhv
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_LUA $(LUA_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ilua -o bin/hvlua examples/hvlua.cpp -Llib -lhv -pthread $(LUA_LIBS)

hvjs: prepare libhv
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ijs -o bin/hvjs examples/hvjs.cpp -Llib -lhv -pthread $(JS_LIBS)

multi-acceptor-processes: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/multi-thread/multi-acceptor-processes.c"

Expand Down Expand Up @@ -424,6 +444,24 @@ ifeq ($(WITH_REDIS), yes)
endif
endif
endif
ifeq ($(WITH_JS), yes)
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_HTTP), yes)
ifeq ($(WITH_HTTP_SERVER), yes)
ifeq ($(WITH_HTTP_CLIENT), yes)
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -o bin/http_js_handler_test unittest/http_js_handler_test.cpp -Llib -lhv -pthread $(JS_LIBS)
ifeq ($(WITH_REDIS), yes)
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP -DHVJS_WITH_REDIS $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -Iredis -o bin/http_js_redis_test unittest/http_js_redis_test.cpp unittest/redis_test_server.cpp -Llib -lhv -pthread $(JS_LIBS)
endif
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -o bin/http_js_ws_test unittest/http_js_ws_test.cpp -Llib -lhv -pthread $(JS_LIBS)
ifeq ($(WITH_MQTT), yes)
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP -DHVJS_WITH_MQTT $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -Imqtt -o bin/http_js_mqtt_test unittest/http_js_mqtt_test.cpp -Llib -lhv -pthread $(JS_LIBS)
endif
endif
endif
endif
endif
endif

run-unittest: unittest
bash scripts/unittest.sh
Expand Down
33 changes: 33 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ ifeq ($(ALL_SRCS), )
ALL_SRCS = $(wildcard *.c *.cc *.cpp)
endif
override SRCS += $(filter-out %_test.c %_test.cc %_test.cpp, $(ALL_SRCS))
ifeq ($(filter clean,$(MAKECMDGOALS)),)
ifneq ($(WITH_LUA), yes)
override SRCS := $(filter-out %/HttpLuaHandler.cpp HttpLuaHandler.cpp, $(SRCS))
endif
ifneq ($(WITH_JS), yes)
override SRCS := $(filter-out %/HttpJsHandler.cpp HttpJsHandler.cpp, $(SRCS))
endif
ifeq ($(filter yes,$(WITH_LUA) $(WITH_JS)),)
override SRCS := $(filter-out %/HttpScriptHandler.cpp HttpScriptHandler.cpp, $(SRCS))
endif
endif
# OBJS += $(patsubst %.c, %.o, $(SRCS))
# OBJS += $(patsubst %.cc, %.o, $(SRCS))
# OBJS += $(patsubst %.cpp, %.o, $(SRCS))
Expand Down Expand Up @@ -185,6 +196,28 @@ endif
endif
endif

ifeq ($(WITH_JS), yes)
CPPFLAGS += -DWITH_JS $(JS_CFLAGS)
LDFLAGS += $(JS_LIBS)
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_HTTP), yes)
ifeq ($(WITH_HTTP_CLIENT), yes)
CPPFLAGS += -DHVJS_WITH_HTTP
endif
endif
endif
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_REDIS), yes)
CPPFLAGS += -DHVJS_WITH_REDIS
endif
endif
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_MQTT), yes)
CPPFLAGS += -DHVJS_WITH_MQTT
endif
endif
endif

CPPFLAGS += $(addprefix -D, $(DEFINES))
CPPFLAGS += $(addprefix -I, $(INCDIRS))
CPPFLAGS += $(addprefix -I, $(SRCDIRS))
Expand Down
5 changes: 5 additions & 0 deletions Makefile.vars
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ LUA_PREFIX ?= $(shell for dir in /opt/homebrew/opt/lua /usr/local/opt/lua /usr;
LUA_INCLUDE_DIR ?= $(shell if [ -n "$(LUA_PREFIX)" ]; then for dir in "$(LUA_PREFIX)/include/lua" "$(LUA_PREFIX)/include/lua5.5" "$(LUA_PREFIX)/include/lua5.4" "$(LUA_PREFIX)/include/lua5.3" "$(LUA_PREFIX)/include"; do if [ -f "$$dir/lua.h" ]; then echo $$dir; break; fi; done; fi)
LUA_CFLAGS ?= $(shell if [ -n "$(LUA_PKG_CONFIG)" ]; then $(PKG_CONFIG) --cflags $(LUA_PKG_CONFIG); elif [ -n "$(LUA_INCLUDE_DIR)" ]; then echo -I$(LUA_INCLUDE_DIR); fi)
LUA_LIBS ?= $(shell if [ -n "$(LUA_PKG_CONFIG)" ]; then $(PKG_CONFIG) --libs $(LUA_PKG_CONFIG); elif [ -n "$(LUA_PREFIX)" ]; then echo -L$(LUA_PREFIX)/lib -llua; else echo -llua; fi)
QUICKJS_ROOT ?= $(shell for dir in /opt/homebrew/opt/quickjs /usr/local/opt/quickjs /usr; do if [ -f "$$dir/include/quickjs/quickjs.h" ] || [ -f "$$dir/include/quickjs.h" ]; then echo $$dir; break; fi; done)
QUICKJS_INCLUDE_DIR ?= $(shell if [ -n "$(QUICKJS_ROOT)" ]; then for dir in "$(QUICKJS_ROOT)/include/quickjs" "$(QUICKJS_ROOT)/include"; do if [ -f "$$dir/quickjs.h" ]; then echo $$dir; break; fi; done; fi)
QUICKJS_LIB_DIR ?= $(shell if [ -n "$(QUICKJS_ROOT)" ]; then for dir in "$(QUICKJS_ROOT)/lib/quickjs" "$(QUICKJS_ROOT)/lib"; do if [ -f "$$dir/libquickjs.a" ] || [ -f "$$dir/libquickjs.dylib" ] || [ -f "$$dir/libquickjs.so" ]; then echo $$dir; break; fi; done; fi)
JS_CFLAGS ?= $(shell if [ -n "$(QUICKJS_INCLUDE_DIR)" ]; then echo -I$(QUICKJS_INCLUDE_DIR); fi)
JS_LIBS ?= $(shell if [ -n "$(QUICKJS_LIB_DIR)" ]; then echo -L$(QUICKJS_LIB_DIR) -lquickjs; else echo -lquickjs; fi)

BASE_HEADERS = base/hplatform.h\
\
Expand Down
2 changes: 2 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ WITH_GNUTLS=no
WITH_MBEDTLS=no
# for http lua handler
WITH_LUA=no
# for http js handler (QuickJS)
WITH_JS=no

# rudp
WITH_KCP=no
Expand Down
2 changes: 2 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ modules:
--with-redis compile redis module? (DEFAULT: $WITH_REDIS)
--with-rpc compile hrpc (libhrpc, needs protobuf)? (DEFAULT: $WITH_RPC)
--with-lua compile lua module? (DEFAULT: $WITH_LUA)
--with-js compile js module? (DEFAULT: $WITH_JS)

features:
--enable-uds enable Unix Domain Socket? (DEFAULT: $ENABLE_UDS)
Expand Down Expand Up @@ -305,6 +306,7 @@ option=ENABLE_UDS && check_option
option=USE_MULTIMAP && check_option
option=WITH_KCP && check_option
option=WITH_IO_URING && check_option
option=WITH_JS && check_option

# end confile
cat << END >> $confile
Expand Down
1 change: 1 addition & 0 deletions docs/PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- redis client
- async DNS
- lua binding
- http js script handler
- hrpc = libhv + protobuf

## Plan
Expand Down
Loading
Loading