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
use std::{env, fs};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use json::{JsonValue, object};
use df_cache::Cache;
use df_db::db::ModeDb;
use df_db::model::ModelTable;
use df_kafka::Kafka;
use crate::tests::test::test::TestAction;
use crate::tests::test::TestModel;
use crate::tests::TestPlugin;
pub mod tests;
pub fn plugins(name: &str) -> Box<dyn Plugin> {
match name {
_ => Box::new(TestPlugin {})
}
}
pub fn models(name: &str) -> Box<dyn Model> {
match name {
_ => Box::new(TestModel {})
}
}
pub fn actions(name: &str) -> Box<dyn Action> {
match name {
_ => Box::new(TestAction { model: TestModel {} })
}
}
pub trait Plugin {
fn model(&mut self, name: &str) -> Box<dyn Model>;
}
pub trait Model {
fn version(&mut self) -> String {
return "0.0.1".to_string();
}
fn table(&mut self) -> String;
fn title(&mut self) -> String;
fn fields(&mut self) -> JsonValue;
fn unique(&mut self) -> Vec<String> {
return vec![];
}
fn index(&mut self) -> Vec<Vec<String>> {
return vec![];
}
fn primary_key(&mut self) -> String {
return "id".to_string();
}
fn auto(&mut self) -> bool {
return false;
}
fn json(&mut self) -> ModelTable {
ModelTable {
version: self.version(),
table: self.table(),
title: self.title(),
primary_key: self.primary_key(),
auto: self.auto(),
unique: self.unique(),
index: self.index(),
fields: self.fields(),
}
}
fn create_json_file(&mut self, path: &str) -> bool {
let json = self.json();
let o = Path::new(path);
if !o.is_dir() {
fs::create_dir(path).unwrap();
}
env::set_current_dir(path).unwrap();
let dir = env::current_dir().unwrap();
let version = self.version();
let version = version.replace(".", "_");
let dir = dir.join(format!("{}_{}.json", self.table(), version));
let mut f = File::create(dir.to_str().unwrap()).unwrap();
match f.write_all(json.to_string().as_bytes()) {
Ok(_) => true,
Err(_) => false
}
}
fn action(&mut self, name: &str) -> Box<dyn Action>;
}
pub trait Action {
fn title(&mut self) -> String;
fn name(&mut self) -> String;
fn method(&mut self) -> String { "post".to_string() }
fn token(&mut self) -> bool { true }
fn params(&mut self) -> JsonValue { object! {} }
fn _check(&mut self, mut request: JsonValue) -> (bool, String, JsonValue) {
let params = self.params();
let mut new_request = object! {};
for (key, field) in params.entries() {
if request[key].is_empty() && field["require"].as_bool().unwrap() {
return (false, format!("缺少参数 {}:{}", key, field["title"]), request);
}
if request[key].is_empty() && !field["require"].as_bool().unwrap() {
request[key] = field["def"].clone().into()
}
new_request[key] = request[key].clone();
}
return (true, format!("验证通过"), new_request);
}
fn run(&mut self, header: JsonValue, request: JsonValue, db: ModeDb, cache: Cache, kafka: Kafka) -> Response {
let (state, msg, request) = self._check(request.clone());
if !state {
return self.fail(-1, request, msg.as_str().clone());
}
return self.index(header, request, db, cache, kafka);
}
fn index(&mut self, header: JsonValue, request: JsonValue, db: ModeDb, cache: Cache, kafka: Kafka) -> Response;
fn success(&mut self, code: i32, data: JsonValue, msg: &str) -> Response {
Response {
code,
data,
msg: msg.to_string(),
}
}
fn fail(&mut self, code: i32, data: JsonValue, msg: &str) -> Response {
Response {
code,
data,
msg: msg.to_string(),
}
}
}
pub struct Response {
code: i32,
data: JsonValue,
msg: String,
}
impl Response {
pub fn to_json(self) -> JsonValue {
let mut res = object! {};
res["code"] = self.code.into();
res["data"] = self.data.into();
res["msg"] = self.msg.into();
res
}
pub fn success(code: i32, data: JsonValue, msg: &str) -> Self {
Self {
code,
data,
msg: msg.to_string(),
}
}
pub fn fail(code: i32, data: JsonValue, msg: &str) -> Self {
Self {
code,
data,
msg: msg.to_string(),
}
}
}