Skip to content

Commit

Permalink
Remove unwanted session trackers
Browse files Browse the repository at this point in the history
Add GH action workflows for build and upload
  • Loading branch information
dormant-user committed Sep 21, 2024
1 parent 309a75c commit 8511ef1
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 15 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/none.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Validate Hyperlinks

on:
workflow_dispatch:
push:
branches:
- main
paths-ignore:
- '.github/**'
- 'src/**'
- 'Cargo.toml'

jobs:
none-shall-pass:
runs-on: thevickypedia-lite
steps:
- uses: thevickypedia/none-shall-pass@v5
with:
excludeHostnames: "crates"
257 changes: 257 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
name: Build, Test and Upload Artifact

on:
workflow_dispatch:
push:
branches:
- main
paths-ignore:
- '.github/**'

env:
CARGO_TERM_COLOR: always

jobs:
release:
runs-on: thevickypedia-lite
permissions:
contents: write
outputs:
release-id: ${{ steps.create-release.outputs.release_id }}
release-tag: ${{ steps.create-release.outputs.release_tag }}
release-flag: ${{ steps.set-release-flag.outputs.release_flag }}
pkg-name: ${{ steps.get-package-info.outputs.pkg_name }}
bin-name: ${{ steps.get-package-info.outputs.bin_name }}
steps:
- uses: actions/checkout@v4
- name: Get Package Name
id: get-package-info
run: |
pkg_name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
echo "Package Name: $pkg_name"
echo "pkg_name=$pkg_name" >> $GITHUB_ENV
echo "pkg_name=$pkg_name" >> "$GITHUB_OUTPUT"
bin_name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].targets[] | select(.kind[] == "bin" or .crate_types[] == "bin") | .name')
echo "Bin Name: $bin_name"
echo "bin_name=$bin_name" >> $GITHUB_ENV
echo "bin_name=$bin_name" >> "$GITHUB_OUTPUT"
shell: bash
- name: Set Release Flag # Release flag is set only for a push on main branch
id: set-release-flag
run: |
current_version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
# latest_version=$(curl -s https://crates.io/api/v1/crates/${{ env.pkg_name }} | jq -r '.versions[0].num')
versions=$(curl -s https://crates.io/api/v1/crates/${{ env.pkg_name }} | jq -r '.versions | map(.num)')
latest_version=$(echo $versions | jq -r '.[0]')
echo "Current Package Version: ${current_version}"
echo "Latest Package Version: $latest_version"
version_exists=false
for version in $(echo "$versions" | jq -r '.[]'); do
trimmed=$(echo "$version" | awk '{$1=$1};1')
if [ "$trimmed" == "$current_version" ]; then
version_exists=true
break
fi
done
if [ "$version_exists" = true ]; then
echo "Version $current_version exists in crates.io, setting release flag to 'false'"
echo "release=false" >> $GITHUB_ENV
echo "release_flag=false" >> "$GITHUB_OUTPUT"
else
echo "Version $current_version does not exist in crates.io, setting release flag to 'true'"
echo "release=true" >> $GITHUB_ENV
echo "release_flag=true" >> "$GITHUB_OUTPUT"
fi
echo "pkg_version=$current_version" >> $GITHUB_ENV
shell: bash
- name: Create New Release
if: env.release == 'true'
id: create-release
run: |
release_tag="v${{ env.pkg_version }}"
echo "release_tag=v${{ env.pkg_version }}" >> "$GITHUB_OUTPUT"
cargo_prerelease=("alpha" "beta" "rc")
prerelease=false
for cargo_pre in "${cargo_prerelease[@]}"; do
if [[ $pkg_version == *"$cargo_pre"* ]]; then
prerelease=true
break
fi
done
echo "Release Tag: $release_tag"
latest_tag=$(curl -s -L https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .tag_name)
commit_msg="$(git log -1 --pretty=%B | sed ':a;N;$!ba;s/\n/\\n/g')"
commit_msg+="\n**Full Changelog**: ${{ github.server_url }}/${{ github.repository }}/compare/$latest_tag...$release_tag"
release_data="{\"tag_name\":\"$release_tag\",\"name\":\"$release_tag\",\"body\":\"$commit_msg\",\"draft\":false,\"prerelease\":$prerelease}"
response=$(curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \
-d "$release_data" \
"https://api.github.com/repos/${{ github.repository }}/releases")
echo "Response: $response"
release_id=$(echo $response | jq -r .id)
if [ "$release_id" = "null" ] || [ -z "$release_id" ]; then
echo "Error: release_id is null. Exiting with code 1."
exit 1
fi
echo "Release ID: $release_id"
echo "release_id=$release_id" >> "$GITHUB_OUTPUT"
shell: bash

upload_assets:
needs: release
strategy:
matrix:
platform:
- release_for: Linux-x86_64
os: linux-amd64
bin: ${{ needs.release.outputs.bin-name }}
name: ${{ needs.release.outputs.pkg-name }}-Linux-x86_64.tar.gz

- release_for: Windows-x86_64
os: windows-amd64
bin: ${{ needs.release.outputs.bin-name }}.exe
name: ${{ needs.release.outputs.pkg-name }}-Windows-x86_64.zip

- release_for: macOS-x86_64
os: darwin-amd64
bin: ${{ needs.release.outputs.bin-name }}
name: ${{ needs.release.outputs.pkg-name }}-Darwin-x86_64.tar.gz

- release_for: macOS-arm64
os: darwin-arm64
bin: ${{ needs.release.outputs.bin-name }}
name: ${{ needs.release.outputs.pkg-name }}-Darwin-arm64.tar.gz

name: Upload asset for ${{ matrix.platform.release_for }}
if: needs.release.outputs.release-flag == 'true'
runs-on: ${{ matrix.platform.os }}
permissions:
contents: write

steps:
- name: Release ID Propagation
run: |
if [ -n "${{ needs.release.outputs.release-id }}" ]; then
echo "Release ID propagated: ${{ needs.release.outputs.release-id }}"
else
echo "Release ID propagation failed. Exiting.."
exit 1
fi
echo "start_time=$(date +%s)" >> "$GITHUB_ENV"
shell: bash

- name: Checkout Repo
uses: actions/checkout@v4

- name: Update Rust
# print it with style
run: |
printf '*%.0s' {1..60} && printf "\n"
echo "Existing rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n\n"
rustup update && printf "\n"
printf '*%.0s' {1..60} && printf "\n"
echo "Updated rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n"
shell: bash

- name: Install OpenSSL static for Windows
# https://github.com/sfackler/rust-openssl/issues/1086
if: startsWith(matrix.platform.os, 'windows')
run: |
if (-Not (Test-Path -Path \Tools)) {
mkdir \Tools
}
cd \Tools
if (-Not (Test-Path -Path .\vcpkg)) {
git clone https://github.com/Microsoft/vcpkg.git
}
cd vcpkg
echo ("vcpkg_dir=" + $pwd) >> $env:GITHUB_ENV
if (-Not (Test-Path -Path .\vcpkg.exe)) {
.\bootstrap-vcpkg.bat
}
.\vcpkg.exe install openssl:x64-windows-static
shell: pwsh

- name: Build
run: |
if [[ "${{ matrix.platform.os }}" =~ ^windows ]]; then
echo "Setting vcpkg env vars for OpenSSL in Windows"
export OPENSSL_DIR="${{ env.vcpkg_dir }}\installed\x64-windows-static"
export OPENSSL_STATIC="Yes"
export VCPKG_ROOT="${{ env.vcpkg_dir }}\installed\x64-windows-static"
fi
cargo build --release
shell: bash

- name: Run tests
run: |
if [[ "${{ matrix.platform.os }}" =~ ^windows ]]; then
echo "Setting vcpkg env vars for OpenSSL in Windows"
export OPENSSL_DIR="${{ env.vcpkg_dir }}\installed\x64-windows-static"
export OPENSSL_STATIC="Yes"
export VCPKG_ROOT="${{ env.vcpkg_dir }}\installed\x64-windows-static"
fi
cargo test --no-run
shell: bash

- name: Compress and Copy Artifact (Windows)
if: startsWith(matrix.platform.os, 'windows')
run: |
mkdir -p ${{ needs.release.outputs.pkg-name }}
cp target/release/${{ matrix.platform.bin }} ${{ needs.release.outputs.pkg-name }}/${{ matrix.platform.bin }}
Compress-Archive -DestinationPath ${{ matrix.platform.name }} -Path ${{ needs.release.outputs.pkg-name }}/
shell: pwsh

- name: Compress and Copy Artifact (macOS/Ubuntu)
if: "!startsWith(matrix.platform.os, 'windows')"
run: |
mkdir -p ${{ needs.release.outputs.pkg-name }}
cp target/release/${{ matrix.platform.bin }} ${{ needs.release.outputs.pkg-name }}/${{ matrix.platform.bin }}
tar -zcvf ${{ matrix.platform.name }} ${{ needs.release.outputs.pkg-name }}/
shell: bash

- name: Upload Asset to Release
run: |
curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
--data-binary @"${{ matrix.platform.name }}" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ needs.release.outputs.release-id }}/assets?name=${{ matrix.platform.name }}"
shell: bash

