mirror of
https://github.com/haiwen/seafile-server.git
synced 2025-08-31 15:11:08 +00:00
Load config from file and env (#710)
Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
@@ -1196,7 +1196,7 @@ test_seafile_config(const char *central_config_dir, const char *config_dir, cons
|
|||||||
|
|
||||||
event_init ();
|
event_init ();
|
||||||
|
|
||||||
seaf = seafile_session_new (central_config_dir, seafile_dir, config_dir, NULL, NULL);
|
seaf = seafile_session_new (central_config_dir, seafile_dir, config_dir);
|
||||||
if (!seaf) {
|
if (!seaf) {
|
||||||
seaf_error ("Error: failed to create ccnet session\n");
|
seaf_error ("Error: failed to create ccnet session\n");
|
||||||
return -1;
|
return -1;
|
||||||
@@ -1221,8 +1221,6 @@ main (int argc, char **argv)
|
|||||||
int daemon_mode = 1;
|
int daemon_mode = 1;
|
||||||
gboolean test_config = FALSE;
|
gboolean test_config = FALSE;
|
||||||
char *repo_id = NULL;
|
char *repo_id = NULL;
|
||||||
const char *private_key = NULL;
|
|
||||||
const char *site_root = NULL;
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
argv = get_argv_utf8 (&argc);
|
argv = get_argv_utf8 (&argc);
|
||||||
@@ -1323,9 +1321,6 @@ main (int argc, char **argv)
|
|||||||
debug_str = g_getenv("SEAFILE_DEBUG");
|
debug_str = g_getenv("SEAFILE_DEBUG");
|
||||||
seafile_debug_set_flags_string (debug_str);
|
seafile_debug_set_flags_string (debug_str);
|
||||||
|
|
||||||
private_key = g_getenv("JWT_PRIVATE_KEY");
|
|
||||||
site_root = g_getenv("SITE_ROOT");
|
|
||||||
|
|
||||||
if (seafile_dir == NULL)
|
if (seafile_dir == NULL)
|
||||||
seafile_dir = g_build_filename (ccnet_dir, "seafile", NULL);
|
seafile_dir = g_build_filename (ccnet_dir, "seafile", NULL);
|
||||||
if (logfile == NULL)
|
if (logfile == NULL)
|
||||||
@@ -1348,12 +1343,7 @@ main (int argc, char **argv)
|
|||||||
exit (0);
|
exit (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!private_key) {
|
seaf = seafile_session_new (central_config_dir, seafile_dir, ccnet_dir);
|
||||||
seaf_warning ("Failed to read JWT_PRIVATE_KEY.\n");
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
seaf = seafile_session_new (central_config_dir, seafile_dir, ccnet_dir, private_key, site_root);
|
|
||||||
if (!seaf) {
|
if (!seaf) {
|
||||||
seaf_warning ("Failed to create seafile session.\n");
|
seaf_warning ("Failed to create seafile session.\n");
|
||||||
exit (1);
|
exit (1);
|
||||||
|
@@ -104,26 +104,109 @@ load_fileserver_config (SeafileSession *session)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
load_config (SeafileSession *session, const char *config_file_path, const char *config_file_ccnet)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
GError *error = NULL;
|
||||||
|
GKeyFile *config = NULL;
|
||||||
|
GKeyFile *ccnet_config = NULL;
|
||||||
|
gboolean notif_enabled = FALSE;
|
||||||
|
char *notif_server = NULL;
|
||||||
|
int notif_port = 8083;
|
||||||
|
const char *private_key = NULL;
|
||||||
|
const char *site_root = NULL;
|
||||||
|
const char *log_to_stdout = NULL;
|
||||||
|
|
||||||
|
config = g_key_file_new ();
|
||||||
|
if (!g_key_file_load_from_file (config, config_file_path,
|
||||||
|
G_KEY_FILE_NONE, &error)) {
|
||||||
|
seaf_warning ("Failed to load config file.\n");
|
||||||
|
ret = -1;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
ccnet_config = g_key_file_new ();
|
||||||
|
g_key_file_set_list_separator (ccnet_config, ',');
|
||||||
|
if (!g_key_file_load_from_file (ccnet_config, config_file_ccnet,
|
||||||
|
G_KEY_FILE_KEEP_COMMENTS, NULL))
|
||||||
|
{
|
||||||
|
seaf_warning ("Can't load ccnet config file %s.\n", config_file_ccnet);
|
||||||
|
ret = -1;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
session->config = config;
|
||||||
|
session->ccnet_config = ccnet_config;
|
||||||
|
|
||||||
|
session->cloud_mode = g_key_file_get_boolean (config,
|
||||||
|
"general", "cloud_mode",
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
|
||||||
|
notif_enabled = g_key_file_get_boolean (config,
|
||||||
|
"notification", "enabled",
|
||||||
|
NULL);
|
||||||
|
if (notif_enabled) {
|
||||||
|
notif_server = g_key_file_get_string (config,
|
||||||
|
"notification", "host",
|
||||||
|
NULL);
|
||||||
|
notif_port = g_key_file_get_integer (config,
|
||||||
|
"notification", "port",
|
||||||
|
NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read config from env
|
||||||
|
private_key = g_getenv("JWT_PRIVATE_KEY");
|
||||||
|
site_root = g_getenv("SITE_ROOT");
|
||||||
|
log_to_stdout = g_getenv("SEAFILE_LOG_TO_STDOUT");
|
||||||
|
|
||||||
|
if (!private_key) {
|
||||||
|
seaf_warning ("Failed to read JWT_PRIVATE_KEY.\n");
|
||||||
|
ret = -1;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if (notif_enabled && notif_server != NULL) {
|
||||||
|
session->notif_server_private_key = g_strdup (private_key);
|
||||||
|
char notif_url[128];
|
||||||
|
g_sprintf (notif_url, "%s:%d", notif_server, notif_port);
|
||||||
|
session->notif_url = g_strdup (notif_url);
|
||||||
|
}
|
||||||
|
session->seahub_pk = g_strdup (private_key);
|
||||||
|
if (site_root) {
|
||||||
|
session->seahub_url = g_strdup_printf("http://127.0.0.1:8000%sapi/v2.1/internal", site_root);
|
||||||
|
} else {
|
||||||
|
session->seahub_url = g_strdup("http://127.0.0.1:8000/api/v2.1/internal");
|
||||||
|
}
|
||||||
|
session->seahub_conn_pool = connection_pool_new ();
|
||||||
|
|
||||||
|
if (g_strcmp0 (log_to_stdout, "true") == 0) {
|
||||||
|
session->log_to_stdout = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
|
g_free (notif_server);
|
||||||
|
if (ret < 0) {
|
||||||
|
if (config)
|
||||||
|
g_key_file_free (config);
|
||||||
|
if (ccnet_config)
|
||||||
|
g_key_file_free (ccnet_config);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
SeafileSession *
|
SeafileSession *
|
||||||
seafile_session_new(const char *central_config_dir,
|
seafile_session_new(const char *central_config_dir,
|
||||||
const char *seafile_dir,
|
const char *seafile_dir,
|
||||||
const char *ccnet_dir,
|
const char *ccnet_dir)
|
||||||
const char *private_key,
|
|
||||||
const char *site_root)
|
|
||||||
{
|
{
|
||||||
char *abs_central_config_dir = NULL;
|
char *abs_central_config_dir = NULL;
|
||||||
char *abs_seafile_dir;
|
char *abs_seafile_dir;
|
||||||
char *abs_ccnet_dir = NULL;
|
char *abs_ccnet_dir = NULL;
|
||||||
char *tmp_file_dir;
|
char *tmp_file_dir;
|
||||||
char *config_file_path;
|
char *config_file_path = NULL;
|
||||||
char *config_file_ccnet;
|
char *config_file_ccnet = NULL;
|
||||||
GKeyFile *config;
|
|
||||||
GKeyFile *ccnet_config;
|
|
||||||
SeafileSession *session = NULL;
|
SeafileSession *session = NULL;
|
||||||
gboolean notif_enabled = FALSE;
|
|
||||||
char *notif_server = NULL;
|
|
||||||
int notif_port = 8083;
|
|
||||||
char *notif_server_private_key = NULL;
|
|
||||||
|
|
||||||
abs_ccnet_dir = ccnet_expand_path (ccnet_dir);
|
abs_ccnet_dir = ccnet_expand_path (ccnet_dir);
|
||||||
abs_seafile_dir = ccnet_expand_path (seafile_dir);
|
abs_seafile_dir = ccnet_expand_path (seafile_dir);
|
||||||
@@ -158,55 +241,17 @@ seafile_session_new(const char *central_config_dir,
|
|||||||
abs_central_config_dir ? abs_central_config_dir : abs_ccnet_dir,
|
abs_central_config_dir ? abs_central_config_dir : abs_ccnet_dir,
|
||||||
"ccnet.conf", NULL);
|
"ccnet.conf", NULL);
|
||||||
|
|
||||||
GError *error = NULL;
|
|
||||||
config = g_key_file_new ();
|
|
||||||
if (!g_key_file_load_from_file (config, config_file_path,
|
|
||||||
G_KEY_FILE_NONE, &error)) {
|
|
||||||
seaf_warning ("Failed to load config file.\n");
|
|
||||||
g_key_file_free (config);
|
|
||||||
g_free (config_file_path);
|
|
||||||
goto onerror;
|
|
||||||
}
|
|
||||||
ccnet_config = g_key_file_new ();
|
|
||||||
g_key_file_set_list_separator (ccnet_config, ',');
|
|
||||||
if (!g_key_file_load_from_file (ccnet_config, config_file_ccnet,
|
|
||||||
G_KEY_FILE_KEEP_COMMENTS, NULL))
|
|
||||||
{
|
|
||||||
seaf_warning ("Can't load ccnet config file %s.\n", config_file_ccnet);
|
|
||||||
g_key_file_free (ccnet_config);
|
|
||||||
g_free (config_file_ccnet);
|
|
||||||
goto onerror;
|
|
||||||
}
|
|
||||||
g_free (config_file_path);
|
|
||||||
g_free (config_file_ccnet);
|
|
||||||
|
|
||||||
session = g_new0(SeafileSession, 1);
|
session = g_new0(SeafileSession, 1);
|
||||||
session->seaf_dir = abs_seafile_dir;
|
session->seaf_dir = abs_seafile_dir;
|
||||||
session->ccnet_dir = abs_ccnet_dir;
|
session->ccnet_dir = abs_ccnet_dir;
|
||||||
session->tmp_file_dir = tmp_file_dir;
|
session->tmp_file_dir = tmp_file_dir;
|
||||||
session->config = config;
|
|
||||||
session->ccnet_config = ccnet_config;
|
|
||||||
|
|
||||||
session->cloud_mode = g_key_file_get_boolean (config,
|
if (load_config (session, config_file_path, config_file_ccnet) < 0) {
|
||||||
"general", "cloud_mode",
|
goto onerror;
|
||||||
NULL);
|
}
|
||||||
|
|
||||||
load_fileserver_config (session);
|
load_fileserver_config (session);
|
||||||
|
|
||||||
notif_enabled = g_key_file_get_boolean (config,
|
|
||||||
"notification", "enabled",
|
|
||||||
NULL);
|
|
||||||
if (notif_enabled) {
|
|
||||||
notif_server = g_key_file_get_string (config,
|
|
||||||
"notification", "host",
|
|
||||||
NULL);
|
|
||||||
notif_port = g_key_file_get_integer (config,
|
|
||||||
"notification", "port",
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
session->notif_server_private_key = g_strdup (private_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (load_database_config (session) < 0) {
|
if (load_database_config (session) < 0) {
|
||||||
seaf_warning ("Failed to load database config.\n");
|
seaf_warning ("Failed to load database config.\n");
|
||||||
goto onerror;
|
goto onerror;
|
||||||
@@ -217,14 +262,6 @@ seafile_session_new(const char *central_config_dir,
|
|||||||
goto onerror;
|
goto onerror;
|
||||||
}
|
}
|
||||||
|
|
||||||
session->seahub_pk = g_strdup (private_key);
|
|
||||||
if (site_root) {
|
|
||||||
session->seahub_url = g_strdup_printf("http://127.0.0.1:8000%sapi/v2.1/internal", site_root);
|
|
||||||
} else {
|
|
||||||
session->seahub_url = g_strdup("http://127.0.0.1:8000/api/v2.1/internal");
|
|
||||||
}
|
|
||||||
session->seahub_conn_pool = connection_pool_new ();
|
|
||||||
|
|
||||||
session->cfg_mgr = seaf_cfg_manager_new (session);
|
session->cfg_mgr = seaf_cfg_manager_new (session);
|
||||||
if (!session->cfg_mgr)
|
if (!session->cfg_mgr)
|
||||||
goto onerror;
|
goto onerror;
|
||||||
@@ -298,12 +335,9 @@ seafile_session_new(const char *central_config_dir,
|
|||||||
if (!session->org_mgr)
|
if (!session->org_mgr)
|
||||||
goto onerror;
|
goto onerror;
|
||||||
|
|
||||||
if (notif_enabled && notif_server != NULL) {
|
if (session->notif_url) {
|
||||||
char notif_url[128];
|
session->notif_mgr = seaf_notif_manager_new (session, session->notif_url);
|
||||||
g_sprintf (notif_url, "%s:%d", notif_server, notif_port);
|
|
||||||
session->notif_mgr = seaf_notif_manager_new (session, g_strdup (notif_url));
|
|
||||||
if (!session->notif_mgr) {
|
if (!session->notif_mgr) {
|
||||||
g_free (notif_url);
|
|
||||||
goto onerror;
|
goto onerror;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -311,8 +345,8 @@ seafile_session_new(const char *central_config_dir,
|
|||||||
return session;
|
return session;
|
||||||
|
|
||||||
onerror:
|
onerror:
|
||||||
g_free (notif_server);
|
g_free (config_file_path);
|
||||||
g_free (notif_server_private_key);
|
g_free (config_file_ccnet);
|
||||||
free (abs_seafile_dir);
|
free (abs_seafile_dir);
|
||||||
free (abs_ccnet_dir);
|
free (abs_ccnet_dir);
|
||||||
g_free (tmp_file_dir);
|
g_free (tmp_file_dir);
|
||||||
|
@@ -94,6 +94,9 @@ struct _SeafileSession {
|
|||||||
// For notification server
|
// For notification server
|
||||||
NotifManager *notif_mgr;
|
NotifManager *notif_mgr;
|
||||||
char *notif_server_private_key;
|
char *notif_server_private_key;
|
||||||
|
char *notif_url;
|
||||||
|
|
||||||
|
gboolean log_to_stdout;
|
||||||
|
|
||||||
gboolean is_repair;
|
gboolean is_repair;
|
||||||
};
|
};
|
||||||
@@ -103,9 +106,7 @@ extern SeafileSession *seaf;
|
|||||||
SeafileSession *
|
SeafileSession *
|
||||||
seafile_session_new(const char *central_config_dir,
|
seafile_session_new(const char *central_config_dir,
|
||||||
const char *seafile_dir,
|
const char *seafile_dir,
|
||||||
const char *ccnet_dir,
|
const char *ccnet_dir);
|
||||||
const char *private_key,
|
|
||||||
const char *site_root);
|
|
||||||
|
|
||||||
SeafileSession *
|
SeafileSession *
|
||||||
seafile_repair_session_new(const char *central_config_dir,
|
seafile_repair_session_new(const char *central_config_dir,
|
||||||
|
Reference in New Issue
Block a user