Skip to content

Commit

Permalink
feat(redirect): automatic redirects (#1429)
Browse files Browse the repository at this point in the history
* feat(redirect): basic project setup

* feat(redirect): simple redirect service

* feat(git): update .gitignore

* feat(redirect): rewrote redirect to axum

* chore(redirect): simplification and formatting

* feat(redirect): added Dockerfile

* feat(actions): deploy redirect action
  • Loading branch information
lindtvedtsebastian authored Mar 13, 2024
1 parent 68414b4 commit 2606adb
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/deploy_redirect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deploy redirect

on:
workflow_dispatch:

env:
PROJECT_ID: ${{ secrets.BETA_PROD_ID }}
RUN_REGION: ${{ secrets.BETA_RUN_REGION }}

jobs:
deploy-prod:
runs-on: ubuntu-latest
defaults:
run:
working-directory: redirect
steps:
- uses: actions/checkout@v3
- uses: "google-github-actions/auth@v1"
with:
credentials_json: "${{ secrets.DEPLOY_BETA_PROD }}"

- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v1"
with:
version: ">= 363.0.0"
project_id: "${{ secrets.BETA_PROD_ID }}"

- name: "Build container image"
run: "gcloud builds submit --tag eu.gcr.io/$PROJECT_ID/redirect:$GITHUB_SHA"
- name: "Deploy"
run: >
gcloud run deploy redirect --image eu.gcr.io/$PROJECT_ID/redirect:$GITHUB_SHA
--region $RUN_REGION --service-account="[email protected]" --allow-unauthenticated
27 changes: 27 additions & 0 deletions flake.lock

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

36 changes: 36 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
description = "tavla dev env";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};

outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{

devShells.${system}.default =
pkgs.mkShell
{
buildInputs = with pkgs; [
rustc
cargo
rust-analyzer
clippy
rustfmt

# Tavla
yarn
openjdk19
nodejs
nodePackages.prettier
nodePackages.eslint
nodePackages.typescript-language-server
];
};

};
}
2 changes: 2 additions & 0 deletions redirect/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Dockerfile
14 changes: 14 additions & 0 deletions redirect/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
12 changes: 12 additions & 0 deletions redirect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "redirect"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = {version = "1.36.0", features = ["full"] }
hyper = {version = "1.1.0", features = ["full"]}
axum = "0.7.4"
tower = {version = "0.4.13", features = ['make']}
14 changes: 14 additions & 0 deletions redirect/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM messense/rust-musl-cross:x86_64-musl AS builder

WORKDIR /redirect

COPY . .

RUN cargo build --release --target x86_64-unknown-linux-musl

FROM alpine:3.18 AS runner

COPY --from=builder /redirect/target/x86_64-unknown-linux-musl/release/redirect /redirect

EXPOSE 3000
CMD ["/redirect"]
1 change: 1 addition & 0 deletions redirect/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2018"
21 changes: 21 additions & 0 deletions redirect/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use axum::{extract::Request, handler::HandlerWithoutStateExt, response::Redirect};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
let host = std::env::var("HOST").unwrap_or("0.0.0.0".to_string());
let port = std::env::var("PORT").unwrap_or("3000".to_string());

let listener = TcpListener::bind(format!("{}:{}", host, port))
.await
.unwrap();

axum::serve(listener, tower::make::Shared::new(redirect.into_service()))
.await
.unwrap();
}

async fn redirect(request: Request) -> Redirect {
let target = std::env::var("TARGET").unwrap_or("https://tavla.entur.no".to_string());
Redirect::temporary(format!("{}{}", target, request.uri()).as_str())
}

0 comments on commit 2606adb

Please sign in to comment.