Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Parse configuration from both env vars and command line args.
Kishore Nallan374ab9789bc8
diff --git a/include/config.h b/include/config.hnew file mode 100644index 00000000..6f945f38--- /dev/null+++ b/include/config.h@@ -0,0 +1,187 @@+#pragma once++#include <cmdline.h>+#include "option.h"+#include "string_utils.h"++class Config {+private:+ std::string data_dir;+ std::string log_dir;++ std::string api_key;+ std::string search_only_api_key;++ std::string listen_address;+ uint32_t listen_port;++ std::string master;++ std::string ssl_certificate;+ std::string ssl_certificate_key;++ bool enable_cors;++public:++ Config() {+ this->listen_address = "0.0.0.0";+ this->listen_port = 8108;+ this->enable_cors = false;+ }++ // setters++ void set_data_dir(std::string & data_dir) {+ this->data_dir = data_dir;+ }++ void set_log_dir(std::string & log_dir) {+ this->log_dir = log_dir;+ }++ void set_api_key(std::string & api_key) {+ this->api_key = api_key;+ }++ void set_search_only_api_key(std::string & search_only_api_key) {+ this->search_only_api_key = search_only_api_key;+ }++ void set_listen_address(std::string & listen_address) {+ this->listen_address = listen_address;+ }++ void set_listen_port(int listen_port) {+ this->listen_port = listen_port;+ }++ void set_master(std::string & master) {+ this->master = master;+ }++ void set_ssl_cert(std::string & ssl_cert) {+ this->ssl_certificate = ssl_cert;+ }++ void set_ssl_cert_key(std::string & ssl_cert_key) {+ this->ssl_certificate_key = ssl_cert_key;+ }++ void set_enable_cors(bool enable_cors) {+ this->enable_cors = enable_cors;+ }++ // getters++ std::string get_data_dir() {+ return this->data_dir;+ }++ std::string get_log_dir() {+ return this->log_dir;+ }++ std::string get_api_key() {+ return this->api_key;+ }++ std::string get_search_only_api_key() {+ return this->search_only_api_key;+ }++ std::string get_listen_address() {+ return this->listen_address;+ }++ int get_listen_port() {+ return this->listen_port;+ }++ std::string get_master() {+ return this->master;+ }++ std::string get_ssl_cert() {+ return this->ssl_certificate;+ }++ std::string get_ssl_cert_key() {+ return this->ssl_certificate_key;+ }++ bool get_enable_cors() {+ return this->enable_cors;+ }++ // loaders++ std::string get_env(const char *name) {+ const char *ret = getenv(name);+ if (!ret) {+ return std::string();+ }+ return std::string(ret);+ }++ void load_config_env() {+ this->data_dir = get_env("TYPESENSE_DATA_DIR");+ this->log_dir = get_env("TYPESENSE_LOG_DIR");+ this->api_key = get_env("TYPESENSE_API_KEY");++ this->search_only_api_key = get_env("TYPESENSE_SEARCH_ONLY_API_KEY");++ if(!get_env("TYPESENSE_LISTEN_ADDRESS").empty()) {+ this->listen_address = get_env("TYPESENSE_LISTEN_ADDRESS");+ }++ if(!get_env("TYPESENSE_LISTEN_PORT").empty()) {+ this->listen_port = std::stoi(get_env("TYPESENSE_LISTEN_PORT"));+ }++ this->master = get_env("TYPESENSE_MASTER");+ this->ssl_certificate = get_env("TYPESENSE_SSL_CERTIFICATE");+ this->ssl_certificate_key = get_env("TYPESENSE_SSL_CERTIFICATE_KEY");++ std::string enable_cors_str = get_env("TYPESENSE_ENABLE_CORS");+ StringUtils::toupper(enable_cors_str);+ this->enable_cors = ("TRUE" == enable_cors_str) ? true : false;+ }++ void load_config_cmd_args(cmdline::parser & options) {+ this->data_dir = options.get<std::string>("data-dir");+ this->log_dir = options.get<std::string>("log-dir");+ this->api_key = options.get<std::string>("api-key");++ this->search_only_api_key = options.get<std::string>("search-only-api-key");++ if(options.exist("listen-address")) {+ this->listen_address = options.get<std::string>("listen-address");+ }++ if(options.exist("listen-port")) {+ this->listen_port = options.get<uint32_t>("listen-port");+ }++ this->master = options.get<std::string>("master");+ this->ssl_certificate = options.get<std::string>("ssl-certificate");+ this->ssl_certificate_key = options.get<std::string>("ssl-certificate-key");++ this->enable_cors = options.exist("enable-cors");+ }++ // validation++ Option<bool> is_valid() {+ if(data_dir.empty()) {+ return Option<bool>(404, "Data directory is not specified.");+ }++ if(api_key.empty()) {+ return Option<bool>(404, "API key is not specified.");+ }++ return Option<bool>(true);+ }++};\ No newline at end of filediff --git a/include/typesense_server_utils.h b/include/typesense_server_utils.hindex 13a962dd..9e4aba1c 100644--- a/include/typesense_server_utils.h+++ b/include/typesense_server_utils.h@@ -3,6 +3,7 @@#include <string>#include <iostream>#include <cmdline.h>+#include "config.h"#include "logger.h"#include "store.h"#include "collection_manager.h"@@ -20,6 +21,6 @@ bool directory_exists(const std::string & dir_path);void init_cmdline_options(cmdline::parser & options, int argc, char **argv);-int init_logger(cmdline::parser & options, std::unique_ptr<g3::LogWorker> & log_worker);+int init_logger(Config & config, std::unique_ptr<g3::LogWorker> & log_worker);-int run_server(cmdline::parser & options, void (*master_server_routes)(), void (*replica_server_routes)());\ No newline at end of file+int run_server(Config & config, void (*master_server_routes)(), void (*replica_server_routes)());\ No newline at end of filediff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cppindex 0f768e66..3ddcb15e 100644--- a/src/main/typesense_server.cpp+++ b/src/main/typesense_server.cpp@@ -1,4 +1,5 @@#include "core_api.h"+#include "config.h"#include "typesense_server_utils.h"void master_server_routes() {@@ -41,15 +42,29 @@ void replica_server_routes() {}int main(int argc, char **argv) {+ Config config;+cmdline::parser options;init_cmdline_options(options, argc, argv);- options.parse_check(argc, argv);+ options.parse(argc, argv);++ // Command line args override env vars+ config.load_config_env();+ config.load_config_cmd_args(options);++ Option<bool> config_validitation = config.is_valid();++ if(!config_validitation.ok()) {+ std::cerr << "Invalid configuration: " << config_validitation.error() << std::endl;+ std::cerr << "Command line " << options.usage() << std::endl;+ exit(1);+ }std::unique_ptr<g3::LogWorker> log_worker = g3::LogWorker::createLogWorker();- int ret_code = init_logger(options, log_worker);+ int ret_code = init_logger(config, log_worker);if(ret_code != 0) {return ret_code;}- return run_server(options, &master_server_routes, &replica_server_routes);+ return run_server(config, &master_server_routes, &replica_server_routes);}\ No newline at end of filediff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex 7059363a..35bfc888 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -18,8 +18,8 @@ bool directory_exists(const std::string & dir_path) {void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {options.set_program_name("./typesense-server");- options.add<std::string>("data-dir", 'd', "Directory where data will be stored.", true);- options.add<std::string>("api-key", 'a', "API key that allows all operations.", true);+ options.add<std::string>("data-dir", 'd', "Directory where data will be stored.", false);+ options.add<std::string>("api-key", 'a', "API key that allows all operations.", false);options.add<std::string>("search-only-api-key", 's', "API key that allows only searches.", false);options.add<std::string>("listen-address", 'h', "Address to which Typesense server binds.", false, "0.0.0.0");@@ -34,7 +34,7 @@ void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {options.add<std::string>("log-dir", '\0', "Path to the log directory.", false, "");}-int init_logger(cmdline::parser & options, std::unique_ptr<g3::LogWorker> & log_worker) {+int init_logger(Config & config, std::unique_ptr<g3::LogWorker> & log_worker) {// remove SIGTERM since we handle it on our owng3::overrideSetupSignals({{SIGABRT, "SIGABRT"}, {SIGFPE, "SIGFPE"},{SIGILL, "SIGILL"}, {SIGSEGV, "SIGSEGV"},});@@ -42,7 +42,7 @@ int init_logger(cmdline::parser & options, std::unique_ptr<g3::LogWorker> & log_signal(SIGINT, catch_interrupt);signal(SIGTERM, catch_interrupt);- std::string log_dir = options.get<std::string>("log-dir");+ std::string log_dir = config.get_log_dir();if(log_dir.empty()) {// use console logger if log dir is not specified@@ -65,22 +65,22 @@ int init_logger(cmdline::parser & options, std::unique_ptr<g3::LogWorker> & log_return 0;}-int run_server(cmdline::parser & options, void (*master_server_routes)(), void (*replica_server_routes)()) {+int run_server(Config & config, void (*master_server_routes)(), void (*replica_server_routes)()) {LOG(INFO) << "Starting Typesense " << TYPESENSE_VERSION << std::flush;- if(!directory_exists(options.get<std::string>("data-dir"))) {- LOG(ERR) << "Typesense failed to start. " << "Data directory " << options.get<std::string>("data-dir")+ if(!directory_exists(config.get_data_dir())) {+ LOG(ERR) << "Typesense failed to start. " << "Data directory " << config.get_data_dir()<< " does not exist.";return 1;}- Store store(options.get<std::string>("data-dir"));+ Store store(config.get_data_dir());LOG(INFO) << "Loading collections from disk...";CollectionManager & collectionManager = CollectionManager::get_instance();- Option<bool> init_op = collectionManager.init(&store, options.get<std::string>("api-key"),- options.get<std::string>("search-only-api-key"));+ Option<bool> init_op = collectionManager.init(&store, config.get_api_key(),+ config.get_search_only_api_key());if(init_op.ok()) {LOG(INFO) << "Finished loading collections from disk.";@@ -92,11 +92,11 @@ int run_server(cmdline::parser & options, void (*master_server_routes)(), void (curl_global_init(CURL_GLOBAL_SSL);server = new HttpServer(- options.get<std::string>("listen-address"),- options.get<uint32_t>("listen-port"),- options.get<std::string>("ssl-certificate"),- options.get<std::string>("ssl-certificate-key"),- options.exist("enable-cors")+ config.get_listen_address(),+ config.get_listen_port(),+ config.get_ssl_cert(),+ config.get_ssl_cert_key(),+ config.get_enable_cors());server->set_auth_handler(handle_authentication);@@ -104,12 +104,12 @@ int run_server(cmdline::parser & options, void (*master_server_routes)(), void (server->on(SEND_RESPONSE_MSG, on_send_response);server->on(REPLICATION_EVENT_MSG, Replicator::on_replication_event);- if(options.get<std::string>("master").empty()) {+ if(config.get_master().empty()) {master_server_routes();} else {replica_server_routes();- const std::string & master_host_port = options.get<std::string>("master");+ const std::string & master_host_port = config.get_master();std::vector<std::string> parts;StringUtils::split(master_host_port, parts, ":");if(parts.size() != 3) {@@ -118,8 +118,8 @@ int run_server(cmdline::parser & options, void (*master_server_routes)(), void (}LOG(INFO) << "Typesense is starting as a read-only replica... Spawning replication thread...";- std::thread replication_thread([&master_host_port, &store, &options]() {- Replicator::start(::server, master_host_port, options.get<std::string>("api-key"), store);+ std::thread replication_thread([&master_host_port, &store, &config]() {+ Replicator::start(::server, master_host_port, config.get_api_key(), store);});replication_thread.detach();