actix / actix-website Goto Github PK
View Code? Open in Web Editor NEWThe Actix Web website.
Home Page: https://actix.rs
License: Apache License 2.0
The Actix Web website.
Home Page: https://actix.rs
License: Apache License 2.0
I recently updated the examples via #132, but some examples and the explanation is still on v1 or v0.x (to make matters worse, some examples' explanation is incorrect.). We should update the documentation for newcomers.
You should update the "Blazingly Fast" performance link on the homepage to point to the latest round of benchmarks!
https://www.techempower.com/benchmarks/#section=data-r18
Well done :D
Under https://actix.rs/book/actix/sec-2-actor.html#spawning-an-actor, the link to the create
API is broken
Hi everyone,
i'm upgrading to v2 and i saw the new macro in the updated documentation :
#[actix_rt::main]
By adding this i got the following error :
failed to resolve: use of undeclared type or module actix_rt
I think the documentation should mention somewhere that the developer must add actix-rt in his cargo.toml :
actix-rt = "1.0.0"
Add instruction how to generate side.
I got error from cobalt:
[nikki]:actix-website$ cobalt --version
Cobalt 0.11.0
[nikki]:actix-website$ cobalt serve
[warn] No _cobalt.yml file found in current directory, using default config.
[warn] Syntax theme named 'base16-ocean.dark' ignored. Reason: Themes are unsupported in this build.
[info] Building from "./actix-website/" into "./actix-website/_site"
Error: serve command failed
Caused by: Failed to parse "more.md"
Caused by: unknown field `extends`, expected one of `permalink`, `slug`, `title`, `description`, `excerpt`, `categories`, `excerpt_separator`, `published_date`, `format`, `layout`, `is_draft`, `data` at line 1 column 1
I've put together a 2-tier scoped routes example that I'd like to pass by the group before submitting it as a PR. Usually, these examples exclude certain parts, for brevity. I assume the actual handler functions would be cut from the PR unless others believe they would be useful so as to have an example that actually compiles.
I don't like having to add a trailing forward slash for my gets. I thought actix handled this automatically?
Thoughts?
extern crate actix;
extern crate actix_web;
extern crate serde;
#[macro_use] extern crate serde_derive;
use actix_web::{server, App, HttpRequest, Responder, http::Method, Path};
fn main() {
// A Project consists of tasks:
// /project/{project_id}/task/{task}
//
let sys = actix::System::new("scope_example");
// Start http server
server::new(|| {
App::new()
.scope("/project", |proj_scope| {
proj_scope
.resource("/", |r| {
r.method(Method::GET)
.f(get_projects);
r.method(Method::POST)
.f(create_project)})
.resource("/{project_id}", |r| {
r.method(Method::PUT)
.with(update_project);
r.method(Method::DELETE)
.f(delete_project)})
.nested("/{project_id}/task", |task_scope| {
task_scope
.resource("/", |r| {
r.method(Method::GET)
.f(get_tasks);
r.method(Method::POST)
.f(create_task)})
.resource("/{task_id}", |r| {
r.method(Method::PUT)
.with(update_task);
r.method(Method::DELETE)
.with(delete_task)})})})
})
.bind("127.0.0.1:8080").unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}
#[derive(Debug, Deserialize)]
struct PathInfo {
project_id: String,
task_id: Option<String>,
}
fn get_projects(req: HttpRequest) -> impl Responder {
"get_projects".to_string()
}
fn create_project(req: HttpRequest) -> impl Responder {
"create_project".to_string()
}
fn update_project(path: Path<PathInfo>) -> impl Responder {
println!("project_id: {:?}, task_id: {:?}", path.project_id, path.task_id);
"update_project".to_string()
}
fn delete_project(req: HttpRequest) -> impl Responder {
"delete_project".to_string()
}
fn get_tasks(req: HttpRequest) -> impl Responder {
"get_tasks".to_string()
}
fn create_task(req: HttpRequest) -> impl Responder {
"create_task".to_string()
}
fn update_task(path: Path<PathInfo>) -> impl Responder {
println!("project_id: {:?}, task_id: {:?}", path.project_id, path.task_id);
"update_task".to_string()
}
fn delete_task(path: Path<PathInfo>) -> impl Responder {
println!("project_id: {:?}, task_id: {:?}", path.project_id, path.task_id);
"delete_task".to_string()
}
For example, the directory example:
use actix_web::{App, fs};
fn main() {
App::new()
.handler(
"/static",
fs::StaticFiles::new(".")
.unwrap()
.show_files_listing())
.finish();
}
Needs to actually run a server
to do anything:
use actix_web::{App, fs, server};
fn main() {
server::new(|| App::new()
.handler(
"/static",
fs::StaticFiles::new(".")
.unwrap()
.show_files_listing()))
.bind("127.0.0.1:8888")
.expect("Can not bind to port 8888")
.run()
}
Lots of other examples have the same problem, in various chapters. I sympathize with wanting to keep noise low, but I would argue you also want your examples to either actually do something when run, or not compile.
After I had copied the logging example from the section about middlewares, I spent some time figuring out why my own logging statements didn't appear in the terminal. The example modifies the RUST_LOG
environment variable, so the different values I tried in the shell had no effect. The example code could be modified to use a default value without overwriting the environment.
link to examples/tls is invalid
A Static File sample is broken here if I try to compile "# Configuration":
error[E0308]: try expression alternatives have incompatible types
--> src/main.rs:20:8
|
20 | Ok(NamedFile::open_with_config(path, MyConfig)?)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected struct `actix_web::fs::DefaultConfig`, found struct `MyConfig`
| help: try wrapping with a success variant: `Ok(NamedFile::open_with_config(path, MyConfig)?)`
|
= note: expected type `actix_web::fs::NamedFile<actix_web::fs::DefaultConfig>`
found type `actix_web::fs::NamedFile<MyConfig>`
the error message is also broken in Rust, see here rust-lang/rust#52537
It seems that the documentation on the Actix Home page (https://actix.rs/docs) reflects code that is still using the prior major release (e.g.: actix-web 1.0.9)
I have been trying to implement the trait FromRequest and have found issues when using the latest web-actix 2.0 release.
Another example is the documentation on testing (https://actix.rs/docs/testing/). The test::block_on
function is no longer available in release 2.0.0
https://actix.rs/docs/errors/responseerrorimpls on this page, at the end of the first section ("Errors").
Official document should upgrade to match version 2.0.0, now it still for actix-web 1.0 series, Need refresh
In the Getting Started page we say:
If you want to use the
#[actix_rt::main]
macro
I am yet to see any alternative to the use of #[actix_rt::main]
macro. If it is there it is not discussed in the guide. It will be easier for the reader if actix_rt
is added as dependency right in the beginning instead of presenting it as a personal choice for the reader.
I tried the following code to do app separation logic
fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
|| {
let app = App::new()
.route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet));
return app;
}
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(routes())
.bind("127.0.0.1:8088")?
.run()
.await
}
But with this compiler is giving me errors-
Compiling main-example v1.0.0 (/home/bob/code/rust/hello_world)
error[E0433]: failed to resolve: use of undeclared type or module `actix_http`
--> src/main.rs:17:83
|
17 | fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
| ^^^^^^^^^^ use of undeclared type or module `actix_http`
error[E0603]: module `app` is private
--> src/main.rs:17:40
|
17 | fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
| ^^^ this module is private
|
note: the module `app` is defined here
--> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-2.0.0/src/lib.rs:81:1
|
81 | mod app;
| ^^^^^^^^
error[E0603]: module `app_service` is private
--> src/main.rs:17:60
|
17 | fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
| ^^^^^^^^^^^ this module is private
|
note: the module `app_service` is defined here
--> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-2.0.0/src/lib.rs:82:1
|
82 | mod app_service;
| ^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `impl std::ops::Fn<()>: std::clone::Clone` is not satisfied
--> src/main.rs:29:21
|
29 | HttpServer::new(routes())
| ^^^^^^^^ the trait `std::clone::Clone` is not implemented for `impl std::ops::Fn<()>`
|
= note: required by `actix_web::server::HttpServer::<F, I, S, B>::new`
error[E0277]: the trait bound `impl std::ops::Fn<()>: std::clone::Clone` is not satisfied
--> src/main.rs:29:5
|
29 | HttpServer::new(routes())
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `impl std::ops::Fn<()>`
|
= note: required by `actix_web::server::HttpServer`
So how can I do separation of routes list from the server declaration?
Tossed up where to file this, settled on here. This is a somewhat lighthearted report, but nonetheless a serious issue for the two or three of us who care about such matters. ☺
The Actix logo is not balanced. If you cut it in metal and used it for the sprocket on a bicycle, it would fail dismally.
This is probably most easily demonstrated by supercharging the spinning of the logo on the website:
.navbar-brand img {
/* The forties are for forty teeth, the 60 for 60fps. */
animation: spin-logo calc(40s/60) steps(40) infinite;
/* While we’re at it, let’s make it as big as we can. */
width: 100vmin;
height: auto;
top: calc(50vh - 50vmin);
left: calc(50vw - 50vmin);
position: fixed;
z-index: 1;
/* You might like to play around with this too, to mitigate some of the image centrepoint unbalancedness: */
/* transform-origin: 49% 50%; */
}
As you observe, the image is not centred properly, and the teeth are irregularly cut because they’re not aligned with the rest.
The logo should have a fixed centre point about which it rotates, rotational symmetry on its teeth (40-way if it must have 40 teeth—for reference, the Rust logo has 32, and I recommend 48 for Actix to factor neatly into the three-way rotational symmetry), and three-way rotational symmetry on the inner portion.
I’m willing to produce a new, mathematically-sound logo if you want (my approach on the teeth is to use Inkscape’s Pattern Along Path live path effect). I still won’t guarantee it will work as a sprocket, especially if it stays fairly similar in the tooth design. Is there an SVG source for the current logo, for mild convenience? (I observe logo.afdesign, but I have nothing that can open that file type.)
I briefly looked for a way to create a PR for this however I am uncertain how or where the examples are being pulled from in the docs.
at the following location: https://actix.rs/docs/handlers/ (Streaming response body)
There is the following example:
use actix_web::{get, App, Error, HttpResponse, HttpServer};
use bytes::Bytes;
use futures::future::ok;
use futures::stream::once;
#[get("/stream")]
async fn stream() -> HttpResponse {
let body = once(ok::<_, Error>(Bytes::from_static(b"test")));
HttpResponse::Ok()
.content_type("application/json")
.streaming(body)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(stream))
.bind("127.0.0.1:8080")?
.run()
.await
}
I believe that Bytes
should be imported from the following location:
use actix_web::web::Bytes;
When first going through this I assumed that I was meant to add this as a dependency. However this will give you an issue of having two different versions of the same type.
Could someone update the docs or point me in the direction where I can make a PR?
Instead of std::sync::Mutex
tokio::sync::Mutex
should be used otherwise the threads can be blocked and the application comes to a full stop.
We have created an authorization middleware for Actix-web based on Casbin: https://github.com/casbin-rs/actix-casbin-auth
Is it OK for us to add a section about it in the official middleware page: https://actix.rs/docs/middleware/ ? If yes, we will make a PR in this repo for you to review.
In the docs for Writing an Application, the code examples linked in from main.rs
are missing.
I quickly compared the markdown page to some other pages where example code is included from a main.rs
file, but I could not spot a difference.
The example here https://actix.rs/docs/application/ for State is outdated compared to https://github.com/actix/examples/tree/master/state
This was reported on this issue on actix-web actix/actix-web#1033
@krircc the latest changes you made to the templates make this a mess to maintain and it broke things more than once. I do not want to change too much now but before you do any more changes can we discuss why things are done the way they are?
baseURL
s in the config? This makes multilingual testing really complicated. It spawns multiple hugo serversI think we could have a lang
directory within content/docs
with a new directory for each language.
content/
lang/
fr/
pt_br/
application.md // all files translated for each language
In the sequence, it would be nice to have some language selector in the header, already based on the existing lang directories.
And to display the languages, I imagine that we will need to customize the layouts/code
so that it renders the Content based on some URL parameter, for example.
https://actix.rs/docs/getting-started?lang=fr
or https://actix.rs/docs/fr/getting-started/
.
I don't know if it is a good way, they are just ideas so that we can simplify the process in the long run.
It would be nice to mention the fact that in addition to using the FromRequest::extract
method, you can also use the implementation of FromRequest
for Option<T: FromRequest>
if you have form data that may or may not exist.
You usually don't control the data being submitted, so it's pretty much always better to use Option<web::Form>
rather than just having actix return "Content type error" to the user.
I am wondering why is hugo used to build actix website?
Currently, I have the Actix website's landing page open in a tab in my Tree Style Tab sidebar as the root of a tree of tabs I use as a "TODO: Try rewriting one of my small web projects in Rust using Actix"... but it makes for a very unhelpful tab title, because it just says "Welcome" and I have a lot of tabs.
It's good practice to include the site/project title in each page title, so it would be "Welcome - Actix" in that case.
it should display a counter as expected.
it display 'App data is not configured, to configure use App::data()' in response
use actix_web::{web, Responder};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[derive(Clone)]
struct AppState {
count: Arc<AtomicUsize>,
}
async fn show_count(data: web::Data<AppState>) -> impl Responder {
format!("count: {}", data.count.load(Ordering::Relaxed))
}
async fn add_one(data: web::Data<AppState>) -> impl Responder {
data.count.fetch_add(1, Ordering::Relaxed);
format!("count: {}", data.count.load(Ordering::Relaxed))
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
let data = AppState {
count: Arc::new(AtomicUsize::new(0)),
};
HttpServer::new(move || {
App::new()
.app_data(data.clone())
.route("/", web::to(show_count))
.route("/add", web::to(add_one))
})
.bind("127.0.0.1:8088")?
.run()
.await
}
changing .app_data to .data makes it work but data can't be shared between threads.
win10x64 1909
rustc -V
): rustc 1.41.0 (5e1a79984 2020-01-27)cargo.toml
[package]
name = "test-actix"
version = "0.1.0"
authors = [""]
edition = "2018"
[dependencies]
actix-web = "2.0"
actix-rt = "1.0"
listenfd = "0.3"
serde = "1.0.105"
The example the Server Basics are as follows right now:
use actix_web::{web, App, HttpResponse, HttpServer};
fn main() {
let sys = actix_rt::System::new("example");
HttpServer::new(|| {
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:8088")
.unwrap()
.start();
let _ = sys.run();
}
This is the first time actix_rt
usage comes up. This was my experience:
Unfortunately, I'm using it, even though I don't understand what its purpose is beyond a somewhat educate guess.
I would suggest that the reader be shown what to do, so they don't have to do the same. I also think it might be important to briefly mention the purpose of the runtime, even if it's just "For now, just know that the server needs this runtime. We cover it in more depth in "
I'm not familiar enough with actix to know what a fix might be, hence the lack of PR. I'm keen to participate how I can though
(updated by @JohnTitor, re-formatted)
Comparing to HTTP/1.1, when I am using HTTP/2
I am losing header "host:". It looks very strange and very suspicious.
I could not found the answer to my question. Can you please help me and comment on the issue?
Thank you very much
I see there were many questions around how to use a sync database API in the GitHub issues and gitter channel. Looks like the easiest and well-accepted method is to use web::block()
. But the database documentation still only mention sync actors.
It would be nice to mention that web::block()
is an option and point the reader to the example code.
The doc only mentions sync actors, which requires a lot of boilerplate code and people keep asking in gitter or github issues.
I apologize if this is already tracked in other documentation update PR, or web::block()
is not that ready yet.
Found dead link to Host Predicate and did not found issue for that.
Thought you may want to fix it.
...provided here:
I'm wondering how this sample is supposed to work. OK, I see the server is started on a separate thread, but if I run this app, the entire server is torn down again immediately, because nobody stops is from doing so.
So what does this sample give?
use actix_web::{web, App, HttpResponse, HttpServer, rt::System};
use std::sync::mpsc;
use std::thread;
#[actix_web::main]
async fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let sys = System::new("http-server");
let srv = HttpServer::new(|| {
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:8080")?
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
.run();
let _ = tx.send(srv);
sys.run()
});
let srv = rx.recv().unwrap();
// pause accepting new connections
srv.pause().await;
// resume accepting new connections
srv.resume().await;
// stop server
srv.stop(true).await;
}
actix.rs currently lacks a featured link to this great resource, Actix Book.
A dark mode would be very nice to have
Hello,
When a file is not available in StaticFiles actix-web automatically shows 404 error without body, i want to show custom error but default_handler not working.
.handler(
"/static",
actix_web::fs::StaticFiles::new("./static/")
.default_handler(p404)
.index_file("index.html"),
)
fn p404(req: actix_web::HttpRequest) -> Result<actix_web::HttpResponse, actix_web::Error> {
println!("{:?}lll", req);
Ok(actix_web::HttpResponse::NotFound()
.content_type("text/plain")
.body("Not Found"))
}
Hi,
since v2 this middleware documentation example won't compile anymore due to lack of Req type declaration: https://actix.rs/docs/middleware/
I've been fiddling around with it to make my middleware compatible, but couldn't figure this one out:
impl<S, B> Transform<S, B> for MyMiddleware
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
the compiler complains about
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
because
wrong number of type arguments: expected 1, found 0
expected 1 type argument
JSON is the primary data format for RESTful service today. Yet the JSON response documentation is buried much later in the Responses
page. Conversely items like form and multi-part requests that are not idiomatic for web services are surfaced much earlier. I will love to see the material re-organized so that JSON request and response comes earlier.
In addition, the JSON response example uses Result<HttpResponse>
as the return type.
async fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().json(MyObj {
name: obj.name.to_string(),
}))
}
While this works it is not strongly typed. A better example will be to use strong typing in return.
async fn index(req: HttpRequest) -> Result<web::Json<MyObj>> {
Ok(web::Json(MyObj {
name: String::from("Hello"),
}))
}
on the page: https://actix.rs/docs/url-dispatch/, the example for scoping routes prepares to go over a nested structure using /project/{project_id}/task/{task_id}, but the code block below for the example reflects something different with /users/show/{id}
The welcome page in docs: https://actix.rs/docs/ doesn't have a next up link unlike the other pages. Not sure if this was intentional.
I expected the main and the only one official website about actix
framework to contain great explanation of it, examples, docs, both, about actix
and actix-web
, but it is only about actix-web
it seems. Why is that?
The database documentation on the website seems outdated to the example code.
Website: https://actix.rs/docs/databases/
Example: https://github.com/actix/examples/tree/master/diesel/
Which is considered more accurate as to how one should use diesel and would updated documentation be accepted in this case?
So the codegen part is great. I mean this syntax: https://github.com/actix/examples/blob/44053504ad39b2bb023712c7577a06091629ad41/basics/src/main.rs#L24
I think it makes a lot of sense to document that as it makes actix-web easier to read and more approachable in general.
The actix.rs website is still focused around 0.7. Wanted to see if there is effort to update it to 1.0 in progress, haven't seen any branches and how to best get started to help.
Example url:
https://actix.rs/docs/installation/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.