]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-api.rs
load auth keys on startup
[proxmox-backup.git] / src / bin / proxmox-backup-api.rs
CommitLineData
fe0e04c6 1extern crate proxmox_backup;
845901f4 2
768e0109
DM
3use std::sync::Arc;
4
d01e2420 5//use proxmox_backup::tools;
fe0e04c6
DM
6use proxmox_backup::api::schema::*;
7use proxmox_backup::api::router::*;
8use proxmox_backup::api::config::*;
9use proxmox_backup::server::rest::*;
10use proxmox_backup::getopts;
6c30068e 11use proxmox_backup::auth_helpers::*;
886e5ce8 12
16b48b81
DM
13use lazy_static::lazy_static;
14
9bc17e8d 15use futures::future::Future;
b82472c0 16
9bc17e8d 17use hyper;
886e5ce8 18
d8d978eb 19fn main() {
d8d978eb 20
d96d8273
DM
21 if let Err(err) = syslog::init(
22 syslog::Facility::LOG_DAEMON,
23 log::LevelFilter::Info,
24 Some("proxmox-backup-api")) {
25 eprintln!("unable to inititialize syslog: {}", err);
26 std::process::exit(-1);
27 }
28
39a90ca6 29 if let Err(err) = generate_auth_key() {
8d04280b
DM
30 eprintln!("unable to generate auth key: {}", err);
31 std::process::exit(-1);
32 }
d01e2420 33 let _ = private_auth_key(); // load with lazy_static
8d04280b 34
39a90ca6
DM
35 if let Err(err) = generate_csrf_key() {
36 eprintln!("unable to generate csrf key: {}", err);
37 std::process::exit(-1);
38 }
d01e2420 39 let _ = csrf_secret(); // load with lazy_static
39a90ca6 40
73f29c34
DM
41 let command : Arc<Schema> = StringSchema::new("Command.")
42 .format(Arc::new(ApiStringFormat::Enum(vec![
43 "start".into(),
44 "status".into(),
45 "stop".into()
46 ])))
47 .into();
82df76ff 48
7edeec7b 49 let schema = ObjectSchema::new("Parameters.")
73f29c34 50 .required("command", command);
845901f4
DM
51
52 let args: Vec<String> = std::env::args().skip(1).collect();
73f29c34
DM
53
54 let options = match getopts::parse_arguments(&args, &vec!["command"], &schema) {
0c9ce2bb 55 Ok((options, rest)) => {
73f29c34
DM
56 if !rest.is_empty() {
57 eprintln!("Error: got additional arguments: {:?}", rest);
58 std::process::exit(-1);
59 }
60 options
0c9ce2bb
DM
61 }
62 Err(err) => {
73f29c34 63 eprintln!("Error: unable to parse arguments:\n{}", err);
0c9ce2bb
DM
64 std::process::exit(-1);
65 }
73f29c34
DM
66 };
67
68 let command = options["command"].as_str().unwrap();
69
70 match command {
71 "start" => {
72 println!("Starting server.");
73 },
74 "stop" => {
75 println!("Stopping server.");
76 std::process::exit(0);
77 },
78 "status" => {
79 println!("Server status.");
80 std::process::exit(0);
81 },
82 _ => {
83 eprintln!("got unexpected command {}", command);
84 std::process::exit(-1);
85 },
0c9ce2bb 86 }
845901f4 87
02c7a755 88 let addr = ([127,0,0,1], 82).into();
886e5ce8 89
a9696f7b 90 lazy_static!{
576e3bf2 91 static ref ROUTER: Router = proxmox_backup::api2::router();
a9696f7b 92 }
324a5bd0 93
02c7a755
DM
94 let config = ApiConfig::new(
95 "/usr/share/javascript/proxmox-backup", &ROUTER, RpcEnvironmentType::PRIVILEDGED);
324a5bd0 96
9bc17e8d 97 let rest_server = RestServer::new(config);
886e5ce8 98
9bc17e8d
DM
99 let server = hyper::Server::bind(&addr)
100 .serve(rest_server)
886e5ce8
DM
101 .map_err(|e| eprintln!("server error: {}", e));
102
0f52af5b 103
886e5ce8
DM
104 // Run this server for... forever!
105 hyper::rt::run(server);
d8d978eb 106}