Sfoglia il codice sorgente

move consul => examples

hewei.it 4 anni fa
parent
commit
37ac0d2c91
13 ha cambiato i file con 35 aggiunte e 68 eliminazioni
  1. 1 8
      CMakeLists.txt
  2. 12 13
      Makefile
  3. 0 2
      Makefile.vars
  4. 0 1
      TREE.md
  5. 0 2
      cmake/vars.cmake
  6. 0 3
      config.ini
  7. 1 2
      config.mk
  8. 0 1
      configure
  9. 15 19
      examples/CMakeLists.txt
  10. 3 12
      examples/consul/consul.cpp
  11. 3 5
      examples/consul/consul.h
  12. 0 0
      examples/consul/main.cpp
  13. 0 0
      examples/nmap/main.cpp

+ 1 - 8
CMakeLists.txt

@@ -16,9 +16,6 @@ option(WITH_HTTP "compile http" ON)
 option(WITH_HTTP_SERVER "compile http/server" ON)
 option(WITH_HTTP_CLIENT "compile http/client" ON)
 
-# WITH_CONSUL need WITH_HTTP_CLIENT=ON
-option(WITH_CONSUL "compile consul" OFF)
-
 option(ENABLE_IPV6 "ipv6" OFF)
 option(ENABLE_UDS "Unix Domain Socket" OFF)
 option(ENABLE_WINDUMP "Windows MiniDumpWriteDump" OFF)
@@ -134,7 +131,7 @@ if(ANDROID)
 endif()
 
 # see Makefile
-set(ALL_SRCDIRS . base util event protocol cpputil evpp http http/client http/server consul)
+set(ALL_SRCDIRS . base util event protocol cpputil evpp http http/client http/server)
 set(LIBHV_SRCDIRS . base util event)
 set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
 set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${UTIL_HEADERS} ${EVENT_HEADERS})
@@ -157,10 +154,6 @@ if(WITH_EVPP)
         if(WITH_HTTP_CLIENT)
             set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
             set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
-            if(WITH_CONSUL)
-                set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CONSUL_HEADERS})
-                set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} consul)
-            endif()
         endif()
     endif()
 endif()

+ 12 - 13
Makefile

@@ -2,7 +2,7 @@ include config.mk
 include Makefile.vars
 
 MAKEF=$(MAKE) -f Makefile.in
-ALL_SRCDIRS=. base util event protocol cpputil evpp http http/client http/server consul
+ALL_SRCDIRS=. base util event protocol cpputil evpp http http/client http/server
 
 LIBHV_SRCDIRS = . base util event
 LIBHV_HEADERS = hv.h hconfig.h hexport.h
@@ -16,28 +16,28 @@ endif
 ifeq ($(WITH_EVPP), yes)
 LIBHV_HEADERS += $(CPPUTIL_HEADERS) $(EVPP_HEADERS)
 LIBHV_SRCDIRS += cpputil evpp
+
 ifeq ($(WITH_HTTP), yes)
 LIBHV_HEADERS += $(HTTP_HEADERS)
 LIBHV_SRCDIRS += http
+
 ifeq ($(WITH_HTTP_SERVER), yes)
 LIBHV_HEADERS += $(HTTP_SERVER_HEADERS)
 LIBHV_SRCDIRS += http/server
 endif
+
 ifeq ($(WITH_HTTP_CLIENT), yes)
 LIBHV_HEADERS += $(HTTP_CLIENT_HEADERS)
 LIBHV_SRCDIRS += http/client
-ifeq ($(WITH_CONSUL), yes)
-LIBHV_HEADERS += $(CONSUL_HEADERS)
-LIBHV_SRCDIRS += consul
-endif
 endif
+
 endif
 endif
 
 default: all
 all: libhv examples
 examples: hmain_test htimer_test hloop_test \
-	nc nmap httpd curl wget \
+	nc nmap httpd curl wget consul\
 	tcp_echo_server \
 	tcp_chat_server \
 	tcp_proxy_server \
@@ -46,11 +46,10 @@ examples: hmain_test htimer_test hloop_test \
 	http_server_test http_client_test \
 	websocket_server_test \
 	websocket_client_test \
-	consul_cli
 
 clean:
