-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
319 lines (269 loc) · 11.1 KB
/
lib.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#[macro_use] extern crate log;
#[macro_use] extern crate serde_derive;
#[cfg(feature = "tantivy_bindings")] extern crate tantivy;
#[macro_use] pub mod error;
pub mod bindings;
pub mod logger;
pub mod conf;
use actix::prelude::*;
use actix_lua::LuaActorBuilder;
use actix_web::{server as actix_server, App};
use rlua::prelude::*;
use std::{
path::{Path, PathBuf},
result,
fs, io::prelude::*
};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use serde_json::Value;
use crate::error::Error;
type LuaAddr = ::actix::Addr<::actix_lua::LuaActor>;
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone)]
pub struct AppState {
pub lua: Option<LuaAddr>,
pub init_path: PathBuf,
pub init_args: Vec<String>,
pub package_path: Option<String>,
pub settings: Value,
pub app_settings: Option<(String, Value)>,
}
impl AppState {
pub fn create_vm (&self) -> Result<Lua> {
let lua = unsafe { Lua::new_with_debug() };
lua.context(|lua| {
lua.load(include_str!("handlers/debug.lua")).exec()
})?;
bindings::app::init(&lua)?;
bindings::archive::init(&lua)?;
bindings::crypto::init(&lua)?;
bindings::string::init(&lua)?;
bindings::system::init(&lua)?;
bindings::text::init(&lua)?;
bindings::web::init(&lua)?;
bindings::number::init(&lua)?;
bindings::net::init(&lua)?;
lua.context(|lua| -> result::Result<(), LuaError> {
// torchbear global table
{
let tb_table: LuaTable = lua.create_table()?;
tb_table.set("settings", rlua_serde::to_value(lua, &self.settings).map_err(LuaError::external)?)?;
tb_table.set("init_filename", self.init_path.to_str())?;
tb_table.set("version", env!("CARGO_PKG_VERSION"))?;
let os = if cfg!(target_os = "windows") {
"windows"
} else if cfg!(target_os = "linux") {
"linux"
} else if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "android") {
"android"
} else {
"unknown"
};
tb_table.set("os", os)?;
lua.globals().set("torchbear", tb_table)?;
}
// app table
if let Some((name, app_settings)) = &self.app_settings {
let tb_table = lua.create_table()?;
tb_table.set("settings", rlua_serde::to_value(lua, app_settings)?)?;
lua.globals().set(name.as_str(), tb_table)?;
}
// Lua package.path
match self.package_path {
Some(ref package_path) => {
let package: LuaTable = lua.globals().get("package")?;
let mut path: String = package.get("path")?;
path.push_str(";");
path.push_str(package_path);
package.set("path", path)?;
},
None => ()
}
// Lua arg
let mut cmd_args = self.init_args.clone();
// if no command line argument is passed, push current working directory
if cmd_args.is_empty() {
cmd_args.push(String::from("."));
}
// if file path is symlink, then resolve
match fs::read_link(&cmd_args[0]) {
Ok(p) => cmd_args[0] = String::from(p.to_str().unwrap_or("")),
Err(_) => (),
};
lua.globals().set("arg", lua.create_sequence_from(cmd_args)?)?;
// Lua Bridge
lua.load(include_str!("handlers/bridge.lua")).exec()?;
Ok(())
})?;
Ok(lua)
}
pub fn create_addr (&self) -> LuaAddr {
let vm = self.create_vm().unwrap();
Arbiter::start(move |_| {
let lua_actor = LuaActorBuilder::new()
.on_handle_with_lua(include_str!("handlers/web_server.lua"))
.build_with_vm(vm)
.unwrap();
lua_actor
})
}
}
pub struct ApplicationBuilder {
log_settings: logger::Settings,
}
#[derive(Debug, Default, Deserialize)]
pub struct SettingConfig {
general: Option<Value>,
#[serde(rename = "web-server")]
web_server: Option<Value>,
}
impl ApplicationBuilder {
pub fn new () -> Self {
Self {
log_settings: logger::Settings{
level: logger::LevelFilter::Info,
everything: false,
}
}
}
pub fn log_level (&mut self, level: logger::Level) -> &mut Self {
self.log_settings.level = level.to_level_filter(); self
}
pub fn log_everything (&mut self, b: bool) -> &mut Self {
self.log_settings.everything = b; self
}
pub fn start (&mut self, args: Option<Vec<String>>) -> Result<()> {
openssl_probe::init_ssl_cert_env_vars();
error::create_hook(include_str!("../panic-message.txt"), None, |path, data| {
if let Some(path) = path {
let mut fs = fs::File::create(path)?;
fs.write_all(data.as_bytes())?;
}
Ok(())
});
let mut init_path: Option<PathBuf> = None;
let mut init_args: Vec<String> = vec![];
let mut package_path: Option<String> = None;
match args {
// Interpreter
Some(args) => {
init_path = Path::new(args.first().expect("Missing first argument."))
.canonicalize()
.map_err(|e| {
println!("Error getting the absolute path: {}", e);
std::process::exit(1);
})
.ok();
init_args = args.to_vec();
package_path = match &init_path {
Some(p) => p.parent().map(|p| {
let mut t = p.to_str().expect("Error getting the directory.").to_string();
t.push_str("/?.lua"); t
}),
None => None
};
},
// Server
None => ()
}
fn get_or (map: &Value, key: &str, val: &str) -> String {
map.get(key).map(|s| String::from(s.as_str().unwrap_or(val)) ).unwrap_or(String::from(val))
}
let root_path = match &init_path {
Some(p) => p.parent().unwrap_or(Path::new(".")),
None => Path::new("."),
};
let config_path = root_path.join("torchbear.scl");
let config = if config_path.exists() {
conf::Conf::load_file(&config_path)?
} else {
SettingConfig::default()
};
let general = config.general.unwrap_or_default();
let app_config: Option<(String, Value)> = general.get("app-name").and_then(Value::as_str).map(PathBuf::from).and_then(|name| {
let mut config_path = root_path.join(&name);
config_path.set_extension("scl");
if config_path.exists() && config_path.is_file() {
conf::Conf::load_file(&config_path).map(|s| (name.to_string_lossy().to_string(), s)).ok()
} else {
None
}
});
let init_path = init_path.unwrap_or(PathBuf::from(&get_or(&general, "init", "init.lua")));
if !init_path.exists() || !init_path.is_file() {
println!("Error: Specified init.lua not found. You may have not completed installing your app");
std::process::exit(1);
}
let log_path = general.get("log_path").and_then(|log| log.as_str());
logger::init(log_path, self.log_settings.clone());
let sys = actix::System::new("torchbear");
let mut app_state = AppState {
lua: None,
init_path: init_path,
init_args: init_args,
package_path: package_path,
settings: general,
app_settings: app_config,
};
if let Some(web) = config.web_server {
if let Some(Some(bootstrap)) = web.get("bootstrap_path").map(|s| { s.as_str() }) {
let vm = app_state.create_vm().unwrap();
vm.context(|vm| {
vm.globals().get::<_, LuaTable>("torchbear").unwrap().set("bootstrap", bootstrap).unwrap();
// if !vm.exec::<_, bool>(include_str!("handlers/bootstrap.lua"), Some("bootstrap")).unwrap()
if !vm.load(include_str!("handlers/bootstrap.lua")).set_name("bootstrap").unwrap().eval::<bool>().unwrap()
{ std::process::exit(1); }
});
}
let single_actor = match web.get("single_actor").map(|s| { s.as_bool() }) {
None => false,
Some(Some(b)) => b,
_ => {
println!("Error: Setting web_server.single_actor must be a boolean value");
std::process::exit(1);
},
};
if single_actor {
app_state.lua = Some(app_state.create_addr());
}
log::debug!("web server section in settings, starting seting up web server");
let host = get_or(&web, "address", "0.0.0.0");
let port = get_or(&web, "port", "3000").parse().unwrap_or(3000);
let some_ssl = match (web.get("tls_private"), web.get("tls_certificate")) {
(None, None) => None,
(Some(priv_path), Some(cert_path)) => {
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_private_key_file(priv_path.as_str().unwrap(), SslFiletype::PEM).unwrap();
builder.set_certificate_chain_file(cert_path.as_str().unwrap()).unwrap();
Some(builder)
},
_ => {
println!("Error: SSL needs both tls_private and tls_certificate settings.");
std::process::exit(1);
}
};
let mut server = actix_server::new(move || {
App::with_state(app_state.clone())
.default_resource(|r| r.with(bindings::web::server::handler))
});
server = server.bind((host.as_str(), port))?;
log::debug!("web server listening on port {}:{}", &host, port);
if let Some(ssl_builder) = some_ssl {
let host = get_or(&web, "tls_address", "0.0.0.0");
let port = get_or(&web, "tls_port", "3001").parse().unwrap_or(3001);
server = server.bind_ssl((host.as_str(), port), ssl_builder)?;
log::debug!("tls server listening on port {}:{}", &host, port);
}
server.start();
let _ = sys.run();
} else {
// Temporary fix to run non webserver apps. Doesn't start the actor
// system, just runs a vanilla lua vm.
debug!("Torchbear app started");
let _ = app_state.create_vm()?;
}
Ok(())
}
}