Line data Source code
1 : #ifndef SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_THRIFT_H_
2 : #define SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_THRIFT_H_
3 :
4 : #include <string>
5 : #include <nlohmann/json.hpp>
6 : #include <thrift/transport/TServerSocket.h>
7 : #include <thrift/transport/TSSLSocket.h>
8 : #include <thrift/transport/TSSLServerSocket.h>
9 :
10 : namespace social_network{
11 : using json = nlohmann::json;
12 : using apache::thrift::transport::TServerSocket;
13 : using apache::thrift::transport::TSSLServerSocket;
14 : using apache::thrift::transport::TSSLSocketFactory;
15 :
16 1 : std::shared_ptr<TServerSocket> get_server_socket(const json &config_json, const std::string &address, int port) {
17 1 : bool ssl_enabled = config_json["ssl"]["enabled"];
18 1 : if (ssl_enabled) {
19 0 : std::string cert_path = config_json["ssl"]["serverCertPath"];
20 0 : std::string key_path = config_json["ssl"]["serverKeyPath"];
21 0 : std::string ca_path = config_json["ssl"]["caPath"];
22 0 : std::string ciphers = config_json["ssl"]["ciphers"];
23 :
24 0 : std::shared_ptr<TSSLSocketFactory> ssl_socket_factory;
25 0 : ssl_socket_factory = std::make_shared<TSSLSocketFactory>();
26 0 : ssl_socket_factory->loadCertificate(cert_path.c_str());
27 0 : ssl_socket_factory->loadPrivateKey(key_path.c_str());
28 0 : ssl_socket_factory->ciphers(ciphers);
29 : // if (config_json["ssl"]["verifyClient"]) {
30 : // ssl_socket_factory->loadTrustedCertificates(ca_path.c_str());
31 : // ssl_socket_factory->authenticate(true);
32 : // }
33 0 : return std::make_shared<TSSLServerSocket>(address, port, ssl_socket_factory);
34 : }
35 1 : return std::make_shared<TServerSocket>(address, port);
36 : };
37 :
38 : } //namespace social_network
39 :
40 : #endif //SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_THRIFT_H_
|