-	$(MAKEF) clean SRCDIRS="$(ALL_SRCDIRS)"
-	$(RM) examples/*.o examples/httpd/*.o examples/nmap/*.o
+	$(MAKEF) clean SRCDIRS="$(ALL_SRCDIRS) examples/nmap examples/httpd examples/consul"
+	${RM} examples/*.o
 	$(RM) include/hv
 
 prepare:
@@ -96,12 +95,15 @@ nc: prepare
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event" SRCS="examples/nc.c"
 
 nmap: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base event cpputil examples/nmap" SRCS="examples/nmap/nmap_test.cpp" DEFINES="PRINT_DEBUG"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event cpputil examples/nmap" DEFINES="PRINT_DEBUG"
 
 httpd: prepare
 	$(RM) examples/httpd/*.o
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client http/server examples/httpd"
 
+consul: prepare
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client examples/consul" DEFINES="PRINT_DEBUG"
+
 curl: prepare
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/curl.cpp"
 	# $(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/curl.cpp" WITH_CURL=yes
@@ -121,9 +123,6 @@ websocket_server_test: prepare
 websocket_client_test: prepare
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/websocket_client_test.cpp"
 
-consul_cli: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client consul" SRCS="examples/consul_cli.cpp" DEFINES="PRINT_DEBUG"
-
 unittest: prepare
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/mkdir_p           unittest/mkdir_test.c         base/hbase.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/rmdir_p           unittest/rmdir_test.c         base/hbase.c

+ 0 - 2
Makefile.vars

@@ -83,5 +83,3 @@ HTTP_SERVER_HEADERS =   http/server/HttpService.h\
 						http/server/HttpServer.h\
 						http/server/HttpResponseWriter.h\
 						http/server/WebSocketServer.h\
-
-CONSUL_HEADERS = consul/consul.h

+ 0 - 1
TREE.md

@@ -7,7 +7,6 @@
 ├── build       cmake默认构建目录
 ├── cert        SSL证书存放目录
 ├── cmake       cmake脚本存放目录
-├── consul      consul服务注册与发现,使用http客户端实现
 ├── cpputil     libhv工具类,如命令行解析、json解析、ini解析
 ├── docs        文档存放目录
 ├── echo-servers 包含libevent、libev、libuv、libhv、asio、poco、muduo等多个网络库的tcp echo server写法,并做压力测试

+ 0 - 2
cmake/vars.cmake

@@ -93,5 +93,3 @@ set(HTTP_SERVER_HEADERS
     http/server/HttpResponseWriter.h
     http/server/WebSocketServer.h
 )
-
-set(CONSUL_HEADERS consul/consul.h)

+ 0 - 3
config.ini

@@ -14,9 +14,6 @@ WITH_HTTP=yes
 WITH_HTTP_SERVER=yes
 WITH_HTTP_CLIENT=yes
 
-# WITH_CONSUL need WITH_HTTP_CLIENT=yes
-WITH_CONSUL=no
-
 # features
 # base/hsocket.c: replace gethostbyname with getaddrinfo
 ENABLE_IPV6=no

+ 1 - 2
config.mk

@@ -7,7 +7,6 @@ WITH_EVPP=yes
 WITH_HTTP=yes
 WITH_HTTP_SERVER=yes
 WITH_HTTP_CLIENT=yes
-WITH_CONSUL=no
 ENABLE_IPV6=no
 ENABLE_UDS=no
 ENABLE_WINDUMP=no
@@ -16,4 +15,4 @@ WITH_CURL=no
 WITH_NGHTTP2=no
 WITH_OPENSSL=no
 WITH_MBEDTLS=no
-CONFIG_DATE=20210430
+CONFIG_DATE=20210430

+ 0 - 1
configure

@@ -23,7 +23,6 @@ modules:
   --with-http           compile http module?            (DEFAULT: $WITH_HTTP)
   --with-http-client    compile http client module?     (DEFAULT: $WITH_HTTP_CLIENT)
   --with-http-server    compile http server module?     (DEFAULT: $WITH_HTTP_SERVER)
-  --with-consul         compile consul module? default: (DEFAULT: $WITH_CONSUL)
 
 features:
   --enable-ipv6         enable IPv6?                    (DEFAULT: $ENABLE_IPV6)

+ 15 - 19
examples/CMakeLists.txt

@@ -42,7 +42,7 @@ if(WITH_EVPP)
 
     aux_source_directory(nmap NMAP_SRCS)
     add_executable(nmap ${NMAP_SRCS})
-    target_compile_definitions(nmap PRIVATE -DPRINT_DEBUG)
+    target_compile_definitions(nmap PRIVATE PRINT_DEBUG)
     target_link_libraries(nmap hv)
 
     list(APPEND EXAMPLES hmain_test nmap)
@@ -80,6 +80,12 @@ if(WITH_HTTP_CLIENT)
     add_executable(wget wget.cpp)
     target_link_libraries(wget hv)
 
+    # consul
+    aux_source_directory(consul CONSUL_SRCS)
+    add_executable(consul ${CONSUL_SRCS})
+    target_compile_definitions(consul PRIVATE PRINT_DEBUG)
+    target_link_libraries(consul hv)
+
     # http_client_test
     add_executable(http_client_test http_client_test.cpp)
     target_link_libraries(http_client_test hv)
@@ -88,25 +94,15 @@ if(WITH_HTTP_CLIENT)
     add_executable(websocket_client_test websocket_client_test.cpp)
     target_link_libraries(websocket_client_test hv)
 
-    list(APPEND EXAMPLES ${CURL_TARGET_NAME} wget http_client_test websocket_client_test)
-endif()
-
-if(WITH_CONSUL)
-    include_directories(../consul)
-
-    add_executable(consul_cli consul_cli.cpp)
-    target_compile_definitions(consul_cli PRIVATE PRINT_DEBUG)
-    target_link_libraries(consul_cli hv)
+    list(APPEND EXAMPLES ${CURL_TARGET_NAME} wget consul http_client_test websocket_client_test)
 
-    list(APPEND EXAMPLES consul_cli)
-endif()
-
-if(WITH_HTTP_SERVER)
-    # httpd
-    aux_source_directory(httpd HTTPD_SRCS)
-    add_executable(httpd ${HTTPD_SRCS})
-    target_link_libraries(httpd hv)
-    list(APPEND EXAMPLES httpd)
+    if(WITH_HTTP_SERVER)
+        # httpd
+        aux_source_directory(httpd HTTPD_SRCS)
+        add_executable(httpd ${HTTPD_SRCS})
+        target_link_libraries(httpd hv)
+        list(APPEND EXAMPLES httpd)
+    endif()
 endif()
 
 endif()

+ 3 - 12
consul/consul.cpp → examples/consul/consul.cpp

@@ -5,9 +5,6 @@
 #include "json.hpp"
 using json = nlohmann::json;
 
-#include "hstring.h"
-#include "herr.h"
-
 #define PROTOCOL    "http://"
 #define API_VERSION "v1"
 
@@ -112,18 +109,12 @@ int discover_services(consul_node_t* node, const char* service_name, std::vector
     HttpResponse res;
     printd("GET %s\n", req.url.c_str());
     int ret = http_client_send(&req, &res);
-    if (ret != 0) {
-        return ret;
-    }
+    if (ret != 0) return ret;
     printd("%s\n", res.body.c_str());
 
     json jroot = json::parse(res.body);
-    if (!jroot.is_array()) {
-        return ERR_INVALID_JSON;
-    }
-    if (jroot.size() == 0) {
-        return 0;
-    }
+    if (!jroot.is_array()) return -1;
+    if (jroot.size() == 0) return 0;
 
     consul_service_t service;
     std::string name, ip;

+ 3 - 5
consul/consul.h → examples/consul/consul.h

@@ -4,8 +4,6 @@
 #include <vector>
 #include <string.h>
 
-#include "hexport.h"
-
 typedef struct consul_node_s {
     // node
     char ip[64];
@@ -48,8 +46,8 @@ typedef struct consul_health_s {
     }
 } consul_health_t;
 
-HV_EXPORT int register_service(consul_node_t* node, consul_service_t* service, consul_health_t* health);
-HV_EXPORT int deregister_service(consul_node_t* node, consul_service_t* service);
-HV_EXPORT int discover_services(consul_node_t* node, const char* service_name, std::vector<consul_service_t>& services);
+int register_service(consul_node_t* node, consul_service_t* service, consul_health_t* health);
+int deregister_service(consul_node_t* node, consul_service_t* service);
+int discover_services(consul_node_t* node, const char* service_name, std::vector<consul_service_t>& services);
 
 #endif // CONSUL_H_

+ 0 - 0
examples/consul_cli.cpp → examples/consul/main.cpp


+ 0 - 0
examples/nmap/nmap_test.cpp → examples/nmap/main.cpp