aboutsummaryrefslogtreecommitdiff
path: root/net/alist/files/alist.init
blob: a06e4e165d38b8076a8c2692a0ad2a33e9523755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh /etc/rc.common

USE_PROCD=1
START=99

CONF="alist"
PROG="/usr/bin/alist"
CONF_DIR="/etc/$CONF"
RUN_DIR="/var/run/$CONF"

uci_json_add_boolean() {
	local enabled
	config_get_bool enabled "${4:-config}" "$2" "${3:-0}"
	json_add_boolean "$1" "$enabled"
}

uci_json_add_int() {
	local value
	config_get value "${4:-config}" "$2" "${3:-0}"
	json_add_int "$1" "$value"
}

uci_json_add_string() {
	local value
	config_get value "${4:-config}" "$2" $3
	json_add_string "$1" "$value"
}

start_service() {
	config_load "$CONF"

	local enabled
	config_get_bool enabled "config" "enabled" "0"
	[ "$enabled" -eq "1" ] || return 1

	local jwt_secret
	jwt_secret="$(jsonfilter -qi "$CONF_DIR/config.json" -e "@.jwt_secret")"
	[ -n "$jwt_secret" ] || jwt_secret="$(tr -cd "a-zA-Z0-9" < "/dev/urandom" | head -c16)"

	mkdir -p "$CONF_DIR"
	mkdir -p "$RUN_DIR"

	json_init
	json_add_boolean "force" "1"
	uci_json_add_string "site_url" "site_url"
	uci_json_add_string "cdn" "site_cdn"
	json_add_string "jwt_secret" "$jwt_secret"
	uci_json_add_int "token_expires_in" "site_login_expire" "48"
	json_add_object "database"
		uci_json_add_string "type" "db_type" "sqlite3"
		uci_json_add_string "host" "db_host"
		uci_json_add_int "port" "db_port"
		uci_json_add_string "user" "db_user"
		uci_json_add_string "password" "db_pass"
		uci_json_add_string "name" "db_name"
		json_add_string "db_file" "$CONF_DIR/data.db"
		uci_json_add_string "table_prefix" "db_table_prefix" "x_"
		uci_json_add_string "ssl_mode" "db_ssl_mode"
	json_close_object
	json_add_object "scheme"
		uci_json_add_string "address" "listen_addr" "0.0.0.0"
		uci_json_add_int "http_port" "listen_http_port" "5244"
		uci_json_add_int "https_port" "listen_https_port" "-1"
		uci_json_add_boolean "force_https" "listen_force_https"
		uci_json_add_string "cert_file" "listen_cert_file"
		uci_json_add_string "key_file" "listen_key_file"
		uci_json_add_string "unix_file" "listen_unix_file"
		uci_json_add_string "unix_file_perm" "listen_unix_file_perm"
	json_close_object
	json_add_string "temp_dir" "$RUN_DIR/temp"
	json_add_string "bleve_dir" "$CONF_DIR/bleve"
	json_add_object "log"
		uci_json_add_boolean "enable" "log_enable" "1"
		json_add_string "name" "$RUN_DIR/log/alist.log"
		uci_json_add_int "max_size" "log_max_size" "5"
		uci_json_add_int "max_backups" "log_max_backups" "1"
		uci_json_add_int "max_age" "log_max_age" "15"
		json_add_boolean "compress" "0"
	json_close_object
	json_add_int "delayed_start" "0"
	uci_json_add_int "max_connections" "site_max_connections"
	uci_json_add_boolean "tls_insecure_skip_verify" "site_tls_insecure"
	json_dump > "$CONF_DIR/config.json"

	local db_type
	config_get db_type "config" "db_type" "sqlite3"
	[ "$db_type" != "sqlite3" -o -e "$CONF_DIR/data.db" ] || "$PROG" --data "$CONF_DIR" admin set "password" 2>"/dev/null"

	procd_open_instance
	procd_set_param command "$PROG"
	procd_append_param command server
	procd_append_param command --data "$CONF_DIR"
	procd_append_param command --no-prefix

	local debug
	config_get_bool debug "config" "debug" "0"
	[ "$debug" -eq "0" ] || procd_append_param command --debug

	procd_set_param limits core="unlimited"
	procd_set_param limits nofile="1000000 1000000"
	procd_set_param respawn
	[ "$debug" -eq "0" ] || procd_set_param stderr 1

	procd_close_instance
}

stop_service() {
	rm -rf "$RUN_DIR"
}

service_triggers() {
	procd_add_reload_trigger "$CONF"
}