Przeglądaj źródła

update framework info; better json handling (#3221)

Nikolay Kim 7 lat temu
rodzic
commit
1cee232c04

+ 3 - 3
frameworks/Rust/actix/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "actix"
-version = "0.1.0"
+version = "0.1.1"
 build = "build.rs"
 
 [dependencies]
@@ -13,8 +13,8 @@ num_cpus = "1.0"
 futures = "0.1"
 http = "0.1"
 actix = "^0.4.3"
-actix-web = "0.3"
-diesel = { version = "1.0.0-beta1", features = ["postgres"] }
+actix-web = "=0.3.1"
+diesel = { version = "1.1", features = ["postgres"] }
 
 [build-dependencies]
 askama = "0.5"

+ 13 - 1
frameworks/Rust/actix/README.md

@@ -2,12 +2,24 @@
 
 ## Description
 
-Actix web is open source rust web framework. It is based on [Tokio](https://tokio.rs).
+Actix web is a small, fast, pragmatic, open source rust web framework.
 
 * [User Guide](http://actix.github.io/actix-web/guide/)
 * [API Documentation](http://actix.github.io/actix-web/actix_web/)
 * Cargo package: [actix-web](https://crates.io/crates/actix-web)
 
+## Features
+
+* Supported HTTP/1.x and HTTP/2.0 protocols
+* Streaming and pipelining
+* Keep-alive and slow requests handling
+* WebSockets
+* Transparent content compression/decompression (br, gzip, deflate)
+* Configurable request routing
+* Graceful server shutdown
+* Multipart streams
+* Middlewares (Logger, Session, DefaultHeaders, CORS)
+
 ## Database
 
 PostgreSQL.

+ 1 - 1
frameworks/Rust/actix/src/db.rs

@@ -57,7 +57,7 @@ impl Handler<UpdateWorld> for DbExecutor {
     fn handle(&mut self, msg: UpdateWorld, _: &mut Self::Context) -> io::Result<()> {
         use schema::World::dsl::*;
 
-        for world in msg.0 {
+        for world in &msg.0 {
             let _ = diesel::update(World)
                 .filter(id.eq(world.id))
                 .set(randomnumber.eq(world.randomnumber))

+ 17 - 12
frameworks/Rust/actix/src/main.rs

@@ -30,10 +30,13 @@ fn json(_: HttpRequest<State>) -> Result<HttpResponse> {
     let message = models::Message {
         message: "Hello, World!"
     };
+    let body = serde_json::to_string(&message)?;
+
     Ok(httpcodes::HTTPOk
        .build()
        .header(header::SERVER, "Actix")
-       .json(message)?)
+       .content_type("application/json")
+       .body(body)?)
 }
 
 fn plaintext(_: HttpRequest<State>) -> Result<HttpResponse> {
@@ -48,10 +51,13 @@ fn world_row(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Err
         .from_err()
         .and_then(|res| {
             match res {
-                Ok(row) => Ok(
-                    httpcodes::HTTPOk.build()
-                        .header(header::SERVER, "Actix")
-                        .json(row)?),
+                Ok(row) => {
+                    let body = serde_json::to_string(&row).unwrap();
+                    Ok(httpcodes::HTTPOk.build()
+                       .header(header::SERVER, "Actix")
+                       .content_type("application/json")
+                       .body(body)?)
+                },
                 Err(_) =>
                     Ok(httpcodes::HTTPInternalServerError.into()),
             }
@@ -81,12 +87,14 @@ fn queries(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
                 Err(e) => Err(e)
             }
         )
-        .and_then(|res|
+        .and_then(|res| {
+            let body = serde_json::to_string(&res).unwrap();
             Ok(httpcodes::HTTPOk.build()
                .header(header::SERVER, "Actix")
+               .content_type("application/json")
                .content_encoding(headers::ContentEncoding::Identity)
-               .json(res)?)
-        )
+               .body(body)?)
+        })
         .responder()
 }
 
@@ -159,8 +167,8 @@ fn fortune(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
 
                     Ok(httpcodes::HTTPOk.build()
                        .header(header::SERVER, "Actix")
-                       .content_encoding(headers::ContentEncoding::Identity)
                        .content_type("text/html; charset=utf-8")
+                       .content_encoding(headers::ContentEncoding::Identity)
                        .body(res)?)
                 },
                 Err(_) => Ok(httpcodes::HTTPInternalServerError.into())
@@ -169,11 +177,8 @@ fn fortune(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
         .responder()
 }
 
-use std::env;
 fn main() {
     let sys = System::new("techempower");
-    env::set_var("RUST_BACKTRACE", "1");
-
     let dbhost = match option_env!("DBHOST") {
         Some(it) => it,
         _ => "127.0.0.1"