blob: a4d489d44c1eafb2ae209655fe829b4e54ba147c (
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
|
// CSRF protection
// https://github.com/heartsucker/iron-csrf
#[macro_use]
extern crate log;
extern crate systemhttp;
extern crate env_logger;
#[macro_use]
extern crate clap;
extern crate sqlite;
extern crate rpassword;
use rpassword::read_password;
use std::io::{self, Write};
use systemhttp::auth;
use clap::{App, AppSettings, Arg, SubCommand};
fn main() {
let matches = App::new("systemhttpd")
.version("0.1")
.author("Kjetil Ørbekk")
.about("Systemd web frontend")
.setting(AppSettings::SubcommandRequired)
.arg(Arg::with_name("port")
.short("p")
.long("port")
.takes_value(true)
.help("Port to serve on"))
.arg(Arg::with_name("db_file")
.long("db_file")
.takes_value(true)
.help("Path to sqlite database"))
.subcommand(SubCommand::with_name("serve")
.about("Start the systemhttpd server"))
.subcommand(SubCommand::with_name("create_admin_user")
.about("Add an admin user to the db"))
.get_matches();
let port = matches.value_of("port").unwrap_or("8080")
.parse::<u16>().expect("port number");
let db_file = matches.value_of("db_file").unwrap();
env_logger::init().unwrap();
let mut conn = sqlite::Connection::open(db_file)
.expect(format!("opening sqlite database at {}", db_file).as_str());
systemhttp::db::init(&mut conn);
let mut serve = || {
let _server = systemhttp::server::serve(port).unwrap();
println!("Serving on {}", port);
};
let create_admin_user = || {
println!("Create admin user");
print!("Username: ");
io::stdout().flush();
let mut user = String::new();
io::stdin().read_line(&mut user).unwrap();
let password = rpassword::prompt_password_stdout("Password: ").unwrap();
let confirmation =
rpassword::prompt_password_stdout("Repeat password: ").unwrap();
if password != confirmation {
println!("\nPasswords don't match");
return
}
println!("\nCreating user {} with password (hashed) {:?}",
user, auth::encode("test_salt", password.as_str()));
};
match matches.subcommand_name() {
Some("serve") => serve(),
Some("create_admin_user") => create_admin_user(),
x => panic!("Don't know about subcommand: {:?}", x)
}
}
|