1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-25 02:51:15 +00:00

start working on guide

This commit is contained in:
Nikolay Kim 2017-11-27 16:41:37 -08:00
parent b5a4f6f855
commit 599f3c26e0
9 changed files with 141 additions and 8 deletions

5
.gitignore vendored
View file

@ -1,7 +1,7 @@
target/
Cargo.lock Cargo.lock
target/
guide/build/
/gh-pages /gh-pages
__pycache__
*.so *.so
*.out *.out
@ -9,7 +9,6 @@ __pycache__
*.pid *.pid
*.sock *.sock
*~ *~
*.egg-info/
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk

View file

@ -29,6 +29,13 @@ before_script:
script: script:
- USE_SKEPTIC=1 cargo test --features=alpn - USE_SKEPTIC=1 cargo test --features=alpn
# Build without unstable flag
- cargo build
# Test examples in guide.
- rustdoc --test guide/src/qs_2.md -L target/debug -L target/debug/deps
- | - |
if [[ "$TRAVIS_RUST_VERSION" == "nightly" && $CLIPPY ]]; then if [[ "$TRAVIS_RUST_VERSION" == "nightly" && $CLIPPY ]]; then
cargo clippy cargo clippy
@ -40,6 +47,8 @@ after_success:
if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" = "false" && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_RUST_VERSION" == "nightly" ]]; then if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" = "false" && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
cargo doc --features alpn --no-deps && cargo doc --features alpn --no-deps &&
echo "<meta http-equiv=refresh content=0;url=os_balloon/index.html>" > target/doc/index.html && echo "<meta http-equiv=refresh content=0;url=os_balloon/index.html>" > target/doc/index.html &&
cargo install mdbook &&
cd guide && mdbook build -d ../target/doc/guide && cd .. &&
git clone https://github.com/davisp/ghp-import.git && git clone https://github.com/davisp/ghp-import.git &&
./ghp-import/ghp_import.py -n -p -f -m "Documentation upload" -r https://"$GH_TOKEN"@github.com/"$TRAVIS_REPO_SLUG.git" target/doc && ./ghp-import/ghp_import.py -n -p -f -m "Documentation upload" -r https://"$GH_TOKEN"@github.com/"$TRAVIS_REPO_SLUG.git" target/doc &&
echo "Uploaded documentation" echo "Uploaded documentation"

View file

@ -8,7 +8,7 @@ use std::{env, fs};
fn main() { fn main() {
if env::var("USE_SKEPTIC").is_ok() { if env::var("USE_SKEPTIC").is_ok() {
// generates doc tests for `README.md`. // generates doc tests for `README.md`.
skeptic::generate_doc_tests(&["README.md"]); skeptic::generate_doc_tests(&["README.md", "guide/src/qs_2.md"]);
} else { } else {
let f = env::var("OUT_DIR").unwrap() + "/skeptic-tests.rs"; let f = env::var("OUT_DIR").unwrap() + "/skeptic-tests.rs";
let _ = fs::File::create(f); let _ = fs::File::create(f);

View file

@ -11,7 +11,7 @@ use actix_web::error::{Error, Result};
use actix_web::middlewares::RequestSession; use actix_web::middlewares::RequestSession;
use futures::stream::{once, Once}; use futures::stream::{once, Once};
/// somple handle /// simple handler
fn index(mut req: HttpRequest) -> Result<HttpResponse> { fn index(mut req: HttpRequest) -> Result<HttpResponse> {
println!("{:?}", req); println!("{:?}", req);
if let Ok(ch) = req.payload_mut().readany() { if let Ok(ch) = req.payload_mut().readany() {
@ -31,7 +31,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse> {
Ok(httpcodes::HTTPOk.into()) Ok(httpcodes::HTTPOk.into())
} }
/// somple handle /// async handler
fn index_async(req: HttpRequest) -> Once<actix_web::Frame, Error> fn index_async(req: HttpRequest) -> Once<actix_web::Frame, Error>
{ {
println!("{:?}", req); println!("{:?}", req);
@ -43,7 +43,7 @@ fn index_async(req: HttpRequest) -> Once<actix_web::Frame, Error>
.into())) .into()))
} }
/// handle with path parameters like `/user/{name}/` /// handler with path parameters like `/user/{name}/`
fn with_param(req: HttpRequest) -> Result<HttpResponse> fn with_param(req: HttpRequest) -> Result<HttpResponse>
{ {
println!("{:?}", req); println!("{:?}", req);

3
guide/book.toml Normal file
View file

@ -0,0 +1,3 @@
title = "Actix web"
description = "Actix web framework guide"
author = "Actix Project and Contributors"

4
guide/src/SUMMARY.md Normal file
View file

@ -0,0 +1,4 @@
# Summary
[Quickstart](./qs_1.md)
- [Getting Started](./qs_2.md)

34
guide/src/qs_1.md Normal file
View file

@ -0,0 +1,34 @@
# Quickstart
Before you can start writing a actix web application, youll 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.

85
guide/src/qs_2.md Normal file
View file

@ -0,0 +1,85 @@
# Getting Started
Lets create and run our first actix web application. Well 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!
Lets 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();
}
```

View file

@ -1,7 +1,6 @@
//! The `actix-web` prelude //! The `actix-web` prelude
pub use super::*; pub use super::*;
pub use error::*; pub use error::*;
pub use application::ApplicationBuilder; pub use application::ApplicationBuilder;
pub use httpresponse::HttpResponseBuilder; pub use httpresponse::HttpResponseBuilder;