├── .cargo └── config.toml ├── .github ├── CODEOWNERS ├── dependabot.yml └── workflows │ └── test.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── SECURITY.md ├── fastly.toml ├── rust-toolchain.toml └── src ├── main.rs └── welcome-to-compute.html /.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "wasm32-wasip1" 3 | 4 | [term] 5 | color = "always" 6 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @fastly/developer-relations 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: cargo 4 | directory: "/" 5 | schedule: 6 | interval: weekly 7 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - 'Cargo.toml' 7 | - 'rust-toolchain.toml' 8 | - '.cargo/config.toml' 9 | - '.github/workflows/test.yml' 10 | - 'src/**' 11 | 12 | jobs: 13 | test: 14 | uses: fastly/devex-reusable-workflows/.github/workflows/compute-starter-kit-rust-v2.yml@main 15 | secrets: 16 | gh_token: ${{ secrets.GITHUB_TOKEN}} 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | /bin 4 | /pkg 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "fastly-compute-project" 3 | version = "0.1.0" 4 | authors = [] 5 | edition = "2021" 6 | # Remove this line if you want to be able to publish this crate on crates.io. 7 | # Otherwise, `publish = false` prevents an accidental `cargo publish` from revealing private source. 8 | publish = false 9 | 10 | [profile.release] 11 | debug = 1 12 | codegen-units = 1 13 | lto = "fat" 14 | 15 | [dependencies] 16 | fastly = "0.11.0" 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Fastly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Default Starter Kit for Rust 2 | 3 | [![Deploy to Fastly](https://deploy.edgecompute.app/button)](https://deploy.edgecompute.app/deploy) 4 | 5 | Get to know the Fastly Compute environment with a basic starter that demonstrates routing, simple synthetic responses and code comments that cover common patterns. 6 | 7 | **For more details about this and other starter kits for Compute, see the [Fastly Documentation Hub](https://www.fastly.com/documentation/solutions/starters/)**. 8 | 9 | ## Features 10 | 11 | - Allow only requests with particular HTTP methods 12 | - Match request URL path and methods for routing 13 | - Build synthetic responses at the edge 14 | 15 | ## Understanding the code 16 | 17 | This starter is intentionally lightweight, and requires no dependencies aside from the [`fastly`](https://docs.rs/fastly) crate. It will help you understand the basics of processing requests at the edge using Fastly. This starter includes implementations of common patterns explained in our [using Compute](https://www.fastly.com/documentation/guides/compute/rust/) and [VCL migration](https://www.fastly.com/documentation/guides/compute/migrate/) guides. 18 | 19 | The starter doesn't require the use of any backends. Once deployed, you will have a Fastly service running on Compute that can generate synthetic responses at the edge. 20 | 21 | ## Security issues 22 | 23 | Please see [SECURITY.md](SECURITY.md) for guidance on reporting security-related issues. 24 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Report a security issue 2 | 3 | The project team welcomes security reports and is committed to providing prompt attention to security issues. Security issues should be reported privately via [Fastly’s security issue reporting process](https://www.fastly.com/security/report-security-issue). 4 | 5 | ## Security advisories 6 | 7 | Remediation of security vulnerabilities is prioritized by the project team. The project team endeavors to coordinate remediation with third-party stakeholders, and is committed to transparency in the disclosure process. The team announces security issues via [GitHub](https://github.com/fastly/compute-starter-kit-rust-default/releases) as well as [RustSec](https://rustsec.org/advisories/) on a best-effort basis. 8 | 9 | Note that communications related to security issues in Fastly-maintained OSS as described here are distinct from [Fastly Security Advisories](https://www.fastly.com/security-advisories). 10 | -------------------------------------------------------------------------------- /fastly.toml: -------------------------------------------------------------------------------- 1 | # This file describes a Fastly Compute package. To learn more visit: 2 | # https://www.fastly.com/documentation/reference/compute/fastly-toml/ 3 | 4 | authors = [""] 5 | description = "Starter kit that demonstrates routing, simple synthetic responses and overriding caching rules." 6 | language = "rust" 7 | manifest_version = 3 8 | name = "Default starter for Rust" 9 | 10 | [scripts] 11 | build = "cargo build --profile release" 12 | -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "stable" 3 | targets = [ "wasm32-wasip1" ] 4 | profile = "default" 5 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | //! Default Compute template program. 2 | 3 | use fastly::http::{header, Method, StatusCode}; 4 | use fastly::{mime, Error, Request, Response}; 5 | 6 | /// The entry point for your application. 7 | /// 8 | /// This function is triggered when your service receives a client request. It could be used to 9 | /// route based on the request properties (such as method or path), send the request to a backend, 10 | /// make completely new requests, and/or generate synthetic responses. 11 | /// 12 | /// If `main` returns an error, a 500 error response will be delivered to the client. 13 | #[fastly::main] 14 | fn main(req: Request) -> Result { 15 | // Log service version 16 | println!( 17 | "FASTLY_SERVICE_VERSION: {}", 18 | std::env::var("FASTLY_SERVICE_VERSION").unwrap_or_else(|_| String::new()) 19 | ); 20 | 21 | // Filter request methods... 22 | match req.get_method() { 23 | // Block requests with unexpected methods 24 | &Method::POST | &Method::PUT | &Method::PATCH | &Method::DELETE => { 25 | return Ok(Response::from_status(StatusCode::METHOD_NOT_ALLOWED) 26 | .with_header(header::ALLOW, "GET, HEAD, PURGE") 27 | .with_body_text_plain("This method is not allowed\n")) 28 | } 29 | 30 | // Let any other requests through 31 | _ => (), 32 | }; 33 | 34 | // Pattern match on the path... 35 | match req.get_path() { 36 | // If request is to the `/` path... 37 | "/" => { 38 | // Below are some common patterns for Compute services using Rust. 39 | // Head to https://developer.fastly.com/learning/compute/rust/ to discover more. 40 | 41 | // Create a new request. 42 | // let mut bereq = Request::get("http://httpbin.org/headers") 43 | // .with_header("X-Custom-Header", "Welcome to Compute!") 44 | // .with_ttl(60); 45 | 46 | // Add request headers. 47 | // bereq.set_header( 48 | // "X-Another-Custom-Header", 49 | // "Recommended reading: https://developer.fastly.com/learning/compute", 50 | // ); 51 | 52 | // Forward the request to a backend. 53 | // let mut beresp = bereq.send("backend_name")?; 54 | 55 | // Remove response headers. 56 | // beresp.remove_header("X-Another-Custom-Header"); 57 | 58 | // Log to a Fastly endpoint. 59 | // use std::io::Write; 60 | // let mut endpoint = fastly::log::Endpoint::from_name("my_endpoint"); 61 | // writeln!(endpoint, "Hello from the edge!").unwrap(); 62 | 63 | // Send a default synthetic response. 64 | Ok(Response::from_status(StatusCode::OK) 65 | .with_content_type(mime::TEXT_HTML_UTF_8) 66 | .with_body(include_str!("welcome-to-compute.html"))) 67 | } 68 | 69 | // Catch all other requests and return a 404. 70 | _ => Ok(Response::from_status(StatusCode::NOT_FOUND) 71 | .with_body_text_plain("The page you requested could not be found\n")), 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/welcome-to-compute.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | Welcome to Fastly Compute 11 | 16 | 17 | 18 | 29 | 30 | 31 | --------------------------------------------------------------------------------