add domain argument for init DB

This commit is contained in:
jb-alvarado 2022-07-08 14:44:21 +02:00
parent 5bbc7a75c4
commit 28d4262b42
5 changed files with 14 additions and 7 deletions

2
Cargo.lock generated
View File

@ -1027,7 +1027,7 @@ dependencies = [
[[package]] [[package]]
name = "ffplayout-api" name = "ffplayout-api"
version = "0.4.1" version = "0.4.2"
dependencies = [ dependencies = [
"actix-multipart", "actix-multipart",
"actix-web", "actix-web",

View File

@ -4,7 +4,7 @@ description = "Rest API for ffplayout"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["Jonathan Baecker jonbae77@gmail.com"] authors = ["Jonathan Baecker jonbae77@gmail.com"]
readme = "README.md" readme = "README.md"
version = "0.4.1" version = "0.4.2"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -14,6 +14,9 @@ pub struct Args {
#[clap(short, long, help = "Initialize Database")] #[clap(short, long, help = "Initialize Database")]
pub init: bool, pub init: bool,
#[clap(short, long, help = "domain name for initialization")]
pub domain: Option<String>,
#[clap(short, long, help = "Create admin user")] #[clap(short, long, help = "Create admin user")]
pub username: Option<String>, pub username: Option<String>,

View File

@ -81,7 +81,7 @@ async fn create_schema() -> Result<SqliteQueryResult, sqlx::Error> {
result result
} }
pub async fn db_init() -> Result<&'static str, Box<dyn std::error::Error>> { pub async fn db_init(domain: Option<String>) -> Result<&'static str, Box<dyn std::error::Error>> {
let db_path = db_path()?; let db_path = db_path()?;
if !Sqlite::database_exists(&db_path).await.unwrap_or(false) { if !Sqlite::database_exists(&db_path).await.unwrap_or(false) {
@ -99,6 +99,11 @@ pub async fn db_init() -> Result<&'static str, Box<dyn std::error::Error>> {
let instances = db_connection().await?; let instances = db_connection().await?;
let url = match domain {
Some(d) => format!("http://{d}/live/stream.m3u8"),
None => "http://localhost/live/stream.m3u8".to_string(),
};
let query = "CREATE TRIGGER global_row_count let query = "CREATE TRIGGER global_row_count
BEFORE INSERT ON global BEFORE INSERT ON global
WHEN (SELECT COUNT(*) FROM global) >= 1 WHEN (SELECT COUNT(*) FROM global) >= 1
@ -107,8 +112,7 @@ pub async fn db_init() -> Result<&'static str, Box<dyn std::error::Error>> {
END; END;
INSERT INTO global(secret) VALUES($1); INSERT INTO global(secret) VALUES($1);
INSERT INTO settings(channel_name, preview_url, config_path, extra_extensions, timezone, service) INSERT INTO settings(channel_name, preview_url, config_path, extra_extensions, timezone, service)
VALUES('Channel 1', 'http://localhost/live/preview.m3u8', VALUES('Channel 1', $2, '/etc/ffplayout/ffplayout.yml', 'jpg,jpeg,png', 'UTC', 'ffplayout.service');
'/etc/ffplayout/ffplayout.yml', 'jpg,jpeg,png', 'UTC', 'ffplayout.service');
INSERT INTO roles(name) VALUES('admin'), ('user'), ('guest'); INSERT INTO roles(name) VALUES('admin'), ('user'), ('guest');
INSERT INTO presets(name, text, x, y, fontsize, line_spacing, fontcolor, box, boxcolor, boxborderw, alpha, channel_id) INSERT INTO presets(name, text, x, y, fontsize, line_spacing, fontcolor, box, boxcolor, boxborderw, alpha, channel_id)
VALUES('Default', 'Wellcome to ffplayout messenger!', '(w-text_w)/2', '(h-text_h)/2', '24', '4', '#ffffff@0xff', '0', '#000000@0x80', '4', '1.0', '1'), VALUES('Default', 'Wellcome to ffplayout messenger!', '(w-text_w)/2', '(h-text_h)/2', '24', '4', '#ffffff@0xff', '0', '#000000@0x80', '4', '1.0', '1'),
@ -117,7 +121,7 @@ pub async fn db_init() -> Result<&'static str, Box<dyn std::error::Error>> {
'1', '#000000@0x80', '4', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),0,if(lt(t,ld(1)+2),(t-(ld(1)+1))/1,if(lt(t,ld(1)+8),1,if(lt(t,ld(1)+9),(1-(t-(ld(1)+8)))/1,0))))', '1'), '1', '#000000@0x80', '4', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),0,if(lt(t,ld(1)+2),(t-(ld(1)+1))/1,if(lt(t,ld(1)+8),1,if(lt(t,ld(1)+9),(1-(t-(ld(1)+8)))/1,0))))', '1'),
('Scrolling Text', 'We have a very important announcement to make.', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),w+4,w-w/12*mod(t-ld(1),12*(w+tw)/w))', '(h-line_h)*0.9', ('Scrolling Text', 'We have a very important announcement to make.', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),w+4,w-w/12*mod(t-ld(1),12*(w+tw)/w))', '(h-line_h)*0.9',
'24', '4', '#ffffff', '1', '#000000@0x80', '4', '1.0', '1');"; '24', '4', '#ffffff', '1', '#000000@0x80', '4', '1.0', '1');";
sqlx::query(query).bind(secret).execute(&instances).await?; sqlx::query(query).bind(secret).bind(url).execute(&instances).await?;
instances.close().await; instances.close().await;
Ok("Database initialized!") Ok("Database initialized!")

View File

@ -99,7 +99,7 @@ pub async fn run_args(mut args: Args) -> Result<(), i32> {
} }
if args.init { if args.init {
if let Err(e) = db_init().await { if let Err(e) = db_init(args.domain).await {
panic!("{e}"); panic!("{e}");
}; };