- name: Runtime Analyzer
run: |
start=${{ env.start_time }}
end=$(date +%s)
time_taken=$((end-start))
url="${{ github.server_url }}/${{ github.repository }}/releases/download/${{ needs.release.outputs.release-tag }}/${{ matrix.platform.name }}"
hyperlink="[${{ matrix.platform.release_for }}]($url)"
echo "🚀 Built for $hyperlink in $time_taken seconds" >> $GITHUB_STEP_SUMMARY
shell: bash

publish-crate:
needs:
- release
- upload_assets
if: needs.release.outputs.release-flag == 'true'
runs-on: thevickypedia-lite
steps:
- uses: actions/checkout@v4
- name: Update Rust
run: |
printf '*%.0s' {1..60} && printf "\n"
echo "Existing rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n\n"
rustup update && printf "\n"
printf '*%.0s' {1..60} && printf "\n"
echo "Updated rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n"
shell: bash
- name: Release Crate
run: |
cargo login ${{ secrets.CRATES_TOKEN }}
cargo publish --allow-dirty # Set allow-dirty since building will create a /target folder that will be uncommitted in git
shell: bash
5 changes: 1 addition & 4 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ pub fn build_info() -> Arc<MetaData> {
///
/// ## Fields
///
/// * `tracker` - Used to log connection and streaming information without redundancy.
/// * `mapping` - Used to store username and session token's payload as key value pairs.
///
/// ## See Also:
///
/// These fields are updated and used only for authenticated sessions.
pub struct Session {
pub tracker: Mutex<HashMap<String, String>>,
pub mapping: Mutex<HashMap<String, String>>,
}


/// Instantiates the `Session` struct with empty `HashMap` for both `tracker` and `mapping` fields.
/// Instantiates the `Session` struct with empty `HashMap` for `mapping` fields.
///
/// ## See Also
///
Expand All @@ -72,7 +70,6 @@ pub struct Session {
/// Returns the constructed `Arc` for the `Session` struct.
pub fn session_info() -> Arc<Session> {
Arc::new(Session {
tracker: Mutex::new(HashMap::new()),
mapping: Mutex::new(HashMap::new()),
})
}
Expand Down
10 changes: 2 additions & 8 deletions src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct DetailError {
/// * `request` - A reference to the Actix web `HttpRequest` object.
/// * `config` - Configuration data for the application.
/// * `fernet` - Fernet object to encrypt the auth payload that will be set as `session_token` cookie.
/// * `session` - Session struct that holds the `session_mapping` and `session_tracker` to handle sessions.
/// * `session` - Session struct that holds the `session_mapping` to handle sessions.
///
/// # Returns
///
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn login(request: HttpRequest,
///
/// * `request` - A reference to the Actix web `HttpRequest` object.
/// * `fernet` - Fernet object to encrypt the auth payload that will be set as `session_token` cookie.
/// * `session` - Session struct that holds the `session_mapping` and `session_tracker` to handle sessions.
/// * `session` - Session struct that holds the `session_mapping` to handle sessions.
/// * `metadata` - Struct containing metadata of the application.
/// * `config` - Configuration data for the application.
/// * `template` - Configuration container for the loaded templates.
Expand Down Expand Up @@ -107,12 +107,6 @@ pub async fn logout(request: HttpRequest,
}

if auth_response.ok {
let mut tracker = session.tracker.lock().unwrap();
if tracker.get(&host).is_some() {
tracker.remove(&host);
} else {
log::warn!("Session information for {} was not stored or no file was rendered", host);
}
rendered = logout_template.render(minijinja::context!(
version => metadata.pkg_version,
detail => "You have been logged out successfully."
Expand Down
2 changes: 1 addition & 1 deletion src/routes/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn health() -> HttpResponse {
/// # Arguments
///
/// * `request` - A reference to the Actix web `HttpRequest` object.
/// * `session` - Session struct that holds the `session_mapping` and `session_tracker` to handle sessions.
/// * `session` - Session struct that holds the `session_mapping` to handle sessions.
/// * `metadata` - Struct containing metadata of the application.
/// * `template` - Configuration container for the loaded templates.
///
Expand Down
4 changes: 2 additions & 2 deletions src/squire/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn extract_credentials(authorization: &HeaderValue) -> Result<Credentials, &'sta
///
/// * `request` - A reference to the Actix web `HttpRequest` object.
/// * `config` - Configuration data for the application.
/// * `session` - Session struct that holds the `session_mapping` and `session_tracker` to handle sessions.
/// * `session` - Session struct that holds the `session_mapping` to handle sessions.
///
/// # Returns
///
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn verify_login(
/// * `request` - A reference to the Actix web `HttpRequest` object.
/// * `config` - Configuration data for the application.
/// * `fernet` - Fernet object to encrypt the auth payload that will be set as `session_token` cookie.
/// * `session` - Session struct that holds the `session_mapping` and `session_tracker` to handle sessions.
/// * `session` - Session struct that holds the `session_mapping` to handle sessions.
///
/// # Returns
///
Expand Down

0 comments on commit 8511ef1

Please sign in to comment.