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
pub mod re_export {
pub use actix_cors::*;
pub use actix_files::*;
pub use actix_identity::*;
pub use actix_web::*;
pub use spa_server_derive::connect;
pub use spa_server_derive::delete;
pub use spa_server_derive::get;
pub use spa_server_derive::head;
pub use spa_server_derive::main;
pub use spa_server_derive::options;
pub use spa_server_derive::patch;
pub use spa_server_derive::post;
pub use spa_server_derive::put;
pub use spa_server_derive::trace;
}
#[doc(hidden)]
pub use include_flate::flate;
pub use spa_server_derive::main;
pub use spa_server_derive::error_to_json;
pub use spa_server_derive::SPAServer;
#[doc(hidden)]
pub use time::Duration;
use log::{debug, warn};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use re_export::*;
use serde::Serialize;
use std::{
borrow::{Borrow, Cow},
collections::HashMap,
env::temp_dir,
fs::create_dir_all,
path::{Path, PathBuf},
};
#[doc(hidden)]
#[actix_web::get("/{tail:[^\\.]+}")]
async fn index(path: web::Data<PathBuf>) -> actix_web::Result<NamedFile> {
Ok(NamedFile::open(path.join("index.html"))?)
}
#[doc(hidden)]
pub fn release_asset<T>() -> Result<PathBuf>
where
T: Embed,
{
let target_dir = temp_dir().join(
thread_rng()
.sample_iter(&Alphanumeric)
.take(8)
.map(char::from)
.collect::<String>(),
);
if !target_dir.exists() {
create_dir_all(&target_dir)?;
}
debug!("release asset target dir: {}", target_dir.to_string_lossy());
for file in T::iter() {
match T::get(file.borrow()) {
None => {
warn!("assert file {} not found", file);
}
Some(p) => {
if let Some(i) = Path::new(file.as_ref()).parent() {
let sub_dir = target_dir.join(i);
create_dir_all(sub_dir)?;
} else {
warn!("no parent part for file {}", file);
continue;
}
let path = target_dir.join(file.as_ref());
debug!("release asset file: {}", path.to_string_lossy());
if let Err(e) = std::fs::write(path, p) {
warn!("asset file {} write failed: {}", file, e);
}
}
}
}
Ok(target_dir)
}
#[doc(hidden)]
pub trait Embed {
fn get(file_path: &str) -> Option<Cow<'static, [u8]>>;
fn iter() -> Filenames;
}
#[doc(hidden)]
pub struct Filenames(pub std::slice::Iter<'static, &'static str>);
impl Iterator for Filenames {
type Item = Cow<'static, str>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|x| Cow::from(*x))
}
}
#[doc(hidden)]
#[derive(Serialize)]
pub struct ErrorMsg {
errors: Vec<HashMap<String, String>>,
}
#[doc(hidden)]
pub fn quick_err(msg: impl Into<String>) -> ErrorMsg {
let hm = [("detail".to_string(), msg.into())]
.iter()
.cloned()
.collect();
ErrorMsg { errors: vec![hm] }
}