From 6fd10a05249719879c42f119ff4773ff2720583f Mon Sep 17 00:00:00 2001 From: augustuswm Date: Tue, 14 May 2024 11:27:00 -0500 Subject: [PATCH] Remove redirect. Updating cargo-dist --- .github/workflows/release.yml | 158 ---------------------------------- Cargo.lock | 19 ---- Cargo.toml | 6 +- rfd-redirect/Cargo.toml | 24 ------ rfd-redirect/src/config.rs | 71 --------------- rfd-redirect/src/context.rs | 16 ---- rfd-redirect/src/endpoints.rs | 100 --------------------- rfd-redirect/src/main.rs | 51 ----------- rfd-redirect/src/server.rs | 46 ---------- 9 files changed, 3 insertions(+), 488 deletions(-) delete mode 100644 .github/workflows/release.yml delete mode 100644 rfd-redirect/Cargo.toml delete mode 100644 rfd-redirect/src/config.rs delete mode 100644 rfd-redirect/src/context.rs delete mode 100644 rfd-redirect/src/endpoints.rs delete mode 100644 rfd-redirect/src/main.rs delete mode 100644 rfd-redirect/src/server.rs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 5f1f746b..00000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright 2022-2023, axodotdev -# SPDX-License-Identifier: MIT or Apache-2.0 -# -# CI that: -# -# * checks for a Git Tag that looks like a release -# * builds artifacts with cargo-dist (archives, installers, hashes) -# * uploads those artifacts to temporary workflow zip -# * on success, uploads the artifacts to a Github Release™ -# -# Note that the Github Release™ will be created with a generated -# title/body based on your changelogs. -name: Release - -permissions: - contents: write - -# This task will run whenever you push a git tag that looks like a version -# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc. -# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where -# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION -# must be a Cargo-style SemVer Version (must have at least major.minor.patch). -# -# If PACKAGE_NAME is specified, then the release will be for that -# package (erroring out if it doesn't have the given version or isn't cargo-dist-able). -# -# If PACKAGE_NAME isn't specified, then the release will be for all -# (cargo-dist-able) packages in the workspace with that version (this mode is -# intended for workspaces with only one dist-able package, or with all dist-able -# packages versioned/released in lockstep). -# -# If you push multiple tags at once, separate instances of this workflow will -# spin up, creating an independent Github Release™ for each one. However Github -# will hard limit this to 3 tags per commit, as it will assume more tags is a -# mistake. -# -# If there's a prerelease-style suffix to the version, then the Github Release™ -# will be marked as a prerelease. -on: - push: - tags: - - '**[0-9]+.[0-9]+.[0-9]+*' - pull_request: - -jobs: - # Run 'cargo dist plan' to determine what tasks we need to do - plan: - runs-on: ubuntu-latest - outputs: - val: ${{ steps.plan.outputs.manifest }} - tag: ${{ !github.event.pull_request && github.ref_name || '' }} - tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} - publishing: ${{ !github.event.pull_request }} - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - RUSTFLAGS: "--cfg tracing_unstable" - steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - with: - submodules: recursive - - name: Install cargo-dist - run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.4.0-prerelease.2/cargo-dist-installer.sh | sh" - - id: plan - run: | - cargo dist plan ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} -v trace --output-format=json > dist-manifest.json - echo "cargo dist plan ran successfully" - cat dist-manifest.json - echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT" - - name: "Upload dist-manifest.json" - uses: actions/upload-artifact@1746f4ab65b179e0ea60a494b83293b640dd5bba # v4 - with: - name: artifacts - path: dist-manifest.json - - # Build and packages all the platform-specific things - upload-local-artifacts: - # Let the initial task tell us to not run (currently very blunt) - needs: plan - if: ${{ fromJson(needs.plan.outputs.val).releases != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }} - strategy: - fail-fast: false - # Target platforms/runners are computed by cargo-dist in create-release. - # Each member of the matrix has the following arguments: - # - # - runner: the github runner - # - dist-args: cli flags to pass to cargo dist - # - install-dist: expression to run to install cargo-dist on the runner - # - # Typically there will be: - # - 1 "global" task that builds universal installers - # - N "local" tasks that build each platform's binaries and platform-specific installers - matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }} - runs-on: ${{ matrix.runner }} - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - RUSTFLAGS: "--cfg tracing_unstable" - steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - with: - submodules: recursive - - uses: swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2 - - name: Install cargo-dist - run: ${{ matrix.install_dist }} - - name: Build artifacts - run: | - # Actually do builds and make zips and whatnot - cargo dist build ${{ needs.plan.outputs.tag-flag }} -v trace --output-format=json ${{ matrix.dist_args }} > dist-manifest.json - echo "cargo dist ran successfully" - - id: cargo-dist - name: Post-build - # We force bash here just because github makes it really hard to get values up - # to "real" actions without writing to env-vars, and writing to env-vars has - # inconsistent syntax between shell and powershell. - shell: bash - run: | - # Parse out what we just built and upload it to the Github Release™ - echo "paths<> "$GITHUB_OUTPUT" - jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json >> "$GITHUB_OUTPUT" - echo "EOF" >> "$GITHUB_OUTPUT" - - name: "Upload artifacts" - uses: actions/upload-artifact@1746f4ab65b179e0ea60a494b83293b640dd5bba # v4 - with: - name: artifacts - path: ${{ steps.cargo-dist.outputs.paths }} - - should-publish: - needs: - - plan - - upload-local-artifacts - if: ${{ needs.plan.outputs.publishing == 'true' }} - runs-on: ubuntu-latest - steps: - - name: print tag - run: echo "ok we're publishing!" - - # Create a Github Release with all the results once everything is done, - publish-release: - needs: [plan, should-publish] - runs-on: ubuntu-latest - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - with: - submodules: recursive - - name: "Download artifacts" - uses: actions/download-artifact@8caf195ad4b1dee92908e23f56eeb0696f1dd42d # v4 - with: - name: artifacts - path: artifacts - - name: Create Release - uses: ncipollo/release-action@2c591bcc8ecdcd2db72b97d6147f871fcd833ba5 # v1 - with: - tag: ${{ needs.plan.outputs.tag }} - name: ${{ fromJson(needs.plan.outputs.val).announcement_title }} - body: ${{ fromJson(needs.plan.outputs.val).announcement_github_body }} - prerelease: ${{ fromJson(needs.plan.outputs.val).announcement_is_prerelease }} - artifacts: "artifacts/*" diff --git a/Cargo.lock b/Cargo.lock index d838a412..ec93fb82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2696,25 +2696,6 @@ dependencies = [ "yup-oauth2", ] -[[package]] -name = "rfd-redirect" -version = "0.8.0" -dependencies = [ - "config", - "dropshot", - "regex", - "rfd-data", - "schemars", - "serde", - "slog", - "slog-async", - "thiserror", - "tokio", - "tracing", - "tracing-slog", - "tracing-subscriber", -] - [[package]] name = "rfd-sdk" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 333ad2a9..700ca9c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ members = [ "rfd-github", "rfd-model", "rfd-processor", - "rfd-redirect", "rfd-sdk", "trace-request", "w-api-permissions", @@ -93,15 +92,16 @@ yup-oauth2 = "8.3.2" # Config for 'cargo dist' [workspace.metadata.dist] # The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax) -cargo-dist-version = "0.4.0-prerelease.2" +cargo-dist-version = "0.12.2" # CI backends to support ci = ["github"] # The installers to generate for each app installers = [] # Target platforms to build apps for (Rust target-triple syntax) -targets = ["x86_64-unknown-linux-gnu"] +targets = ["aarch64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"] # Publish jobs to run in CI pr-run-mode = "plan" +# Skip checking whether the specified configuration files are up to date allow-dirty = ["ci"] # The profile that 'cargo dist' will build with diff --git a/rfd-redirect/Cargo.toml b/rfd-redirect/Cargo.toml deleted file mode 100644 index e6f1b78c..00000000 --- a/rfd-redirect/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "rfd-redirect" -version = "0.8.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -config = { workspace = true } -dropshot = { workspace = true } -regex = { workspace = true } -rfd-data = { path = "../rfd-data" } -schemars = { workspace = true } -serde = { workspace = true } -slog = { workspace = true } -slog-async = { workspace = true } -tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } -thiserror = { workspace = true } -tracing = { workspace = true } -tracing-slog = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter", "fmt", "json"] } - -[package.metadata.dist] -targets = ["x86_64-unknown-linux-gnu"] diff --git a/rfd-redirect/src/config.rs b/rfd-redirect/src/config.rs deleted file mode 100644 index 06abee7d..00000000 --- a/rfd-redirect/src/config.rs +++ /dev/null @@ -1,71 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -use config::{Config, ConfigError, Environment, File}; -use serde::{ - de::{self, Visitor}, - Deserialize, Deserializer, -}; -use thiserror::Error; - -#[derive(Debug, Error)] -pub enum AppConfigError { - #[error("Encountered invalid log format.")] - InvalidLogFormatVariant, -} - -#[derive(Debug, Deserialize)] -pub struct AppConfig { - pub log_format: ServerLogFormat, - pub server_port: u16, - pub host_regex: String, - pub github_template: String, -} - -#[derive(Debug)] -pub enum ServerLogFormat { - Json, - Pretty, -} - -impl<'de> Deserialize<'de> for ServerLogFormat { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - struct ExternalId; - - impl<'de> Visitor<'de> for ExternalId { - type Value = ServerLogFormat; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("string") - } - - fn visit_str(self, value: &str) -> Result - where - E: de::Error, - { - match value { - "json" => Ok(Self::Value::Json), - "pretty" => Ok(Self::Value::Pretty), - _ => Err(de::Error::custom(AppConfigError::InvalidLogFormatVariant)), - } - } - } - - deserializer.deserialize_any(ExternalId) - } -} - -impl AppConfig { - pub fn new() -> Result { - let config = Config::builder() - .add_source(File::with_name("config.toml")) - .add_source(Environment::default()) - .build()?; - - config.try_deserialize() - } -} diff --git a/rfd-redirect/src/context.rs b/rfd-redirect/src/context.rs deleted file mode 100644 index 4209aa98..00000000 --- a/rfd-redirect/src/context.rs +++ /dev/null @@ -1,16 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -use regex::Regex; - -pub struct Context { - pub host_regex: Regex, - pub github_template: String, -} - -impl Context { - pub fn is_rfd_number_valid(&self, rfd_number: u16) -> bool { - 0 < rfd_number - } -} diff --git a/rfd-redirect/src/endpoints.rs b/rfd-redirect/src/endpoints.rs deleted file mode 100644 index bbe2390b..00000000 --- a/rfd-redirect/src/endpoints.rs +++ /dev/null @@ -1,100 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -use dropshot::{ - endpoint, http_response_temporary_redirect, HttpError, HttpResponseTemporaryRedirect, Path, - RequestContext, -}; -use rfd_data::RfdNumber; -use schemars::JsonSchema; -use serde::Deserialize; -use thiserror::Error; - -use crate::context::Context; - -#[derive(Debug, Deserialize, JsonSchema)] -pub struct RfdPathParams { - number: String, -} - -#[endpoint { - method = GET, - path = "/{number}", -}] -pub async fn redirect_path( - rqctx: RequestContext, - path: Path, -) -> Result { - tracing::trace!("Attempt path redirect"); - - let rfd_number = path - .into_inner() - .number - .parse::() - .ok() - .filter(|rfd_number| rqctx.context().is_rfd_number_valid(*rfd_number)); - - if let Some(rfd_number) = rfd_number { - let location = rqctx.context().github_template.replace( - "{rfd_number}", - &RfdNumber::from(rfd_number as i32).as_number_string(), - ); - tracing::trace!(?rfd_number, ?location, "Redirect to RFD via path"); - http_response_temporary_redirect(location) - } else { - Err(HttpError::for_not_found( - None, - format!("Invalid RFD number requested"), - )) - } -} - -#[endpoint { - method = GET, - path = "/", -}] -pub async fn redirect_host( - rqctx: RequestContext, -) -> Result { - tracing::trace!("Attempt host redirect"); - - if let Ok(host) = get_host_header(&rqctx) { - if let Some(captures) = rqctx.context().host_regex.captures(&host) { - if let Some(rfd_number) = captures - .get(1) - .and_then(|rfd_number| rfd_number.as_str().parse::().ok()) - { - if rqctx.context().is_rfd_number_valid(rfd_number) { - return http_response_temporary_redirect( - rqctx.context().github_template.replace( - "{rfd_number}", - &RfdNumber::from(rfd_number as i32).as_number_string(), - ), - ); - } - } - } - - tracing::trace!(?host, "Dropping invalid host"); - } - - Err(HttpError::for_not_found( - None, - "Invalid host name received".to_string(), - )) -} - -#[derive(Debug, Error)] -enum HostError { - #[error("Failed to extract a host header from the request")] - FailedToExtractHost, -} - -fn get_host_header(rqctx: &RequestContext) -> Result { - let header = rqctx.request.headers().get("Host"); - header - .and_then(|header| header.to_str().ok()) - .map(|s| s.to_string()) - .ok_or(HostError::FailedToExtractHost) -} diff --git a/rfd-redirect/src/main.rs b/rfd-redirect/src/main.rs deleted file mode 100644 index b28be4c7..00000000 --- a/rfd-redirect/src/main.rs +++ /dev/null @@ -1,51 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -use std::{ - error::Error, - net::{SocketAddr, SocketAddrV4}, -}; - -use context::Context; -use regex::Regex; -use tracing_subscriber::EnvFilter; - -use crate::{ - config::{AppConfig, ServerLogFormat}, - server::{server, ServerConfig}, -}; - -mod config; -mod context; -mod endpoints; -mod server; - -#[tokio::main] -async fn main() -> Result<(), Box> { - let config = AppConfig::new()?; - - let subscriber = tracing_subscriber::fmt() - .with_file(false) - .with_line_number(false) - .with_env_filter(EnvFilter::from_default_env()); - - match config.log_format { - ServerLogFormat::Json => subscriber.json().init(), - ServerLogFormat::Pretty => subscriber.pretty().init(), - }; - - let config = ServerConfig { - context: Context { - host_regex: Regex::new(&config.host_regex)?, - github_template: config.github_template.to_string(), - }, - server_address: SocketAddr::V4(SocketAddrV4::new("0.0.0.0".parse()?, config.server_port)), - }; - - let server = server(config)?.start(); - - server.await?; - - Ok(()) -} diff --git a/rfd-redirect/src/server.rs b/rfd-redirect/src/server.rs deleted file mode 100644 index 809d8a1e..00000000 --- a/rfd-redirect/src/server.rs +++ /dev/null @@ -1,46 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -use dropshot::{ApiDescription, ConfigDropshot, EndpointTagPolicy, HttpServerStarter, TagConfig}; -use slog::Drain; -use std::{error::Error, net::SocketAddr}; -use tracing_slog::TracingSlogDrain; - -use crate::{ - context::Context, - endpoints::{redirect_host, redirect_path}, -}; - -pub struct ServerConfig { - pub context: Context, - pub server_address: SocketAddr, -} - -pub fn server( - config: ServerConfig, -) -> Result, Box> { - let mut config_dropshot = ConfigDropshot::default(); - config_dropshot.bind_address = config.server_address; - - // Construct a shim to pipe dropshot logs into the global tracing logger - let dropshot_logger = { - let level_drain = slog::LevelFilter(TracingSlogDrain, slog::Level::Debug).fuse(); - let async_drain = slog_async::Async::new(level_drain).build().fuse(); - slog::Logger::root(async_drain, slog::o!()) - }; - - let mut api = ApiDescription::new().tag_config(TagConfig { - allow_other_tags: false, - endpoint_tag_policy: EndpointTagPolicy::Any, - tag_definitions: vec![].into_iter().collect(), - }); - - // Handle {rfd_number}.hostname redirects - api.register(redirect_host).unwrap(); - - // Handle hostname/{rfd_number} redirects - api.register(redirect_path).unwrap(); - - HttpServerStarter::new(&config_dropshot, api, config.context, &dropshot_logger) -}