-
Notifications
You must be signed in to change notification settings - Fork 141
/
main.rs
103 lines (86 loc) 路 2.52 KB
/
main.rs
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
// SPDX-FileCopyrightText: 漏 2022 Svix Authors
// SPDX-License-Identifier: MIT
#![warn(clippy::all)]
#![forbid(unsafe_code)]
use dotenv::dotenv;
use std::process::exit;
use svix_server::core::security::{default_org_id, generate_org_token};
use svix_server::{cfg, db, run};
const CRATE_NAME: &str = env!("CARGO_CRATE_NAME");
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[clap(author, version, about = env!("CARGO_PKG_DESCRIPTION"), long_about = None)]
struct Args {
#[clap(subcommand)]
command: Option<Commands>,
/// Run database migrations before starting
#[clap(long, value_parser)]
run_migrations: bool,
}
#[derive(Subcommand)]
enum Commands {
/// JWT utilities
#[clap()]
Jwt {
#[clap(subcommand)]
command: JwtCommands,
},
/// Run database migrations and exit
#[clap()]
Migrate,
}
#[derive(Subcommand)]
enum JwtCommands {
/// Generate a new JWT
#[clap()]
Generate,
}
#[tokio::main]
async fn main() {
dotenv().ok();
let args = Args::parse();
let cfg = cfg::load().expect("Error loading configuration");
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var(
"RUST_LOG",
format!(
"{crate}={level},tower_http={level}",
crate = CRATE_NAME,
level = cfg.log_level.to_string()
),
);
}
match cfg.log_format {
cfg::LogFormat::Default => {
tracing_subscriber::fmt::init();
}
cfg::LogFormat::Json => {
let fmt = tracing_subscriber::fmt::format().json().flatten_event(true);
let json_fields = tracing_subscriber::fmt::format::JsonFields::new();
let filter = tracing_subscriber::EnvFilter::from_default_env();
tracing_subscriber::fmt()
.event_format(fmt)
.fmt_fields(json_fields)
.with_env_filter(filter)
.init();
}
};
if args.run_migrations {
db::run_migrations(&cfg).await;
println!("Migrations run");
}
if let Some(Commands::Migrate) = &args.command {
db::run_migrations(&cfg).await;
println!("Migrations run");
exit(0);
} else if let Some(Commands::Jwt {
command: JwtCommands::Generate,
}) = &args.command
{
let token =
generate_org_token(&cfg.jwt_secret, default_org_id()).expect("Error generating token");
println!("Token (Bearer): {}", token);
exit(0);
}
run(cfg, None).await;
}