forked from actix/actix-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
target/ | ||
Cargo.lock | ||
target/ | ||
guide/build/ | ||
/gh-pages | ||
__pycache__ | ||
|
||
*.so | ||
*.out | ||
*.pyc | ||
*.pid | ||
*.sock | ||
*~ | ||
*.egg-info/ | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
title = "Actix web" | ||
description = "Actix web framework guide" | ||
author = "Actix Project and Contributors" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Summary | ||
|
||
[Quickstart](./qs_1.md) | ||
- [Getting Started](./qs_2.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Quickstart | ||
|
||
Before you can start writing a actix web application, you’ll need a version of Rust installed. | ||
We recommend you use rustup to install or configure such a version. | ||
|
||
## Install Rust | ||
|
||
Before we begin, we need to install Rust using the [rustup](https://www.rustup.rs/) installer: | ||
|
||
```bash | ||
curl https://sh.rustup.rs -sSf | sh | ||
``` | ||
|
||
If you already have rustup installed, run this command to ensure you have the latest version of Rust: | ||
|
||
```bash | ||
rustup update | ||
``` | ||
|
||
Actix web framework requies rust version 1.20 and up. | ||
|
||
## Running Examples | ||
|
||
The fastest way to start experimenting with actix web is to clone the actix web repository | ||
and run the included examples in the examples/ directory. The following set of | ||
commands runs the `basic` example: | ||
|
||
```bash | ||
git clone https://github.com/actix/actix-web | ||
cd actix-web | ||
cargo run --example basic | ||
``` | ||
|
||
Check `examples/` directory for more examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Getting Started | ||
|
||
Let’s create and run our first actix web application. We’ll create a new Cargo project | ||
that depends on actix web and then run the application. | ||
|
||
In previous section we already installed required rust version. Now let's create new cargo projects. | ||
|
||
## Hello, world! | ||
|
||
Let’s write our first actix web application! Start by creating a new binary-based | ||
Cargo project and changing into the new directory: | ||
|
||
```bash | ||
cargo new hello-world --bin | ||
cd hello-world | ||
``` | ||
|
||
Now, add actix and actix web as dependencies of your project by ensuring your Cargo.toml | ||
contains the following: | ||
|
||
```toml | ||
[dependencies] | ||
actix = "0.3" | ||
actix-web = { git = "https://github.com/actix/actix-web" } | ||
``` | ||
|
||
In order to implement a web server, first we need to create a request handler. | ||
|
||
A request handler is a function that accepts a `HttpRequest` instance as its only parameter | ||
and returns a `HttpResponse` instance or actor that uses `HttpContext` as an actor's context:: | ||
|
||
```rust,ignore | ||
extern crate actix_web; | ||
use actix_web::prelude::*; | ||
fn index(req: HttpRequest) -> Result<HttpResponse> { | ||
Ok(httpcodes::HTTPOk.with_body("Hello world!")) | ||
} | ||
``` | ||
|
||
Next, create an `Application` instance and register the | ||
request handler with the application's `resource` on a particular *HTTP method* and *path*:: | ||
|
||
```rust,ignore | ||
let app = Application::default("/") | ||
.resource("/", |r| r.handler(Method::GET, index) | ||
.finish() | ||
``` | ||
|
||
After that, application instance can be used with `HttpServer` to listen for incoming | ||
connections: | ||
|
||
```rust,ignore | ||
HttpServer::new(app).serve::<_, ()>("127.0.0.1:8088"); | ||
``` | ||
|
||
That's it. Now, compile and run the program with cargo run. | ||
Head over to ``http://localhost:8088/`` to see the results. | ||
|
||
Here is full source of main.rs file: | ||
|
||
```rust | ||
extern crate actix; | ||
extern crate actix_web; | ||
use actix_web::prelude::*; | ||
|
||
fn index(req: HttpRequest) -> Result<HttpResponse> { | ||
Ok(httpcodes::HTTPOk.with_body("Hello world!")) | ||
} | ||
|
||
fn main() { | ||
let sys = actix::System::new("example"); | ||
|
||
HttpServer::new( | ||
Application::default("/") | ||
.resource("/", |r| r.handler(Method::GET, index))) | ||
.serve::<_, ()>("127.0.0.1:8088").unwrap(); | ||
|
||
println!("Started http server: 127.0.0.1:8088"); | ||
// do not copy this line | ||
actix::Arbiter::system().send(actix::msgs::SystemExit(0)); | ||
|
||
let _ = sys.run(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters