DEV Community

Web Development with Rust— 02/x: Deploy your first App

Bastian Gruber on March 19, 2019

You can find the Introduction to web programming in Rust over here. Follow me on twitter to always get the latest information about web developmen...
Collapse
 
czilla profile image
Chenyang Shao • Edited

Update:


#![feature(async_await)]

extern crate tide;

use tide::App;
use std::{env, net::SocketAddr};



fn main() {
    let mut app = App::new(());
    let address = SocketAddr::from(([127, 0, 0, 1], get_server_port()));

    app.at("/").get(async move |_| "hello world");
    app.serve(address).expect("Start server");
}

fn get_server_port() -> u16 {
    env::var("PORT")
        .ok()
        .and_then(|port| port.parse().ok())
        .unwrap_or_else(|| 8186)
}

Collapse
 
gruberb profile image
Bastian Gruber

Thank you so much for the update Chenyang!

Collapse
 
czilla profile image
Chenyang Shao • Edited

thank you for your tutorial~ ^ ^

Collapse
 
franky47 profile image
François Best • Edited

Thanks for the article !

I'm looking into Zeit Now for Rust, but I have a few issues with it:

  • It does not follow standard crates filesystem recommendations, but rather takes a Next.js approach of using filesystem = routes, which means you end up with your endpoint at example.com/src/lib.rs. I see you defined src/index.rs in now.json, but no such file exists, how does this work ?
  • For this reason, I found it hard to integrate serverless endpoints in an existing codebase that uses Cargo workspaces, because that "crate" won't actually compile.
Collapse
 
gruberb profile image
Bastian Gruber

Thanks Francois! I changed the filename:
github.com/gruberb/web-programming...

You are right, serverless mindest is different than a typical cargo app. You can follow the official documentation here: zeit.co/blog/introducing-now-rust

As I mentioned in the article: You don't really create an app when thinking in serverless terms, but you just invoke functions/handlers which process data.

The ZEIT environment will idle your application when it's not needed and start it when triggered (an endpoint is called).

Collapse
 
franky47 profile image
François Best • Edited

What I meant was that in a real-world case, your serverless endpoint/function will probably want to use business/domain/applicative code that is located and organised elsewhere, in Cargo workspaces, and Zeit's approach does not play well with that.

One way that could work would be to have a directory structure as follows:

├── workspace
│ ├── Cargo.toml      Workspace root definition
│ ├── Cargo.lock      Shared lockfile
│ ├── target/         Shared build directory
│ ├── foo
│ └── bar
└── serverless        Zeit Now endpoints
  ├── baz
  | ├── Cargo.toml    [dependencies] foo = { path = "../../workspace/foo" }
  | └── index.rs
  └── qux
    ├── Cargo.toml    [dependencies] bar = { path = "../../workspace/bar" }
    └── index.rs

Edit: after a quick test, this cannot work either, if the crates that the serverless endpoints depend on (here foo and bar) are not published, which would be the case if they are internal to the project.

Also, not having the Cargo.toml workspace root at the root of the project directory disables RLS, for code completion / formatting etc in VSCode.

I guess this is what people mean when they talk about lock-in with serverless, it's not so much about the platform, but the constraints they impose upon your project structure and dependency management.

Collapse
 
czilla profile image
Chenyang Shao • Edited

Thank you!

And if anyone can't compile tide like this

error: internal compiler error: src/librustc_mir/transform/generator.rs:715: Broken MIR: generator contains type std::option::Option<cookies::CookieData> in MIR, but typeck only knows about for<'r> {cookies::CookieData, std::sync::Arc<std::sync::RwLock<cookie::jar::CookieJar>>, std::pin::Pin<std::boxed::Box<(dyn core::future::future::Future<Output = http::response::Response<http_service::Body>> + std::marker::Send + 'r)>>, ()}
  --> /Users/shaochenyang/.cargo/registry/src/github.com-1ecc6299db9ec823/tide-0.2.0/src/lib.rs:21:56
   |
21 |           ::futures::future::FutureExt::boxed(async move { $($t)* })
   |                                                          ^^^^^^^^^^
   | 
  ::: /Users/shaochenyang/.cargo/registry/src/github.com-1ecc6299db9ec823/tide-0.2.0/src/middleware/cookies.rs:34:9
   |
34 | /         box_async! {
35 | |             let cookie_data = cx
36 | |                 .extensions_mut()
37 | |                 .remove()
...  |
58 | |             res
59 | |         }
   | |_________- in this macro invocation

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:578:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: aborting due to previous error


note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.37.0-nightly (de7c4e423 2019-06-23) running on x86_64-apple-darwin

note: compiler flags: -C debuginfo=2 --crate-type lib

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `tide`.

To learn more, run the command again with --verbose.

please see

hope this helps

Collapse
 
cezaryb profile image
cezaryB • Edited

Hey man,

I have been interested in Rust for the last couple months - from the time that I've heard that a new Node.js framework called Deno is written in Rust. This last couple months I've been learning Rust through the official book (doc.rust-lang.org/book/) as well as through the examples and tutorials that I could find online.

Always had one goal in my mind - wanted to built the services and APIs with Rust that I've already built in Node.js in the past.

Anyway, I think you are doing an amazing work, sharing this tutorials and your mindset with other Rust (wannabe) developers - so thank you for that

Collapse
 
jeikabu profile image
jeikabu

Is Zeit a better choice than Lambda when it comes to rust? I haven't taken the time to look into many other options.

Collapse
 
gruberb profile image
Bastian Gruber

Zeit was just so easy to setup. I will look into AWS Lambda at some point in the next few weeks, but the hassle of setting everything up was not worth it to me (for now).

Collapse
 
franky47 profile image
François Best

Now v2 uses AWS Lambdas under the hood, and provide abstractions and automation of a few things to make the process easier.

Collapse
 
loothood profile image
loothood

great article!
Thank you!