Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tools][server] Environment variables for sc-server host and port #453

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
- "BINARY_PATH=/sc-machine/build/Release/bin"
- "EXTENSIONS_PATH=/sc-machine/build/Release/lib/extensions"
- "CONFIG_PATH=/sc-machine/sc-machine.ini"
- "SC_SERVER_HOST=0.0.0.0"
command:
- "run"

Expand Down
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Using sc-machine as a library is much more optimized: adding it to the CMake tree is no longer necessary. You can install sc-machine packages and import sc-machine targets into your cmake using `find_package(sc-machine REQUIRED)`. See how to do it -- [Build System](build/build_system.md)
- Each release sc-machine binaries are being compiled for supported OS and formed as an archives on Github. Minimum required version of macOS is macOS-14 (arm), of ubuntu is ubuntu-22.04. The sc-machine doesn't support ubuntu-20.04 anymore. You can use sc-machine binaries to work with sc-memory or you can use `RunMachine` method from `sc-machine-runner.so` to create your own entry point to initialize sc-memory instead of using compiled `sc-machine` binary.
- Script for the project build (`build_sc_machine.sh`), scripts for running binaries (`run_sc_server.sh`, `build_kb.sh`) were removed from the sc-machine repository scripts. You should use sc-machine binaries directly.
- sc-server is no longer entry point of the sc-machine, it is an extension (`sc-server-lib.so`), that is loaded dynamically when the machine is started. So, `sc-server` binary was removed, `sc-machine` binary was added instead.
- sc-server is no longer entry point of the sc-machine, it is an extension (`sc-server-lib.so`), that is loaded dynamically when the machine is started. So, `sc-server` binary was removed, `sc-machine` binary was added instead. To specify host and port for sc-server without using config, use environment variables `SC_SERVER_HOST` and `SC_SERVER_PORT` accordingly.
- Config was changed:
- `repo_path` option in `[sc-memory]` group was deprecated, `storage` option was added instead;
- `extensions_path` option in `[sc-memory]` group was deprecated, `extensions` option was added instead.
Expand Down Expand Up @@ -163,6 +163,7 @@ See documentation, to learn more about using new API.

### Added

- Environment variables `SC_SERVER_HOST` and `SC_SERVER_PORT` for sc-server
- `SearchLinksByContentSubstring` method with `ScLinkFilter` parameter
- `ScLinkFilter` class to specify search criteria for sc-links
- Intro for documentation
Expand Down
6 changes: 6 additions & 0 deletions docs/sc-tools/sc_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Options:
--test|-t Test sc-memory state. If this flag is specified, sc-memory will be initialized and shutdown immediately.
--version Display version of ./build/<Release|Debug>/bin/sc-machine.
--help Display this help message.

Environment variables:
SC_SERVER_HOST Specifies a host for sc-server extension. If not set, the value defaults to the `host` option in the `[sc-server]` group of the configuration file.
If neither is specified, the default host is 127.0.0.1.
SC_SERVER_PORT Specifies a port for sc-server extension. If not set, the value defaults to the `port` option in the `[sc-server]` group of the configuration file.
If neither is specified, the default port is 8090.
```

Example of usage:
Expand Down
9 changes: 8 additions & 1 deletion sc-tools/sc-machine-runner/src/sc_machine_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ void PrintHelpMessage(std::string const & binaryName)
<< " --test|-t Test sc-memory state. "
<< "If this flag is specified, sc-memory will be initialized and shutdown immediately.\n"
<< " --version Display version of " << binaryName << ".\n"
<< " --help Display this help message.\n";
<< " --help Display this help message.\n\n"
<< "Environment variables:\n"
<< " SC_SERVER_HOST Specifies a host for sc-server extension. If not set, the value "
"defaults to the `host` option in the `[sc-server]` group of the configuration file.\n"
" If neither is specified, the default host is 127.0.0.1.\n"
<< " SC_SERVER_PORT Specifies a port for sc-server extension. If not set, the value "
"defaults to the `port` option in the `[sc-server]` group of the configuration file.\n"
" If neither is specified, the default port is 8090.\n";
}

sc_int RunMachine(sc_int argc, sc_char * argv[])
Expand Down
8 changes: 4 additions & 4 deletions sc-tools/sc-server/src/sc-server-impl/sc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <sc-memory/sc_keynodes.hpp>

ScServer::ScServer(std::string hostName, size_t port)
: m_hostName(std::move(hostName))
: m_host(std::move(hostName))
, m_port(port)
, m_logger(nullptr)
{
Expand All @@ -21,7 +21,7 @@ ScServer::ScServer(std::string hostName, size_t port)

{
LogMessage(ScServerErrorLevel::info, "Socket data:");
LogMessage(ScServerErrorLevel::info, "\tHost name: " + m_hostName);
LogMessage(ScServerErrorLevel::info, "\tHost: " + m_host);
LogMessage(ScServerErrorLevel::info, "\tPort: " + std::to_string(m_port));
}

Expand All @@ -38,7 +38,7 @@ void ScServer::Run()

Initialize();

m_instance->listen({asio::ip::address::from_string(m_hostName), sc_uint16(m_port)});
m_instance->listen({asio::ip::address::from_string(m_host), sc_uint16(m_port)});
m_instance->start_accept();

LogMessage(ScServerErrorLevel::info, "Start actions processing");
Expand Down Expand Up @@ -108,7 +108,7 @@ void ScServer::Shutdown()

std::string ScServer::GetUri()
{
return "ws://" + m_hostName + ":" + std::to_string(m_port);
return "ws://" + m_host + ":" + std::to_string(m_port);
}

bool ScServer::IsSessionValid(ScServerSessionId const & sessionId)
Expand Down
2 changes: 1 addition & 1 deletion sc-tools/sc-server/src/sc-server-impl/sc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ScServer

protected:
std::atomic<sc_bool> m_isServerRun = SC_FALSE;
std::string m_hostName;
std::string m_host;
ScServerPort m_port;

ScServerLogger * m_logger;
Expand Down
14 changes: 12 additions & 2 deletions sc-tools/sc-server/src/sc_server_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "sc_server_module.hpp"

#include <cstdlib>

#include "sc_server_setup.hpp"

SC_MODULE_REGISTER(ScServerModule);
Expand All @@ -15,8 +17,16 @@

void ScServerModule::Initialize(ScMemoryContext *)
{
// It is backward compatible logic. When all platform-dependent components will be configured from kb it will be
// removed.
// TODO(NikitaZotov): Configure all platform-dependent components from kb.

sc_char const * host = std::getenv("SC_SERVER_HOST");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be tested in some way?

if (host != nullptr)
ms_serverParams.Insert({"host", host});

Check warning on line 24 in sc-tools/sc-server/src/sc_server_module.cpp

View check run for this annotation

Codecov / codecov/patch

sc-tools/sc-server/src/sc_server_module.cpp#L24

Added line #L24 was not covered by tests

sc_char const * port = std::getenv("SC_SERVER_PORT");
if (port != nullptr)
ms_serverParams.Insert({"port", port});

Check warning on line 28 in sc-tools/sc-server/src/sc_server_module.cpp

View check run for this annotation

Codecov / codecov/patch

sc-tools/sc-server/src/sc_server_module.cpp#L28

Added line #L28 was not covered by tests

ScConfig config{ScMemory::ms_configPath, {{"log_file"}}};
ScConfigGroup serverConfig = config["sc-server"];
for (auto const & key : *serverConfig)
Expand Down
Loading