Skip to content

Commit

Permalink
chore_: rename StatusBackendClient.enabled to serverEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Nov 6, 2024
1 parent da9bec7 commit 4caa788
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 43 deletions.
6 changes: 3 additions & 3 deletions doc/use-status-backend-server.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## Solution to use Status Backend Server
`StatusBackendClient` is the entry point to use Status Backend Server. Whether it's enabled or not is controlled by `STATUS_BACKEND_ENABLED` environment variable. We need always to call `status-im.setup.status-backend-client/init` whether `STATUS_BACKEND_ENABLED` is `1` or not. If it's not enabled, the invocation to functions in `native-module.core` will be delegated to built-in status-go library, otherwise it will be delegated to status-go running in status-backend server. Currently, all functions has usages in `native-module.core` should be supported delegated to.
`StatusBackendClient` is the entry point to use Status Backend Server. We need always to call `status-im.setup.status-backend-client/init` whether `STATUS_BACKEND_SERVER_ENABLED` is `1` or not. If it's not enabled, the invocation to functions in `native-module.core` will be delegated to built-in status-go library, otherwise it will be delegated to status-go running in status-backend server. Currently, all functions has usages in `native-module.core` should be supported delegated to.

related [PR](https://github.com/status-im/status-mobile/pull/21550)

## Usage
### Add environment variables to your local machine:
```shell
# enable using status backend server or not
export STATUS_BACKEND_ENABLED=1
# enable using status backend server or not, otherwise it will use built-in status-go library
export STATUS_BACKEND_SERVER_ENABLED=1

#The host should contain an IP address and a port separated by a colon.
#The port comes from your running status backend server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DatabaseManager(private val reactContext: ReactApplicationContext) : React

private fun getExportDBFile(): File {
StatusBackendClient.getInstance()?.let {
if (it.enabled) {
if (it.serverEnabled) {
return File(it.rootDataDir, exportDBFileName)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R
statusgoFunction: () -> String
) {
val statusBackendClient = getInstance()
if (statusBackendClient?.enabled == true) {
if (statusBackendClient?.serverEnabled == true) {
val result = statusBackendClient.request(endpoint, requestBody)
result.onSuccess { response ->
utils.handleStatusGoResponse(response, endpoint)
Expand All @@ -54,7 +54,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R
callback: Callback?
) {
val statusBackendClient = getInstance()
if (statusBackendClient?.enabled == true) {
if (statusBackendClient?.serverEnabled == true) {
val runnable = Runnable {
val result = statusBackendClient.request(endpoint, requestBody)
result.onSuccess { response ->
Expand All @@ -77,7 +77,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R
statusgoFunction: () -> String
): String {
val statusBackendClient = getInstance()
return if (statusBackendClient?.enabled == true) {
return if (statusBackendClient?.serverEnabled == true) {
val result = statusBackendClient.request(endpoint, requestBody)
result.getOrElse { error ->
Log.e(TAG, "request to $endpoint failed", error)
Expand Down Expand Up @@ -109,32 +109,32 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R

private var webSocket: WebSocket? = null

@Volatile var enabled = false
@Volatile var serverEnabled = false
@Volatile private var statusGoEndpoint: String? = null
@Volatile private var signalEndpoint: String? = null
@Volatile var rootDataDir: String? = null

@ReactMethod
fun configStatusBackendServer(
enabled: Boolean,
serverEnabled: Boolean,
statusGoEndpoint: String,
signalEndpoint: String,
rootDataDir: String
) {
configure(enabled, statusGoEndpoint, signalEndpoint, rootDataDir)
configure(serverEnabled, statusGoEndpoint, signalEndpoint, rootDataDir)
}

private fun configure(
enabled: Boolean,
serverEnabled: Boolean,
statusGoEndpoint: String,
signalEndpoint: String,
rootDataDir: String
) {
Log.d(TAG, "configure: enabled=$enabled, statusGoEndpoint=$statusGoEndpoint, " +
Log.d(TAG, "configure: serverEnabled=$serverEnabled, statusGoEndpoint=$statusGoEndpoint, " +
"signalEndpoint=$signalEndpoint, rootDataDir=$rootDataDir")

this.enabled = enabled
if (enabled) {
this.serverEnabled = serverEnabled
if (serverEnabled) {
this.statusGoEndpoint = statusGoEndpoint
this.signalEndpoint = signalEndpoint
this.rootDataDir = rootDataDir
Expand All @@ -148,7 +148,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R
}

private fun connectWebSocket() {
if (!enabled || signalEndpoint == null) {
if (!serverEnabled || signalEndpoint == null) {
return
}

Expand All @@ -173,8 +173,8 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R
}

fun request(endpoint: String, body: String): Result<String> {
if (!enabled || statusGoEndpoint == null) {
return Result.failure(IllegalStateException("Status backend is not enabled"))
if (!serverEnabled || statusGoEndpoint == null) {
return Result.failure(IllegalStateException("Status backend server is not enabled"))
}

val fullUrl = "$statusGoEndpoint$endpoint"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Utils(private val reactContext: ReactApplicationContext) : ReactContextBas

fun getNoBackupDirectory(): String {
StatusBackendClient.getInstance()?.let { client ->
if (client.enabled && client.rootDataDir != null) {
if (client.serverEnabled && client.rootDataDir != null) {
return client.rootDataDir!!
}
}
Expand All @@ -40,7 +40,7 @@ class Utils(private val reactContext: ReactApplicationContext) : ReactContextBas

fun getPublicStorageDirectory(): File? {
StatusBackendClient.getInstance()?.let { client ->
if (client.enabled && client.rootDataDir != null) {
if (client.serverEnabled && client.rootDataDir != null) {
return File(client.rootDataDir!!)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@interface StatusBackendClient : NSObject <RCTBridgeModule>

@property (nonatomic) BOOL enabled;
@property (nonatomic) BOOL serverEnabled;
@property (nonatomic, strong) NSString *statusGoEndpoint;
@property (nonatomic, strong) NSString *signalEndpoint;
@property (nonatomic, strong) NSString *rootDataDir;
Expand Down
26 changes: 13 additions & 13 deletions modules/react-native-status/ios/RCTStatus/StatusBackendClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@implementation StatusBackendClient {
NSURLSessionWebSocketTask *_webSocket;
BOOL _enabled;
BOOL _serverEnabled;
NSString *_statusGoEndpoint;
NSString *_signalEndpoint;
NSString *_rootDataDir;
Expand Down Expand Up @@ -35,7 +35,7 @@ - (instancetype)init {
dispatch_once(&onceToken, ^{
sharedInstance = [super init];
if (sharedInstance) {
sharedInstance->_enabled = NO;
sharedInstance->_serverEnabled = NO;
sharedInstance->_statusGoEndpoint = nil;
sharedInstance->_signalEndpoint = nil;
sharedInstance->_rootDataDir = nil;
Expand All @@ -49,8 +49,8 @@ - (id)copyWithZone:(NSZone *)zone {
return self;
}

- (BOOL)enabled {
return _enabled;
- (BOOL)serverEnabled {
return _serverEnabled;
}

- (NSString *)statusGoEndpoint {
Expand All @@ -66,7 +66,7 @@ - (NSString *)rootDataDir {
}

- (void)connectWebSocket {
if (!self.enabled || !self.signalEndpoint) {
if (!self.serverEnabled || !self.signalEndpoint) {
return;
}

Expand Down Expand Up @@ -145,23 +145,23 @@ - (void)request:(NSString *)endpoint
[task resume];
}

RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)enabled
RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)serverEnabled
statusGoEndpoint:(NSString *)statusGoEndpoint
signalEndpoint:(NSString *)signalEndpoint
rootDataDir:(NSString *)rootDataDir) {
[self configureWithEnabled:enabled
[self configureWithEnabled:serverEnabled
statusGoEndpoint:statusGoEndpoint
signalEndpoint:signalEndpoint
rootDataDir:rootDataDir];
}

- (void)configureWithEnabled:(BOOL)enabled
- (void)configureWithEnabled:(BOOL)serverEnabled
statusGoEndpoint:(NSString *)statusGoEndpoint
signalEndpoint:(NSString *)signalEndpoint
rootDataDir:(NSString *)rootDataDir {
_enabled = enabled;
_serverEnabled = serverEnabled;

if (enabled) {
if (serverEnabled) {
_statusGoEndpoint = statusGoEndpoint;
_signalEndpoint = signalEndpoint;
_rootDataDir = rootDataDir;
Expand All @@ -178,7 +178,7 @@ + (void)executeStatusGoRequest:(NSString *)endpoint
body:(NSString *)body
statusgoFunction:(NSString * (^)(void))statusgoFunction {
StatusBackendClient *client = [StatusBackendClient sharedInstance];
if (client.enabled) {
if (client.serverEnabled) {
[client request:endpoint body:body callback:^(NSString *response, NSError *error) {
[Utils handleStatusGoResponse:response source:endpoint error:error];
}];
Expand All @@ -193,7 +193,7 @@ + (void)executeStatusGoRequestWithCallback:(NSString *)endpoint
statusgoFunction:(NSString * (^)(void))statusgoFunction
callback:(RCTResponseSenderBlock)callback {
StatusBackendClient *client = [StatusBackendClient sharedInstance];
if (client.enabled) {
if (client.serverEnabled) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[client request:endpoint body:body callback:^(NSString *response, NSError *error) {
if (error) {
Expand Down Expand Up @@ -222,7 +222,7 @@ + (NSString *)executeStatusGoRequestWithResult:(NSString *)endpoint
body:(NSString *)body
statusgoFunction:(NSString * (^)(void))statusgoFunction {
StatusBackendClient *client = [StatusBackendClient sharedInstance];
if (client.enabled) {
if (client.serverEnabled) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSString *resultString = @"";

Expand Down
6 changes: 3 additions & 3 deletions modules/react-native-status/ios/RCTStatus/Utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ + (NSURL *)getRootUrl {
NSURL *rootUrl;

StatusBackendClient *client = [StatusBackendClient sharedInstance];
if (client.enabled && client.rootDataDir) {
if (client.serverEnabled && client.rootDataDir) {
rootUrl = [NSURL fileURLWithPath:client.rootDataDir];
} else {
rootUrl = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
Expand Down Expand Up @@ -75,7 +75,7 @@ + (NSString *) getKeyUID:(NSString *)jsonString {

+ (NSString *) getExportDbFilePath {
StatusBackendClient *client = [StatusBackendClient sharedInstance];
if (client.enabled && client.rootDataDir) {
if (client.serverEnabled && client.rootDataDir) {
return [client.rootDataDir stringByAppendingPathComponent:@"export.db"];
}

Expand Down Expand Up @@ -131,7 +131,7 @@ + (void) migrateKeystore:(NSString *)accountData

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(backupDisabledDataDir) {
StatusBackendClient *client = [StatusBackendClient sharedInstance];
if (client.enabled && client.rootDataDir) {
if (client.serverEnabled && client.rootDataDir) {
return client.rootDataDir;
}

Expand Down
2 changes: 1 addition & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
status-im.config/INFURA_TOKEN #shadow/env "INFURA_TOKEN"
status-im.config/STATUS_BUILD_PROXY_USER #shadow/env "STATUS_BUILD_PROXY_USER"
status-im.config/STATUS_BUILD_PROXY_PASSWORD #shadow/env "STATUS_BUILD_PROXY_PASSWORD"
status-im.config/STATUS_BACKEND_ENABLED #shadow/env "STATUS_BACKEND_ENABLED"
status-im.config/STATUS_BACKEND_SERVER_ENABLED #shadow/env "STATUS_BACKEND_SERVER_ENABLED"
status-im.config/STATUS_BACKEND_SERVER_HOST #shadow/env "STATUS_BACKEND_SERVER_HOST"
status-im.config/STATUS_BACKEND_SERVER_ROOT_DATA_DIR #shadow/env
"STATUS_BACKEND_SERVER_ROOT_DATA_DIR"
Expand Down
10 changes: 7 additions & 3 deletions src/status_im/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@
;; interfere with react-native debug tools, such as inspector and Perf monitor
(def enable-alert-banner? (enabled? (get-config :ENABLE_ALERT_BANNER "0")))

(goog-define STATUS_BACKEND_ENABLED "0")
;; e.g. 127.0.0.1:58935
;; enable using status backend server or not, otherwise it will use built-in status-go library
;; see doc/use-status-backend-server.md for more details
(goog-define STATUS_BACKEND_SERVER_ENABLED "0")
;; The host should contain an IP address and a port separated by a colon.
;; The port comes from your running status backend server.
;; If you run it by PORT=60000 make run-status-backend , then host will likely be 127.0.0.1:60000
(goog-define STATUS_BACKEND_SERVER_HOST "")
;; /path/to/root/data/dir
;; make sure it exists
;; make sure it exists, it should be in absolute path
(goog-define STATUS_BACKEND_SERVER_ROOT_DATA_DIR "")
6 changes: 3 additions & 3 deletions src/status_im/setup/status_backend_client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
[status-im.config :as config]))

(def default-config
{:enabled? (config/enabled? config/STATUS_BACKEND_ENABLED)
{:server-enabled? (config/enabled? config/STATUS_BACKEND_SERVER_ENABLED)
:status-go-endpoint (str "http://" config/STATUS_BACKEND_SERVER_HOST "/statusgo/")
:signal-endpoint (str "ws://" config/STATUS_BACKEND_SERVER_HOST "/signals")
:root-data-dir config/STATUS_BACKEND_SERVER_ROOT_DATA_DIR})

(defn set-config!
[{:keys [enabled? status-go-endpoint signal-endpoint root-data-dir]}]
[{:keys [server-enabled? status-go-endpoint signal-endpoint root-data-dir]}]
(when-let [client (.-StatusBackendClient (.-NativeModules react-native))]
(.configStatusBackendServer client
enabled?
server-enabled?
status-go-endpoint
signal-endpoint
root-data-dir)))
Expand Down

0 comments on commit 4caa788

Please sign in to comment.