Skip to content

Commit 78bddcc

Browse files
authored
Fix now dev and other changes (#19)
* Removed openssl installation The openssl installation was breaking `now dev` on any environment that isn't using `yum` as package manager. It seems sensible to remove it and instead projects that require it can add it (along with package management logic) into `build.sh`. * Require main function in entrypoints The replacement logic was breaking `now dev` as it was adding imports and the main function each time `now dev` was run. To mitigate this, the entrypoints now require the main function. * Debugging matches official builders Other builders are using @now/build-utils `debug` function to log debug messages. It is hidden behind NOW_BUILD_DEBUG environment variable, so I have also matched that behaviour for cargo verbosity. Without enabling, cargo is now less verbose, however still shows compilation errors. * Now only support version 3 of runtimes * Added support for path segments rustc does not support `[]` in crate names, hence cargo was failing to build path segments. Replacing brackets with underscores for naming fixes the issue. * Check if rust exists before trying to install This shaves off time as the installer no longer needs to download rustup and try to install. Instead it checks before downloading. * Cargo.toml handling is more flexible In order to allow usage of helper libraries across all folders Cargo allows specifying them via [lib]. This was removed on each build, previously which no longer happens. These changes however mean that Cargo is not as strictly controlled (now_lambda is not set to latest always, however the [[bin]] is overwritten). Cargo.toml is now also backed up and restored for builds, so that it does not get updated for the users source control management. * Updated tests for changes
1 parent 6609c67 commit 78bddcc

File tree

20 files changed

+1240
-194
lines changed

20 files changed

+1240
-194
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,28 @@ That's the simplest way to use this runtime!
3535

3636
The entrypoint, in this case every file that matches `api/**/*.rs`, is used to create a Serverless Function for you. Note that the `Cargo.toml` file must exist on the same level as the `.rs` files.
3737

38-
The requirements for this entrypoint is to expose a `handler` function and not to have a `main` function.
39-
4038
### Dependencies
4139

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

4442
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.
4543

46-
By default, `openssl` is installed by the Builder due to its common usage with Rust projects.
44+
#### Unlisted Utility Functions
45+
46+
Utility functions could be created as described in [Prevent Endpoint Listing](https://zeit.co/docs/v2/serverless-functions/introduction#prevent-endpoint-listing).
47+
To make use of them make sure to include them in the `Cargo.toml` under `[lib]`.
4748

4849
#### Example
4950

5051
This could be our `api/user.rs` file:
5152

5253
```rust
53-
use http::{StatusCode};
54-
use now_lambda::{error::NowError, IntoResponse, Request, Response};
54+
use util::print_foo;
55+
use now_lambda::{lambda, error::NowError, IntoResponse, Request, Response};
56+
use std::error::Error;
5557

5658
fn handler(_: Request) -> Result<impl IntoResponse, NowError> {
59+
print_foo();
5760
let response = Response::builder()
5861
.status(StatusCode::OK)
5962
.header("Content-Type", "text/plain")
@@ -62,6 +65,19 @@ fn handler(_: Request) -> Result<impl IntoResponse, NowError> {
6265

6366
Ok(response)
6467
}
68+
69+
// Start the runtime with the handler
70+
fn main() -> Result<(), Box<dyn Error>> {
71+
Ok(lambda!(handler))
72+
}
73+
```
74+
75+
Our helper utilities `api/_util.rs` file:
76+
77+
```rust
78+
pub fn print_foo() {
79+
println!("foo");
80+
}
6581
```
6682

6783
Our `api/Cargo.toml` could look like this:
@@ -75,7 +91,11 @@ edition = "2018"
7591

7692
[dependencies]
7793
http = "0.1"
78-
now_lambda = "0.1"
94+
now_lambda = "*"
95+
96+
[lib]
97+
name = "util"
98+
path = "_util.rs"
7999
```
80100

81101
Finally we need a `now.json` file to specify the runtime for `api/user.rs`:

package/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ This Builder supports installing dependencies defined in the `Cargo.toml` file.
113113

114114
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 entry point file.
115115

116-
By default, `openssl` is installed by the Builder due to its common usage with Rust projects.
117-
118116
## FAQ
119117

120118
### Are cargo workspaces supported?

0 commit comments

Comments
 (0)