├── .github └── workflows │ └── test.yml ├── .gitignore ├── .gitmodules ├── CHANGELOG.md ├── Makefile ├── README.md └── simhash_server.cpp /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: actions/checkout@v4 13 | with: 14 | submodules: recursive 15 | 16 | - name: make 17 | run: make 18 | - name: Start server 19 | run: ./simhash.server & 20 | - name: Wait for server to start 21 | run: | 22 | timeout=10 23 | while ! curl "http://127.0.0.1:11201/?s=你好世界"; do 24 | if [ $timeout -le 0 ]; then 25 | echo "Server failed to start" 26 | exit 1 27 | fi 28 | timeout=$(($timeout - 1)) 29 | echo 'sleep 1' 30 | sleep 1 31 | done 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | simhash.server 2 | *.swp 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "submodules/simhash"] 2 | path = submodules/simhash 3 | url = https://github.com/yanyiwu/simhash 4 | [submodule "submodules/husky"] 5 | path = submodules/husky 6 | url = https://github.com/yanyiwu/husky.git 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## v0.2.0 4 | 5 | + Add git submodule for `submodules/` directory 6 | 7 | ## v0.1.0 8 | 9 | + Initial release 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -o simhash.server -O3 -DLOGGER_LEVEL=LL_INFO -Wall -g -I./submodules/husky/include -I./submodules/simhash/include -I./submodules/simhash/submodules/cppjieba/include/ -I./submodules/simhash/submodules/cppjieba/deps/limonp/include/ simhash_server.cpp -lpthread 3 | osx: 4 | g++ -o simhash.server -O3 -DLOGGER_LEVEL=LL_INFO -Wall -g -std=c++11 -I./include -I./include/CppJieba/ simhash_server.cpp -lpthread 5 | clean: 6 | rm -f *.o *.a simhash.server 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simhash Server 2 | 3 | [![Test](https://github.com/yanyiwu/simhash_server/actions/workflows/test.yml/badge.svg)](https://github.com/yanyiwu/simhash_server/actions/workflows/test.yml) 4 | 5 | ## Introduction 6 | 7 | HTTP Server based on [simhash] 8 | 9 | ## Usage 10 | 11 | Dependencies: 12 | 13 | + `g++ (version >= 4.1 recommended) or clang++`; 14 | 15 | ```sh 16 | git clone --recurse-submodules https://github.com/yanyiwu/simhash_server.git 17 | ``` 18 | 19 | ```sh 20 | make 21 | cd keyword_server 22 | ``` 23 | 24 | if you are using MacOSX, please use `make osx` . 25 | 26 | ``` 27 | ./simhash.server 28 | ``` 29 | 30 | ``` 31 | curl "http://127.0.0.1:11201/?s=你好世界" 32 | ``` 33 | 34 | ``` 35 | curl -d "我是蓝翔 技工拖拉机学院手扶拖拉机专业的。" "http://127.0.0.1:11201" 36 | ``` 37 | 38 | [simhash]:http://github.com/yanyiwu/simhash 39 | -------------------------------------------------------------------------------- /simhash_server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "thread_pool_server.h" 7 | #include "simhash/Simhasher.hpp" 8 | 9 | using namespace husky; 10 | using namespace simhash; 11 | 12 | const size_t PORT = 11201; 13 | const size_t THREAD_NUMBER = 4; 14 | const char* const DICT_PATH = "./submodules/simhash/submodules/cppjieba/dict/jieba.dict.utf8"; 15 | const char* const MODEL_PATH = "./submodules/simhash/submodules/cppjieba/dict/hmm_model.utf8"; 16 | const char* const IDF_PATH = "./submodules/simhash/submodules/cppjieba/dict/idf.utf8"; 17 | const char* const STOP_WORDS_PATH = "./submodules/simhash/submodules/cppjieba/dict/stop_words.utf8"; 18 | const size_t TOP_N = 5; 19 | 20 | class ReqHandler: public IRequestHandler 21 | { 22 | public: 23 | ReqHandler(const string& dictPath, const string& modelPath, const string& idfPath, const string& stopwordsPath): _simasher(dictPath, modelPath, idfPath, stopwordsPath){}; 24 | virtual ~ReqHandler(){}; 25 | public: 26 | virtual bool DoGET(const HttpReqInfo& httpReq, string& strSnd) 27 | { 28 | string s, tmp; 29 | httpReq.GET("s", tmp); 30 | URLDecode(tmp, s); 31 | uint64_t u64; 32 | bool ok = _simasher.make(s, TOP_N, u64); 33 | strSnd << u64; 34 | return ok; 35 | } 36 | virtual bool DoPOST(const HttpReqInfo& httpReq, string& strSnd) 37 | { 38 | const string& s = httpReq.GetBody(); 39 | uint64_t u64; 40 | bool ok = _simasher.make(s, TOP_N, u64); 41 | strSnd << u64; 42 | return ok; 43 | } 44 | private: 45 | Simhasher _simasher; 46 | }; 47 | 48 | int main(int argc, char* argv[]) 49 | { 50 | ReqHandler reqHandler(DICT_PATH, MODEL_PATH, IDF_PATH, STOP_WORDS_PATH); 51 | ThreadPoolServer server(THREAD_NUMBER, PORT, reqHandler); 52 | server.Start(); 53 | return EXIT_SUCCESS; 54 | } 55 | 56 | --------------------------------------------------------------------------------