GithubHelp home page GithubHelp logo

doytsujin / rust-3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vercel-community/rust

0.0 1.0 0.0 475 KB

๐Ÿฆ€ Rust runtime for โ–ฒ Vercel Serverless Functions.

License: MIT License

JavaScript 8.86% Rust 56.55% TypeScript 34.59%

rust-3's Introduction

Rust

Rust runtime for Vercel Functions.

Community-maintained package to support using Rust inside Vercel Functions as a Runtime.

Usage

First, you'll need a vercel.json file in your project:

{
  "functions": {
    "api/**/*.rs": {
      "runtime": "[email protected]"
    }
  }
}

A Vercel Function will be created for every file that matches api/**/*.rs. Next, you can create a new Function api/user.rs:

use http::{StatusCode};
use vercel_lambda::{lambda, error::VercelError, IntoResponse, Request, Response};
use std::error::Error;

fn handler(_: Request) -> Result<impl IntoResponse, VercelError> {
	let response = Response::builder()
		.status(StatusCode::OK)
		.header("Content-Type", "text/plain")
		.body("Hello World")
		.expect("Internal Server Error");

		Ok(response)
}

// Start the runtime with the handler
fn main() -> Result<(), Box<dyn Error>> {
	Ok(lambda!(handler))
}

Finally, we need an api/Cargo.toml file:

[package]
name = "index"
version = "1.0.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"

[dependencies]
http = "0.1"
vercel_lambda = "*"

[lib]
name = "util"
path = "_util.rs"

Note: Cargo.toml must exist on the same level as the .rs files.

Dependencies

This Builder supports installing dependencies defined in the Cargo.toml file.

Furthermore, more system dependencies can be installed at build time with the presence of a shell build.sh file in the same directory as the entrypoint file.

Local Development

With vercel dev and vercel-rust, you can develop your Rust-based lamdas on your own machine.

During local development with vercel dev, ensure rust and cargo are already installed and available in your PATH, since they will not be installed automatically. The recommended way to install rust and cargo on your machine is with rustup.

Contributing

Since this project contains both Rust and Node.js code, you need to install the relevant dependencies. If you're only working on the JavaScript side, you only need to install those dependencies (and vice-versa).

# install node dependencies
npm install

# install cargo dependencies
cargo fetch

FAQ

Are cargo workspaces supported?

Not quite. Cargo's workspaces feature is a great tool when working on multiple binaries and libraries in a single project. If a cargo workspace is found in the entrypoint, however, vercel-rust will fail to build.

To get around this limitation, create build entries in your vercel.json file for each Cargo.toml that represents a Function within your workspace. In your .vercelignore, you'll want to add any binary or library project folders that aren't needed for your lambdas to speed up the build process like your Cargo.toml workspace.

It's also recommended to have a Cargo.lock alongside your lambda Cargo.toml files to speed up the build process. You can do this by running cargo check or a similar command within each project folder that contains a lambda.

If you have a compelling case for workspaces to be supported by vercel-rust which are too cumbersome with this workaround, please submit an issue! We're always looking for feedback.

Can I use musl/static linking?

Unfortunately, the AWS Lambda Runtime for Rust relies (tangentially) on proc_macro, which won't compile on musl targets. Without musl, all linking must be dynamic. If you have a crate that relies on system libraries like postgres or mysql, you can include those library files with the includeFiles config option and set the proper environment variables, config, etc. that you need to get the library to compile.

For more information, please see this issue.

rust-3's People

Contributors

mike-engel avatar dependabot[bot] avatar andybitz avatar leerob avatar de-luca avatar jacobmischka avatar icyjoseph avatar nico-bachner avatar styfle avatar connec avatar cdinu avatar ekadas avatar ngryman avatar srounce avatar revskill10 avatar sneakycrow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.