1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-14 12:16:32 +00:00
actix-web/guide/src/qs_2.md

99 lines
2.6 KiB
Markdown
Raw Normal View History

2017-11-28 00:41:37 +00:00
# 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
2017-11-29 02:00:10 +00:00
and returns a type that can be converted into `HttpResponse`:
2017-11-28 00:41:37 +00:00
2017-12-04 21:32:05 +00:00
```rust
# extern crate actix_web;
# use actix_web::*;
fn index(req: HttpRequest) -> &'static str {
"Hello world!"
}
# fn main() {}
2017-11-28 00:41:37 +00:00
```
Next, create an `Application` instance and register the
request handler with the application's `resource` on a particular *HTTP method* and *path*::
2017-12-04 21:32:05 +00:00
```rust
# extern crate actix_web;
# use actix_web::*;
# fn index(req: HttpRequest) -> &'static str {
# "Hello world!"
# }
# fn main() {
let app = Application::new()
2017-12-14 06:36:28 +00:00
.resource("/", |r| r.f(index))
2017-12-04 21:32:05 +00:00
.finish();
# }
2017-11-28 00:41:37 +00:00
```
After that, application instance can be used with `HttpServer` to listen for incoming
connections. Server accepts function that should return `HttpHandler` instance:
2017-11-28 00:41:37 +00:00
```rust,ignore
2017-12-20 02:44:17 +00:00
HttpServer::new(|| app)
.bind("127.0.0.1:8088")?
.start();
2017-11-28 00:41:37 +00:00
```
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;
2017-11-29 03:49:17 +00:00
use actix_web::*;
2017-11-28 00:41:37 +00:00
2017-11-28 21:52:53 +00:00
fn index(req: HttpRequest) -> &'static str {
"Hello world!"
2017-11-28 00:41:37 +00:00
}
fn main() {
let sys = actix::System::new("example");
HttpServer::new(
|| Application::new()
.resource("/", |r| r.f(index)))
2017-12-17 20:35:04 +00:00
.bind("127.0.0.1:8088").unwrap()
.start();
2017-11-28 00:41:37 +00:00
println!("Started http server: 127.0.0.1:8088");
2017-12-08 23:25:37 +00:00
# actix::Arbiter::system().send(actix::msgs::SystemExit(0));
2017-11-28 00:41:37 +00:00
let _ = sys.run();
}
```
2017-11-28 03:56:14 +00:00
Note on `actix` crate. Actix web framework is built on top of actix actor library.
`actix::System` initializes actor system, `HttpServer` is an actor and must run within
2017-12-20 02:44:17 +00:00
properly configured actix system. For more information please check
2017-11-28 03:56:14 +00:00
[actix documentation](https://actix.github.io/actix/actix/)