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
use std::{fmt, env, str, path::PathBuf, marker::PhantomData};
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
use tokio::net::{TcpListener, TcpStream};
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
use tokio::sync::Semaphore;
use tokio::{fs, time};
use toml;
use toml::value::Value;
use toml::map::Map;
use rand::seq::SliceRandom;
use rand::rngs::StdRng;
use rand::SeedableRng;
use sha2::{Digest, Sha256};
use crate::db::{DbPool, DbRow};
use crate::records::{VarChar, DateTime, DataType};
use crate::web::{BASE_DIR, Result, Static, Route, BaseRequest, RawRequest, Response, Http404, WebError, WebErrorKind, View, route_request, route_path};
use crate::router::{Router, get_capture, split_url};
use crate::migrations::{migrate, sql_migrate, make_migrations, AppMigration};
use crate::admin_site::AdminRef;
pub type Settings = Map<String, Value>;
type Timer = Arc<Mutex<DateTime>>;
#[macro_export]
macro_rules! main {
() => {
pub mod prelude {
pub use async_trait::async_trait;
pub use crate::project::Request;
pub use anansi::{form, import, record_view, base_view, view, redirect, transact, form_error};
pub use anansi::web::{Result, Response, BaseUser};
pub use anansi::forms::Form;
pub use anansi::records::Record;
pub use anansi::util::auth::records::Group;
}
fn main() {
use std::sync::{Arc, Mutex};
use crate::urls::app_url;
use crate::http_errors::views::ErrorView;
use anansi::util::admin::site::AdminSite;
use anansi::web::Response;
let internal_error = Response::internal_error(include_bytes!("http_errors/500.html"));
let site = Arc::new(Mutex::new(anansi::util::admin::site::BasicAdminSite::new()));
anansi::server::Server::new(APP_STATICS, APP_ADMINS).run(app_url, urls::ROUTES, ErrorView::not_found, internal_error, app_migrations, anansi::util::auth::cmd::admin, site);
}
}
}
pub struct Server<B: BaseRequest + 'static, D: DbPool> {
statics: &'static [&'static [Static]],
admin_inits: AdminInits<B>,
d: PhantomData<D>,
}
pub type AdminInits<B> = &'static [fn(AdminRef<B>)];
impl<B: BaseRequest<SqlPool = D> + fmt::Debug + Clone, D: DbPool + 'static> Server<B, D> {
pub fn new(statics: &'static [&'static [Static]], admin_inits: AdminInits<B>) -> Self {
Self {statics, admin_inits, d: PhantomData}
}
pub fn run(&mut self, url_mapper: fn(&mut HashMap<usize, Vec<String>>), routes: &[Route<B>], handle_404: View<B>, internal_error: Response, migrations: fn() -> Vec<AppMigration<D>>, admin: fn(D) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send>>, site: AdminRef<B>) where <<D as DbPool>::SqlRowVec as IntoIterator>::Item: DbRow {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let addr = "127.0.0.1:9090";
let listener = TcpListener::bind(addr).await.unwrap();
let args: Vec<String> = env::args().collect();
let mut base = PathBuf::new();
BASE_DIR.with(|b| base = b.clone());
base.push("settings.toml");
let s_settings = fs::read_to_string(&base).await.expect(&format!("Could not find {}", base.to_str().unwrap()));
let settings: Settings = toml::from_str(&s_settings).expect("Could not parse settings.toml");
let pool = match D::new(&settings).await {
Ok(p) => p,
Err(_) => {
let p = D::new(&settings).await.expect("Database connection reattempt failed");
migrate(migrations(), &p).await;
p
}
};
if args.len() > 1 {
match args[1].as_str() {
"make-migrations" => {
if args.len() >= 2 {
make_migrations(&args[2], &pool).await;
} else {
eprintln!("expected app name");
}
}
"sql-migrate" => {
if args.len() >= 3 {
sql_migrate(migrations(), &args[2], &args[3]).await;
} else{
eprintln!("expected app name");
}
}
"migrate" => {
migrate(migrations(), &pool).await;
}
"admin" => {
admin(pool.clone()).await.expect("Could not create admin");
}
_ => eprintln!("Unrecognized argument"),
}
} else {
let mut url_map = HashMap::new();
url_mapper(&mut url_map);
let mut files = HashMap::new();
for stats in self.statics {
for (name, file) in *stats {
files.insert(*name, *file);
}
}
let mut rv = routes.to_vec();
for admin_init in self.admin_inits {
admin_init(site.clone());
}
for (name, view) in site.lock().unwrap().urls() {
url_map.insert(*view as View<B> as usize, get_capture(name).unwrap());
rv.push(route_path(name, *view));
}
let login_url = settings.get("login_url").expect("Could not get login url").as_str().expect("Expected string for login url").to_string();
let router = Arc::new(Router::new(rv, handle_404, internal_error, login_url, files).unwrap());
let urls = Arc::new(url_map);
let mut seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs().to_string();
let std_rng = match settings.get("secret_key") {
Some(key) => {
seed += key.as_str().expect("Could not get secret");
Rng::new(&seed)
}
None => {
let rng = Rng::new(&seed);
let s = format!("secret_key = \"{}\"\n{}", rng.secret_string(), s_settings);
let mut file = fs::OpenOptions::new()
.write(true)
.open(&base)
.await
.expect(&format!("error with {}", base.to_str().unwrap()));
file.write_all(&s.into_bytes()).await.unwrap();
println!("Created new secret");
rng
}
};
let sem = Arc::new(Semaphore::new(10000000));
let timer = Arc::new(Mutex::new(DateTime::now()));
let t2 = Arc::clone(&timer);
tokio::spawn(async move {
loop {
time::sleep(time::Duration::from_secs(1)).await;
let mut t2 = t2.lock().unwrap();
*t2 = DateTime::now();
}
});
println!("Server running at http://{addr}/\nPress Ctrl+C to stop");
loop {
let (stream, _) = listener.accept().await.unwrap();
let urls = urls.clone();
let pool = pool.clone();
let std_rng = std_rng.clone();
let router = router.clone();
let sem = Arc::clone(&sem);
let timer = Arc::clone(&timer);
let aq = sem.try_acquire();
let site = site.clone();
if aq.is_ok() {
tokio::spawn(async move {
handle_connection(stream, urls, pool, std_rng, router, timer, site).await.unwrap();
});
} else {
eprintln!("Open socket limit reached");
}
}
}
})
}
}
#[derive(Clone, Debug)]
pub struct Rng(Arc<Mutex<StdRng>>);
impl Rng {
pub fn new(seed: &str) -> Self {
let mut hasher = Sha256::new();
hasher.update(seed.as_bytes());
let hash: [u8; 32] = hasher.finalize().as_slice().try_into().unwrap();
Self {0: Arc::new(Mutex::new(StdRng::from_seed(hash)))}
}
pub fn secret_string(&self) -> String {
let choices = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".as_bytes();
let len = 32;
let mut bytes = Vec::with_capacity(len);
for _ in 0..len {
bytes.push(*choices.choose(&mut *self.0.lock().unwrap()).unwrap());
}
String::from_utf8(bytes).unwrap()
}
pub fn new_secret(&self) -> VarChar<32> {
VarChar::<32>::from_val(self.secret_string()).unwrap()
}
}
pub async fn handle_connection<B: BaseRequest<SqlPool = D> + 'static + fmt::Debug, D: DbPool>(mut stream: TcpStream, urls: Arc<HashMap<usize, Vec<String>>>, pool: D, std_rng: Rng, router: Arc<Router<B>>, timer: Timer, site: AdminRef<B>) -> Result<()> {
let mut buffer = vec![0; 1024];
while let Ok(l) = time::timeout(time::Duration::from_secs(5), stream.read(&mut buffer)).await {
let length = l.unwrap();
if length == 0 {
break;
}
let urls = urls.clone();
let pool = pool.clone();
let std_rng = std_rng.clone();
let site = site.clone();
let mut response = {
let (n, request_line) = RawRequest::<D>::get_request_line(&buffer[..length])?;
let url = request_line.url.clone();
let dirs = split_url(&url)?;
if dirs[0] == "/static" {
match router.serve_static(&url).await {
Ok(r) => r,
Err(_) => router.internal_error.clone(),
}
} else {
let result = B::new(&buffer[n..length], request_line, urls, pool.clone(), std_rng.clone(), site).await;
match result {
Ok(mut req) => {
match route_request(dirs, &mut req, &router.routes).await {
Ok(res) => {
res
}
Err(error) => {
if let Some(web_error) = error.downcast_ref::<WebError>() {
if web_error.kind() == &WebErrorKind::Unauthenticated {
Response::redirect(&router.login_url)
} else {
router.internal_error.clone()
}
} else if let Ok(_http_404) = error.downcast::<Http404>() {
match (router.handle_404)(&mut req).await {
Ok(r) => r,
Err(_) => router.internal_error.clone(),
}
} else {
router.internal_error.clone()
}
}
}
}
Err(error) => {
if let Ok(web_error) = error.downcast::<WebError>() {
match web_error.kind() {
WebErrorKind::NoSession => {
match B::handle_no_session(Response::redirect(&url), pool, std_rng).await {
Ok(r) => {
r
}
Err(_) => {
router.internal_error.clone()
}
}
}
WebErrorKind::Unauthenticated => {
Response::redirect(&router.login_url)
}
WebErrorKind::Invalid => {
router.internal_error.clone()
}
}
} else {
router.internal_error.clone()
}
}
}
}
};
{
let timer = timer.lock().unwrap();
response.headers_mut().insert("Date".to_string(), format!("{timer}"));
}
stream.write(&response.into_bytes()).await.unwrap();
}
Ok(())
}