Skip to content

Commit 1cee232

Browse files
fafhrd91NateBrady23
authored andcommitted
update framework info; better json handling (#3221)
1 parent f9ed633 commit 1cee232

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

frameworks/Rust/actix/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "actix"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
build = "build.rs"
55

66
[dependencies]
@@ -13,8 +13,8 @@ num_cpus = "1.0"
1313
futures = "0.1"
1414
http = "0.1"
1515
actix = "^0.4.3"
16-
actix-web = "0.3"
17-
diesel = { version = "1.0.0-beta1", features = ["postgres"] }
16+
actix-web = "=0.3.1"
17+
diesel = { version = "1.1", features = ["postgres"] }
1818

1919
[build-dependencies]
2020
askama = "0.5"

frameworks/Rust/actix/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
## Description
44

5-
Actix web is open source rust web framework. It is based on [Tokio](https://tokio.rs).
5+
Actix web is a small, fast, pragmatic, open source rust web framework.
66

77
* [User Guide](http://actix.github.io/actix-web/guide/)
88
* [API Documentation](http://actix.github.io/actix-web/actix_web/)
99
* Cargo package: [actix-web](https://crates.io/crates/actix-web)
1010

11+
## Features
12+
13+
* Supported HTTP/1.x and HTTP/2.0 protocols
14+
* Streaming and pipelining
15+
* Keep-alive and slow requests handling
16+
* WebSockets
17+
* Transparent content compression/decompression (br, gzip, deflate)
18+
* Configurable request routing
19+
* Graceful server shutdown
20+
* Multipart streams
21+
* Middlewares (Logger, Session, DefaultHeaders, CORS)
22+
1123
## Database
1224

1325
PostgreSQL.

frameworks/Rust/actix/src/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Handler<UpdateWorld> for DbExecutor {
5757
fn handle(&mut self, msg: UpdateWorld, _: &mut Self::Context) -> io::Result<()> {
5858
use schema::World::dsl::*;
5959

60-
for world in msg.0 {
60+
for world in &msg.0 {
6161
let _ = diesel::update(World)
6262
.filter(id.eq(world.id))
6363
.set(randomnumber.eq(world.randomnumber))

frameworks/Rust/actix/src/main.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ fn json(_: HttpRequest<State>) -> Result<HttpResponse> {
3030
let message = models::Message {
3131
message: "Hello, World!"
3232
};
33+
let body = serde_json::to_string(&message)?;
34+
3335
Ok(httpcodes::HTTPOk
3436
.build()
3537
.header(header::SERVER, "Actix")
36-
.json(message)?)
38+
.content_type("application/json")
39+
.body(body)?)
3740
}
3841

3942
fn plaintext(_: HttpRequest<State>) -> Result<HttpResponse> {
@@ -48,10 +51,13 @@ fn world_row(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Err
4851
.from_err()
4952
.and_then(|res| {
5053
match res {
51-
Ok(row) => Ok(
52-
httpcodes::HTTPOk.build()
53-
.header(header::SERVER, "Actix")
54-
.json(row)?),
54+
Ok(row) => {
55+
let body = serde_json::to_string(&row).unwrap();
56+
Ok(httpcodes::HTTPOk.build()
57+
.header(header::SERVER, "Actix")
58+
.content_type("application/json")
59+
.body(body)?)
60+
},
5561
Err(_) =>
5662
Ok(httpcodes::HTTPInternalServerError.into()),
5763
}
@@ -81,12 +87,14 @@ fn queries(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
8187
Err(e) => Err(e)
8288
}
8389
)
84-
.and_then(|res|
90+
.and_then(|res| {
91+
let body = serde_json::to_string(&res).unwrap();
8592
Ok(httpcodes::HTTPOk.build()
8693
.header(header::SERVER, "Actix")
94+
.content_type("application/json")
8795
.content_encoding(headers::ContentEncoding::Identity)
88-
.json(res)?)
89-
)
96+
.body(body)?)
97+
})
9098
.responder()
9199
}
92100

@@ -159,8 +167,8 @@ fn fortune(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
159167

160168
Ok(httpcodes::HTTPOk.build()
161169
.header(header::SERVER, "Actix")
162-
.content_encoding(headers::ContentEncoding::Identity)
163170
.content_type("text/html; charset=utf-8")
171+
.content_encoding(headers::ContentEncoding::Identity)
164172
.body(res)?)
165173
},
166174
Err(_) => Ok(httpcodes::HTTPInternalServerError.into())
@@ -169,11 +177,8 @@ fn fortune(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
169177
.responder()
170178
}
171179

172-
use std::env;
173180
fn main() {
174181
let sys = System::new("techempower");
175-
env::set_var("RUST_BACKTRACE", "1");
176-
177182
let dbhost = match option_env!("DBHOST") {
178183
Some(it) => it,
179184
_ => "127.0.0.1"

0 commit comments

Comments
 (0)