-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redirect): automatic redirects (#1429)
* 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
1 parent
68414b4
commit 2606adb
Showing
9 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
}; | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
edition = "2018" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |