Browse Source

Update Thruster to use more streamlined API (#3972)

Peter Mertz 7 years ago
parent
commit
4a3a21cd6d

+ 1 - 1
frameworks/Rust/thruster/Cargo.toml

@@ -9,7 +9,7 @@ serde = "1.0"
 serde_derive = "1.0"
 serde_json = "1.0"
 smallvec = "0.6.2"
-thruster = "0.5.0-beta8"
+thruster = "=0.5.0-beta10"
 
 [profile.release]
 codegen-units = 1

+ 34 - 41
frameworks/Rust/thruster/src/context.rs

@@ -1,58 +1,51 @@
+use std::collections::{HashMap};
 use smallvec::SmallVec;
-use std::collections::HashMap;
 use thruster::{Context, Request, Response};
 
 pub struct Ctx {
-    pub body: String,
-    pub method: String,
-    pub path: String,
-    pub request_body: String,
-    pub params: HashMap<String, String>,
-    pub query_params: HashMap<String, String>,
-    pub headers: SmallVec<[(String, String); 8]>,
-    pub status_code: u32,
+  pub body: String,
+  pub method: String,
+  pub path: String,
+  pub request_body: String,
+  pub params: HashMap<String, String>,
+  pub headers: SmallVec<[(String, String); 8]>,
+  pub status_code: u32,
+  response: Response
 }
 
 impl Ctx {
-    pub fn set_header(&mut self, key: String, val: String) {
-        self.headers.push((key, val));
-    }
+  pub fn set_header(&mut self, key: &str, val: &str) {
+    self.response.header(key, val);
+  }
 }
 
 impl Context for Ctx {
-    fn get_response(&self) -> Response {
-        let mut response = Response::new();
-        response.status_code(200, "OK");
+  fn get_response(mut self) -> Response {
+    self.response.status_code(200, "OK");
 
-        for header_pair in &self.headers {
-            let key: &str = &header_pair.0;
-            let val: &str = &header_pair.1;
-            response.header(key, val);
-        }
+    self.response.body(&self.body);
 
-        response.body(&self.body);
+    self.response
+  }
 
-        response
-    }
-
-    fn set_body(&mut self, body: String) {
-        self.body = body;
-    }
+  fn set_body(&mut self, body: String) {
+    self.body = body;
+  }
 }
 
 pub fn generate_context(request: Request) -> Ctx {
-    let method = request.method().to_owned();
-    let path = request.path().to_owned();
-    let request_body = request.raw_body().to_owned();
-
-    Ctx {
-        body: "".to_owned(),
-        method: method,
-        path: path,
-        params: request.params,
-        query_params: request.query_params,
-        request_body: request_body,
-        headers: SmallVec::new(),
-        status_code: 200,
-    }
+  let method = request.method().to_owned();
+  let path = request.path().to_owned();
+  let request_body = request.raw_body().to_owned();
+
+  Ctx {
+    body: "".to_owned(),
+    method: method,
+    path: path,
+    params: request.params,
+    request_body: request_body,
+    headers: SmallVec::new(),
+    status_code: 200,
+    response: Response::new()
+  }
 }

+ 3 - 3
frameworks/Rust/thruster/src/main.rs

@@ -26,7 +26,7 @@ fn json(mut context: Ctx, _chain: &MiddlewareChain<Ctx>) -> MiddlewareReturnValu
     let val = serde_json::to_string(&json).unwrap();
 
     context.body = val;
-    context.set_header("Content-Type".to_owned(), "application/json".to_owned());
+    context.set_header("Content-Type", "application/json");
 
     Box::new(future::ok(context))
 }
@@ -35,7 +35,7 @@ fn plaintext(mut context: Ctx, _chain: &MiddlewareChain<Ctx>) -> MiddlewareRetur
     let val = "Hello, world!".to_owned();
 
     context.body = val;
-    context.set_header("Content-Type".to_owned(), "text/plain".to_owned());
+    context.set_header("Content-Type", "text/plain");
 
     Box::new(future::ok(context))
 }
@@ -48,5 +48,5 @@ fn main() {
     app.get("/json", vec![json]);
     app.get("/plaintext", vec![plaintext]);
 
-    App::start(app, "0.0.0.0", 8080);
+    App::start_small_load_optimized(app, "0.0.0.0", 8080);
 }