Skip to content

Commit 6565f37

Browse files
authored
feat: Support setting serverId with SERVERPOD_SERVER_ID env var (serverpod#3238)
Added the ability to set the serverId from environment variables (SERVER_ID) in addition to the command line argument (--server-id). If both options are provided, the command line argument takes precedence. Closes [serverpod#2832](serverpod#2832)
1 parent 2bd68d3 commit 6565f37

File tree

4 files changed

+158
-26
lines changed

4 files changed

+158
-26
lines changed

packages/serverpod/lib/src/server/serverpod.dart

+21-25
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,23 @@ class Serverpod {
335335
stdout.writeln(commandLineArgs.toString());
336336

337337
_runMode = commandLineArgs.runMode;
338-
serverId = commandLineArgs.serverId;
338+
// Load passwords
339+
_passwordManager = PasswordManager(runMode: runMode);
340+
_passwords = _passwordManager.loadPasswords();
341+
342+
// Load config
343+
this.config = config ??
344+
ServerpodConfig.load(
345+
_runMode,
346+
commandLineArgs.serverId,
347+
_passwords,
348+
);
349+
350+
logVerbose(this.config.toString());
351+
serverId = this.config.serverId;
339352

340353
try {
341-
_innerInitializeServerpod(commandLineArgs, config: config);
354+
_innerInitializeServerpod();
342355
} catch (e, stackTrace) {
343356
_reportException(e, stackTrace,
344357
message: 'Error in Serverpod initialization');
@@ -348,34 +361,17 @@ class Serverpod {
348361
stdout.writeln('SERVERPOD initialized, time: ${DateTime.now().toUtc()}');
349362
}
350363

351-
void _innerInitializeServerpod(
352-
CommandLineArgs commandLineArgs, {
353-
ServerpodConfig? config,
354-
}) {
364+
void _innerInitializeServerpod() {
355365
_instance = this;
356366
_internalSerializationManager = internal.Protocol();
357-
358-
// Load passwords
359-
_passwordManager = PasswordManager(runMode: runMode);
360-
_passwords = _passwordManager.loadPasswords();
361-
362-
// Load config
363-
this.config = config ??
364-
ServerpodConfig.load(
365-
_runMode,
366-
serverId,
367-
_passwords,
368-
);
369-
logVerbose(this.config.toString());
370-
371-
Features(this.config);
367+
Features(config);
372368

373369
// Create a temporary log manager with default settings, until we have
374370
// loaded settings from the database.
375371
_updateLogSettings(_defaultRuntimeSettings);
376372

377373
// Setup database
378-
var databaseConfiguration = this.config.database;
374+
var databaseConfiguration = config.database;
379375
if (Features.enableDatabase && databaseConfiguration != null) {
380376
_databasePoolManager = DatabasePoolManager(
381377
serializationManager,
@@ -398,7 +394,7 @@ class Serverpod {
398394
}
399395

400396
// Setup Redis
401-
var redis = this.config.redis;
397+
var redis = config.redis;
402398
if (Features.enableRedis && redis != null) {
403399
redisController = RedisController(
404400
host: redis.host,
@@ -410,7 +406,7 @@ class Serverpod {
410406

411407
_caches = Caches(
412408
serializationManager,
413-
this.config,
409+
config,
414410
serverId,
415411
redisController,
416412
);
@@ -420,7 +416,7 @@ class Serverpod {
420416
server = Server(
421417
serverpod: this,
422418
serverId: serverId,
423-
port: this.config.apiServer.port,
419+
port: config.apiServer.port,
424420
serializationManager: serializationManager,
425421
databasePoolManager: _databasePoolManager,
426422
passwords: _passwords,

packages/serverpod_shared/lib/src/config.dart

+16
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class ServerpodConfig {
9494
Map configMap, {
9595
Map<String, String> environment = const {},
9696
}) {
97+
serverId = _readServerId(configMap, environment, serverId);
98+
9799
var apiConfig = _apiConfigMap(configMap, environment);
98100
if (apiConfig == null) {
99101
throw _ServerpodApiServerConfigMissing();
@@ -594,6 +596,20 @@ int _readMaxRequestSize(
594596
return maxRequestSize;
595597
}
596598

599+
String _readServerId(
600+
Map<dynamic, dynamic> configMap,
601+
Map<String, String> environment,
602+
String serverIdFromCommandLineArg,
603+
) {
604+
if (serverIdFromCommandLineArg != 'default') {
605+
return serverIdFromCommandLineArg;
606+
}
607+
var serverId = environment[ServerpodEnv.serverId.envVariable] ??
608+
configMap[ServerpodEnv.serverId.configKey] ??
609+
'default';
610+
return serverId;
611+
}
612+
597613
/// Validates that a JSON configuration contains all required keys, and that
598614
/// the values have the correct types.
599615
///

packages/serverpod_shared/lib/src/environment_variables.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ enum ServerpodEnv {
109109
sessionPersistentLogEnabled,
110110

111111
/// True if session console logging is enabled.
112-
sessionConsoleLogEnabled;
112+
sessionConsoleLogEnabled,
113+
114+
/// The id of the server.
115+
serverId;
113116

114117
/// The key used in the environment configuration file.
115118
String get configKey {
@@ -140,6 +143,7 @@ enum ServerpodEnv {
140143
(ServerpodEnv.maxRequestSize) => 'maxRequestSize',
141144
(ServerpodEnv.sessionPersistentLogEnabled) => 'persistentEnabled',
142145
(ServerpodEnv.sessionConsoleLogEnabled) => 'consoleEnabled',
146+
(ServerpodEnv.serverId) => 'serverId',
143147
};
144148
}
145149

@@ -177,6 +181,7 @@ enum ServerpodEnv {
177181
'SERVERPOD_SESSION_PERSISTENT_LOG_ENABLED',
178182
(ServerpodEnv.sessionConsoleLogEnabled) =>
179183
'SERVERPOD_SESSION_CONSOLE_LOG_ENABLED',
184+
(ServerpodEnv.serverId) => 'SERVERPOD_SERVER_ID',
180185
};
181186
}
182187
}

packages/serverpod_shared/test/config_test.dart

+115
Original file line numberDiff line numberDiff line change
@@ -978,4 +978,119 @@ redis:
978978

979979
expect(config.redis?.enabled, isTrue);
980980
});
981+
982+
test(
983+
'Given a Serverpod config with server id when loading from Map then serverId configuration matches supplied value.',
984+
() {
985+
var serverpodConfig = '''
986+
apiServer:
987+
port: 8080
988+
publicHost: localhost
989+
publicPort: 8080
990+
publicScheme: http
991+
serverId: testServer1
992+
''';
993+
994+
var config = ServerpodConfig.loadFromMap(
995+
runMode,
996+
serverId,
997+
passwords,
998+
loadYaml(serverpodConfig),
999+
);
1000+
1001+
expect(config.serverId, 'testServer1');
1002+
});
1003+
1004+
test(
1005+
'Given a Serverpod config with only the api server configuration but the environment variables containing the server id when loading from Map then the server id matches supplied value.',
1006+
() {
1007+
var config = ServerpodConfig.loadFromMap(
1008+
runMode,
1009+
serverId,
1010+
passwords,
1011+
{
1012+
'apiServer': {
1013+
'port': 8080,
1014+
'publicHost': 'localhost',
1015+
'publicPort': 8080,
1016+
'publicScheme': 'http',
1017+
},
1018+
},
1019+
environment: {
1020+
'SERVERPOD_SERVER_ID': 'testServer1',
1021+
},
1022+
);
1023+
1024+
expect(config.serverId, 'testServer1');
1025+
});
1026+
1027+
test(
1028+
'Given a Serverpod config with only the api server configuration but the server id given as an argument then the server id matches supplied value.',
1029+
() {
1030+
var config = ServerpodConfig.loadFromMap(
1031+
runMode,
1032+
'testServer1',
1033+
passwords,
1034+
{
1035+
'apiServer': {
1036+
'port': 8080,
1037+
'publicHost': 'localhost',
1038+
'publicPort': 8080,
1039+
'publicScheme': 'http',
1040+
},
1041+
},
1042+
);
1043+
1044+
expect(config.serverId, 'testServer1');
1045+
});
1046+
1047+
test(
1048+
'Given a Serverpod config with server id when loading from Map and the environment variables containing the server id but the server id given as an argument is the default value then the server id from environment takes the precedence.',
1049+
() {
1050+
var serverpodConfig = '''
1051+
apiServer:
1052+
port: 8080
1053+
publicHost: localhost
1054+
publicPort: 8080
1055+
publicScheme: http
1056+
serverId: testServerIdFromConfig
1057+
''';
1058+
1059+
var config = ServerpodConfig.loadFromMap(
1060+
runMode,
1061+
serverId,
1062+
passwords,
1063+
loadYaml(serverpodConfig),
1064+
environment: {
1065+
'SERVERPOD_SERVER_ID': 'testServerIdFromEnv',
1066+
},
1067+
);
1068+
1069+
expect(config.serverId, 'testServerIdFromEnv');
1070+
});
1071+
1072+
test(
1073+
'Given a Serverpod config with server id when loading from Map and the environment variables containing the server id and the server id given as an argument is a custom defined value then the server id from the argument takes the precedence.',
1074+
() {
1075+
var serverpodConfig = '''
1076+
apiServer:
1077+
port: 8080
1078+
publicHost: localhost
1079+
publicPort: 8080
1080+
publicScheme: http
1081+
serverId: testServerIdFromConfig
1082+
''';
1083+
1084+
var config = ServerpodConfig.loadFromMap(
1085+
runMode,
1086+
'testServerIdFromArg',
1087+
passwords,
1088+
loadYaml(serverpodConfig),
1089+
environment: {
1090+
'SERVERPOD_SERVER_ID': 'testServerIdFromEnv',
1091+
},
1092+
);
1093+
1094+
expect(config.serverId, 'testServerIdFromArg');
1095+
});
9811096
}

0 commit comments

Comments
 (0)