Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge Post Part 1 #65

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/CODEOWNERS

This file was deleted.

24 changes: 21 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
on: pull_request
name: Test
name: Test and Deploy
on: [pull_request, push]
jobs:
test:
strategy:
matrix:
rust-toolchain: [stable]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
environment: test
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down Expand Up @@ -53,3 +52,22 @@ jobs:
- name: build
run: cargo build
shell: bash
deploy:
if: |
startsWith(github.ref, 'refs/tags/v') && needs.test.result == 'success'
runs-on: ubuntu-latest
environment: deploy
needs: [test]
steps:
- uses: actions/checkout@v3
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0 # current Rust toolchain for Compute
target: wasm32-wasi # WebAssembly target
- name: Deploy
uses: fastly/compute-actions@v5
with:
verbose: true
env:
FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
**/*.rs.bk
/bin
/pkg
.DS_Store
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "static_site/hosfe.lt/themes/hermit-v2"]
path = static_site/hosfe.lt/themes/hermit-v2
url = https://github.com/1bl4z3r/hermit-V2
branch = main
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
[package]
name = "fastly-compute-project"
name = "hosfe_lt"
version = "0.1.0"
authors = []
edition = "2018"
# Remove this line if you want to be able to publish this crate as open source on crates.io.
# Otherwise, `publish = false` prevents an accidental `cargo publish` from revealing private source.
authors = ["Ricky <[email protected]>"]
edition = "2021"
publish = false

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Fastly
Copyright (c) 2024 Ricky Hosfelt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 3 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
# Default Starter Kit for Rust
# Source Code and Deployment Pipeline for Hosfe.lt

[![Deploy to Fastly](https://deploy.edgecompute.app/button)](https://deploy.edgecompute.app/deploy)
This is the source code for my website [Hosfe.lt](https://hosfe.lt) this is built using [Hugo](https://gohugo.io/) as a static site generate and deployed directly to Fastly's [Edge Compute](https://www.fastly.com/products/compute) platform.

Get to know the Fastly Compute environment with a basic starter that demonstrates routing, simple synthetic responses and code comments that cover common patterns.
Look for a future blog post soon on how this is done! :D (RH 20231227)

**For more details about this and other starter kits for Compute, see the [Fastly Developer Hub](https://developer.fastly.com/solutions/starters/)**.

## Features

- Allow only requests with particular HTTP methods
- Match request URL path and methods for routing
- Build synthetic responses at the edge

## Understanding the code

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://developer.fastly.com/learning/compute/rust/) and [VCL migration](https://developer.fastly.com/learning/compute/migrate/) guides.

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.

## Security issues

Please see [SECURITY.md](SECURITY.md) for guidance on reporting security-related issues.
9 changes: 0 additions & 9 deletions SECURITY.md

This file was deleted.

10 changes: 4 additions & 6 deletions fastly.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# This file describes a Fastly Compute package. To learn more visit:
# https://developer.fastly.com/reference/fastly-toml/

authors = ["<[email protected]>"]
description = "A basic starter kit that demonstrates routing, simple synthetic responses and overriding caching rules."
authors = ["Ricky <[email protected]>"]
description = "Using Fastly's compute at edge to serve my static site (built using fastly/compute-starter-kit-rust-default)"
language = "rust"
manifest_version = 3
name = "Default starter for Rust"
name = "Hosfe.lt on C@E"
service_id = "D4wTB3DpTsW6Zy60r9tPO0"
103 changes: 60 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
//! Default Compute template program.

use fastly::http::{header, Method, StatusCode};
use fastly::{mime, Error, Request, Response};

/// The entry point for your application.
///
/// This function is triggered when your service receives a client request. It could be used to
/// route based on the request properties (such as method or path), send the request to a backend,
/// make completely new requests, and/or generate synthetic responses.
///
/// If `main` returns an error, a 500 error response will be delivered to the client.

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
// Log service version
Expand All @@ -19,7 +9,6 @@ fn main(req: Request) -> Result<Response, Error> {
std::env::var("FASTLY_SERVICE_VERSION").unwrap_or_else(|_| String::new())
);

// Filter request methods...
match req.get_method() {
// Block requests with unexpected methods
&Method::POST | &Method::PUT | &Method::PATCH | &Method::DELETE => {
Expand All @@ -32,43 +21,71 @@ fn main(req: Request) -> Result<Response, Error> {
_ => (),
};

// Pattern match on the path...
match req.get_path() {
// If request is to the `/` path...
"/" => {
// Below are some common patterns for Compute services using Rust.
// Head to https://developer.fastly.com/learning/compute/rust/ to discover more.

// Create a new request.
// let mut bereq = Request::get("http://httpbin.org/headers")
// .with_header("X-Custom-Header", "Welcome to Compute!")
// .with_ttl(60);

// Add request headers.
// bereq.set_header(
// "X-Another-Custom-Header",
// "Recommended reading: https://developer.fastly.com/learning/compute",
// );

// Forward the request to a backend.
// let mut beresp = bereq.send("backend_name")?;

// Remove response headers.
// beresp.remove_header("X-Another-Custom-Header");

// Log to a Fastly endpoint.
// use std::io::Write;
// let mut endpoint = fastly::log::Endpoint::from_name("my_endpoint");
// writeln!(endpoint, "Hello from the edge!").unwrap();

// Send a default synthetic response.
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("welcome-to-compute.html")))
.with_body(include_str!("static_site/hosfe.lt/public/index.html")))
}

"/index.xml" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_XML)
.with_body(include_str!("static_site/hosfe.lt/public/index.xml")))
},
"/sitemap.xml" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_XML)
.with_body(include_str!("static_site/hosfe.lt/public/sitemap.xml")))
}
"/posts/" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/posts/index.html")))
},
"/posts/index.xml" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_XML)
.with_body(include_str!("static_site/hosfe.lt/public/posts/index.xml")))
}
"/about/" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/about/index.html")))
},
"/resume/" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/resume/index.html")))
},
"/css/style.min.dbbe08cb3b07bbce02de1a13a57d4221bb75487e75b0d1a5196a5353f7135921.css" => {
Ok(Response::from_status(StatusCode::OK)
.with_header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "https://www.hosfe.lt")
.with_content_type(mime::TEXT_CSS_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/css/style.min.dbbe08cb3b07bbce02de1a13a57d4221bb75487e75b0d1a5196a5353f7135921.css")))
}
"/js/bundle.min.038214de9d568246fadcfeb06c69349925de3209f332ec123861b6aa031d63c6.js" => {
Ok(Response::from_status(StatusCode::OK)
.with_header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "https://www.hosfe.lt")
.with_content_type(mime::APPLICATION_JAVASCRIPT_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/js/bundle.min.038214de9d568246fadcfeb06c69349925de3209f332ec123861b6aa031d63c6.js")))
},
"/js/link-share.min.24409a4f6e5537d70ffc55ec8f9192208d718678cb8638585342423020b37f39.js" => {
Ok(Response::from_status(StatusCode::OK)
.with_header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "https://www.hosfe.lt")
.with_content_type(mime::APPLICATION_JAVASCRIPT_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/js/link-share.min.24409a4f6e5537d70ffc55ec8f9192208d718678cb8638585342423020b37f39.js")))
},
// blog posts go after here
"/posts/story/" => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/posts/story/index.html")))
},
// Catch all other requests and return a 404.
_ => Ok(Response::from_status(StatusCode::NOT_FOUND)
.with_body_text_plain("The page you requested could not be found\n")),
_ => {
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("static_site/hosfe.lt/public/404.html")))
},
}
}
Empty file.
5 changes: 5 additions & 0 deletions src/static_site/hosfe.lt/archetypes/default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
+++
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
date = {{ .Date }}
draft = true
+++
9 changes: 9 additions & 0 deletions src/static_site/hosfe.lt/content/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+++
title = 'About'
date = 2023-12-27T14:41:52-05:00
+++


# Hello!

I am Ricky Hosfelt a career software engineer and now an Engineering Manager with a passion for Rust, Networking, and helping others. I hope to write more on all of the above plus some other random interests of mine (watches, hockey, cars, video games, etc) in the future. Thanks for stopping by :D
Loading