mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Compare commits
9 Commits
0fea67c7c3
...
release/v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
fceffb900d
|
|||
|
49ce0005dc
|
|||
|
4c738085f2
|
|||
|
20602eb863
|
|||
|
e04d462223
|
|||
|
22af02464d
|
|||
|
9a517519db
|
|||
|
65376e75f7
|
|||
|
bef6163c1b
|
169
.github/workflows/release.yml
vendored
Normal file
169
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
name: release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags: [ "release/v*" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
BINARY_NAME: schedule-parser-rusted
|
||||||
|
|
||||||
|
TEST_DB: ${{ secrets.TEST_DATABASE_URL }}
|
||||||
|
|
||||||
|
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||||
|
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
|
||||||
|
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
|
||||||
|
|
||||||
|
DOCKER_IMAGE_NAME: ${{ github.repository }}
|
||||||
|
|
||||||
|
DOCKER_REGISTRY_HOST: registry.n08i40k.ru
|
||||||
|
DOCKER_REGISTRY_USERNAME: ${{ github.repository_owner }}
|
||||||
|
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
touch .env.test
|
||||||
|
cargo test --verbose
|
||||||
|
env:
|
||||||
|
DATABASE_URL: ${{ env.TEST_DB }}
|
||||||
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
|
VKID_CLIENT_ID: 0
|
||||||
|
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||||
|
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: test
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: cargo build --release --verbose
|
||||||
|
|
||||||
|
- name: Extract debug symbols
|
||||||
|
run: |
|
||||||
|
objcopy --only-keep-debug target/release/${{ env.BINARY_NAME }}{,.d}
|
||||||
|
objcopy --strip-debug --strip-unneeded target/release/${{ env.BINARY_NAME }}
|
||||||
|
objcopy --add-gnu-debuglink target/release/${{ env.BINARY_NAME }}{.d,}
|
||||||
|
|
||||||
|
- name: Setup sentry-cli
|
||||||
|
uses: matbour/setup-sentry-cli@v2.0.0
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
token: ${{ env.SENTRY_AUTH_TOKEN }}
|
||||||
|
organization: ${{ env.SENTRY_ORG }}
|
||||||
|
project: ${{ env.SENTRY_PROJECT }}
|
||||||
|
|
||||||
|
- name: Upload debug symbols to Sentry
|
||||||
|
run: |
|
||||||
|
sentry-cli debug-files upload --include-sources .
|
||||||
|
|
||||||
|
- name: Upload build binary artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-binary
|
||||||
|
path: target/release/${{ env.BINARY_NAME }}
|
||||||
|
|
||||||
|
- name: Upload build debug symbols artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-symbols
|
||||||
|
path: target/release/${{ env.BINARY_NAME }}.d
|
||||||
|
|
||||||
|
docker:
|
||||||
|
name: Build & Push Docker Image
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: build
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Download build artifacts
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-binary
|
||||||
|
|
||||||
|
- name: Setup Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3.10.0
|
||||||
|
|
||||||
|
- name: Login to Registry
|
||||||
|
uses: docker/login-action@v3.4.0
|
||||||
|
with:
|
||||||
|
registry: ${{ env.DOCKER_REGISTRY_HOST }}
|
||||||
|
username: ${{ env.DOCKER_REGISTRY_USERNAME }}
|
||||||
|
password: ${{ env.DOCKER_REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Extract Docker metadata
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5.7.0
|
||||||
|
with:
|
||||||
|
images: ${{ env.DOCKER_REGISTRY_HOST }}/${{ env.DOCKER_IMAGE_NAME }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
id: build-and-push
|
||||||
|
uses: docker/build-push-action@v6.15.0
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
build-args: |
|
||||||
|
"BINARY_NAME=${{ env.BINARY_NAME }}"
|
||||||
|
release:
|
||||||
|
name: Create GitHub Release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs:
|
||||||
|
- build
|
||||||
|
- docker
|
||||||
|
# noinspection GrazieInspection,SpellCheckingInspection
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Generate changelog
|
||||||
|
run: |
|
||||||
|
LAST_TAG=$(git describe --tags --abbrev=0 HEAD^)
|
||||||
|
echo "## Коммиты с прошлого релиза $LAST_TAG" > CHANGELOG.md
|
||||||
|
git log $LAST_TAG..HEAD --oneline >> CHANGELOG.md
|
||||||
|
|
||||||
|
- name: Download build artifacts
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: release-*
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
id: create_release
|
||||||
|
uses: ncipollo/release-action@v1.16.0
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
artifacts: "${{ env.BINARY_NAME }},${{ env.BINARY_NAME }}.d"
|
||||||
|
bodyFile: CHANGELOG.md
|
||||||
6
.github/workflows/test.yml
vendored
6
.github/workflows/test.yml
vendored
@@ -1,10 +1,9 @@
|
|||||||
name: Tests
|
name: cargo test
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "master" ]
|
branches: [ "master" ]
|
||||||
pull_request:
|
tags-ignore: [ "release/v*" ]
|
||||||
branches: [ "master" ]
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
@@ -30,3 +29,4 @@ jobs:
|
|||||||
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
VKID_CLIENT_ID: 0
|
VKID_CLIENT_ID: 0
|
||||||
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||||
|
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
|
||||||
9
.idea/sqldialects.xml
generated
9
.idea/sqldialects.xml
generated
@@ -1,9 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="SqlDialectMappings">
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-211822_create_user_role/down.sql" dialect="PostgreSQL" />
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-212111_create_users/up.sql" dialect="PostgreSQL" />
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-212723_create_fcm/down.sql" dialect="PostgreSQL" />
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-212723_create_fcm/up.sql" dialect="PostgreSQL" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -2152,9 +2152,9 @@ checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl"
|
name = "openssl"
|
||||||
version = "0.10.72"
|
version = "0.10.71"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fedfea7d58a1f73118430a55da6a286e7b044961736ce96a16a17068ea25e5da"
|
checksum = "5e14130c6a98cd258fdcb0fb6d744152343ff729cbfcb28c656a9d12b999fbcd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
@@ -2184,9 +2184,9 @@ checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl-sys"
|
name = "openssl-sys"
|
||||||
version = "0.9.107"
|
version = "0.9.106"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8288979acd84749c744a9014b4382d42b8f7b2592847b5afb2ed29e5d16ede07"
|
checksum = "8bb61ea9811cc39e3c2069f40b8b8e2e70d8569b361f879786cc7ed48b777cdd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"libc",
|
"libc",
|
||||||
@@ -2876,7 +2876,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "schedule-parser-rusted"
|
name = "schedule-parser-rusted"
|
||||||
version = "0.8.0"
|
version = "1.0.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-macros 0.1.0",
|
"actix-macros 0.1.0",
|
||||||
"actix-test",
|
"actix-test",
|
||||||
|
|||||||
@@ -3,10 +3,13 @@ members = ["actix-macros", "actix-test"]
|
|||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "schedule-parser-rusted"
|
name = "schedule-parser-rusted"
|
||||||
version = "0.8.0"
|
version = "1.0.3"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.10.2"
|
actix-web = "4.10.2"
|
||||||
actix-macros = { path = "actix-macros" }
|
actix-macros = { path = "actix-macros" }
|
||||||
|
|||||||
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
FROM debian:stable-slim
|
||||||
|
LABEL authors="n08i40k"
|
||||||
|
|
||||||
|
ARG BINARY_NAME
|
||||||
|
|
||||||
|
WORKDIR /app/
|
||||||
|
|
||||||
|
RUN apt update && \
|
||||||
|
apt install -y libpq5 ca-certificates openssl
|
||||||
|
|
||||||
|
COPY ./${BINARY_NAME} /bin/main
|
||||||
|
RUN chmod +x /bin/main
|
||||||
|
|
||||||
|
ENTRYPOINT ["main"]
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
use crate::utility::jwt::DEFAULT_ALGORITHM;
|
|
||||||
use jsonwebtoken::errors::ErrorKind;
|
use jsonwebtoken::errors::ErrorKind;
|
||||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
|
||||||
use std::sync::LazyLock;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
struct TokenData {
|
struct TokenData {
|
||||||
@@ -17,7 +14,7 @@ struct TokenData {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: String,
|
sub: i32,
|
||||||
iis: String,
|
iis: String,
|
||||||
jti: i32,
|
jti: i32,
|
||||||
app: i32,
|
app: i32,
|
||||||
@@ -52,17 +49,10 @@ const VK_PUBLIC_KEY: &str = concat!(
|
|||||||
"-----END PUBLIC KEY-----"
|
"-----END PUBLIC KEY-----"
|
||||||
);
|
);
|
||||||
|
|
||||||
static VK_ID_CLIENT_ID: LazyLock<i32> = LazyLock::new(|| {
|
pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
|
||||||
env::var("VK_ID_CLIENT_ID")
|
|
||||||
.expect("VK_ID_CLIENT_ID must be set")
|
|
||||||
.parse::<i32>()
|
|
||||||
.expect("VK_ID_CLIENT_ID must be i32")
|
|
||||||
});
|
|
||||||
|
|
||||||
pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
|
|
||||||
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
||||||
|
|
||||||
match decode::<Claims>(&token_str, &dkey, &Validation::new(DEFAULT_ALGORITHM)) {
|
match decode::<Claims>(&token_str, &dkey, &Validation::new(Algorithm::RS256)) {
|
||||||
Ok(token_data) => {
|
Ok(token_data) => {
|
||||||
let claims = token_data.claims;
|
let claims = token_data.claims;
|
||||||
|
|
||||||
@@ -70,13 +60,10 @@ pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
|
|||||||
Err(Error::UnknownIssuer(claims.iis))
|
Err(Error::UnknownIssuer(claims.iis))
|
||||||
} else if claims.jti != 21 {
|
} else if claims.jti != 21 {
|
||||||
Err(Error::UnknownType(claims.jti))
|
Err(Error::UnknownType(claims.jti))
|
||||||
} else if claims.app != *VK_ID_CLIENT_ID {
|
} else if claims.app != client_id {
|
||||||
Err(Error::UnknownClientId(claims.app))
|
Err(Error::UnknownClientId(claims.app))
|
||||||
} else {
|
} else {
|
||||||
match claims.sub.parse::<i32>() {
|
Ok(claims.sub)
|
||||||
Ok(sub) => Ok(sub),
|
|
||||||
Err(_) => Err(Error::InvalidToken),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => Err(match err.into_kind() {
|
Err(err) => Err(match err.into_kind() {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ pub async fn sign_in_vk(
|
|||||||
) -> ServiceResponse {
|
) -> ServiceResponse {
|
||||||
let data = data_json.into_inner();
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
match parse_vk_id(&data.access_token) {
|
match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
|
||||||
Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(),
|
Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(),
|
||||||
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
|
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ pub async fn sign_up_vk(
|
|||||||
) -> ServiceResponse {
|
) -> ServiceResponse {
|
||||||
let data = data_json.into_inner();
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
match parse_vk_id(&data.access_token) {
|
match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
|
||||||
Ok(id) => sign_up_combined(
|
Ok(id) => sign_up_combined(
|
||||||
SignUpData {
|
SignUpData {
|
||||||
username: data.username,
|
username: data.username,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::app_state::Schedule;
|
|||||||
use crate::parser::parse_xls;
|
use crate::parser::parse_xls;
|
||||||
use crate::routes::schedule::schema::CacheStatus;
|
use crate::routes::schedule::schema::CacheStatus;
|
||||||
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
use crate::xls_downloader::interface::XLSDownloader;
|
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
||||||
use actix_web::web::Json;
|
use actix_web::web::Json;
|
||||||
use actix_web::{patch, web};
|
use actix_web::{patch, web};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
@@ -60,18 +60,20 @@ pub async fn update_download_url(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!("Unknown url provided {}", data.url);
|
if let FetchError::Unknown(error) = &error {
|
||||||
eprintln!("{:?}", error);
|
sentry::capture_error(&error);
|
||||||
|
}
|
||||||
|
|
||||||
ErrorCode::DownloadFailed.into_response()
|
ErrorCode::DownloadFailed(error).into_response()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!("Unknown url provided {}", data.url);
|
if let FetchError::Unknown(error) = &error {
|
||||||
eprintln!("{:?}", error);
|
sentry::capture_error(&error);
|
||||||
|
}
|
||||||
|
|
||||||
ErrorCode::FetchFailed.into_response()
|
ErrorCode::FetchFailed(error).into_response()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,6 +85,7 @@ mod schema {
|
|||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
use crate::xls_downloader::interface::FetchError;
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>;
|
pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>;
|
||||||
|
|
||||||
@@ -101,12 +104,12 @@ mod schema {
|
|||||||
NonWhitelistedHost,
|
NonWhitelistedHost,
|
||||||
|
|
||||||
/// Failed to retrieve file metadata.
|
/// Failed to retrieve file metadata.
|
||||||
#[display("Unable to retrieve metadata from the specified URL.")]
|
#[display("Unable to retrieve metadata from the specified URL: {_0}")]
|
||||||
FetchFailed,
|
FetchFailed(FetchError),
|
||||||
|
|
||||||
/// Failed to download the file.
|
/// Failed to download the file.
|
||||||
#[display("Unable to retrieve data from the specified URL.")]
|
#[display("Unable to retrieve data from the specified URL: {_0}")]
|
||||||
DownloadFailed,
|
DownloadFailed(FetchError),
|
||||||
|
|
||||||
/// The link leads to an outdated schedule.
|
/// The link leads to an outdated schedule.
|
||||||
///
|
///
|
||||||
@@ -127,8 +130,8 @@ mod schema {
|
|||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
ErrorCode::NonWhitelistedHost => serializer.serialize_str("NON_WHITELISTED_HOST"),
|
ErrorCode::NonWhitelistedHost => serializer.serialize_str("NON_WHITELISTED_HOST"),
|
||||||
ErrorCode::FetchFailed => serializer.serialize_str("FETCH_FAILED"),
|
ErrorCode::FetchFailed(_) => serializer.serialize_str("FETCH_FAILED"),
|
||||||
ErrorCode::DownloadFailed => serializer.serialize_str("DOWNLOAD_FAILED"),
|
ErrorCode::DownloadFailed(_) => serializer.serialize_str("DOWNLOAD_FAILED"),
|
||||||
ErrorCode::OutdatedSchedule => serializer.serialize_str("OUTDATED_SCHEDULE"),
|
ErrorCode::OutdatedSchedule => serializer.serialize_str("OUTDATED_SCHEDULE"),
|
||||||
ErrorCode::InvalidSchedule(_) => serializer.serialize_str("INVALID_SCHEDULE"),
|
ErrorCode::InvalidSchedule(_) => serializer.serialize_str("INVALID_SCHEDULE"),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,13 +59,16 @@ async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> Serv
|
|||||||
return ErrorCode::VkIdError.into_response();
|
return ErrorCode::VkIdError.into_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(auth_data) = res.json::<VkIdAuthResponse>().await {
|
match res.json::<VkIdAuthResponse>().await {
|
||||||
Ok(Response {
|
Ok(auth_data) =>
|
||||||
access_token: auth_data.id_token,
|
Ok(Response {
|
||||||
})
|
access_token: auth_data.id_token,
|
||||||
.into()
|
}).into(),
|
||||||
} else {
|
Err(error) => {
|
||||||
ErrorCode::VkIdError.into_response()
|
sentry::capture_error(&error);
|
||||||
|
|
||||||
|
ErrorCode::VkIdError.into_response()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => ErrorCode::VkIdError.into_response(),
|
Err(_) => ErrorCode::VkIdError.into_response(),
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
|
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use std::env;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct BasicXlsDownloader {
|
pub struct BasicXlsDownloader {
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
|
user_agent: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchResult {
|
async fn fetch_specified(url: &String, user_agent: &String, head: bool) -> FetchResult {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let response = if head {
|
let response = if head {
|
||||||
@@ -13,14 +16,14 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
|||||||
} else {
|
} else {
|
||||||
client.get(url)
|
client.get(url)
|
||||||
}
|
}
|
||||||
.header("User-Agent", user_agent)
|
.header("User-Agent", user_agent.clone())
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
if r.status().as_u16() != 200 {
|
if r.status().as_u16() != 200 {
|
||||||
return Err(FetchError::BadStatusCode);
|
return Err(FetchError::BadStatusCode(r.status().as_u16()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let headers = r.headers();
|
let headers = r.headers();
|
||||||
@@ -30,11 +33,18 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
|||||||
let last_modified = headers.get("last-modified");
|
let last_modified = headers.get("last-modified");
|
||||||
let date = headers.get("date");
|
let date = headers.get("date");
|
||||||
|
|
||||||
if content_type.is_none() || etag.is_none() || last_modified.is_none() || date.is_none()
|
if content_type.is_none() {
|
||||||
{
|
Err(FetchError::BadHeaders("Content-Type".to_string()))
|
||||||
Err(FetchError::BadHeaders)
|
} else if etag.is_none() {
|
||||||
|
Err(FetchError::BadHeaders("ETag".to_string()))
|
||||||
|
} else if last_modified.is_none() {
|
||||||
|
Err(FetchError::BadHeaders("Last-Modified".to_string()))
|
||||||
|
} else if date.is_none() {
|
||||||
|
Err(FetchError::BadHeaders("Date".to_string()))
|
||||||
} else if content_type.unwrap() != "application/vnd.ms-excel" {
|
} else if content_type.unwrap() != "application/vnd.ms-excel" {
|
||||||
Err(FetchError::BadContentType)
|
Err(FetchError::BadContentType(
|
||||||
|
content_type.unwrap().to_str().unwrap().to_string(),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
let etag = etag.unwrap().to_str().unwrap().to_string();
|
let etag = etag.unwrap().to_str().unwrap().to_string();
|
||||||
let last_modified =
|
let last_modified =
|
||||||
@@ -49,13 +59,16 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => Err(FetchError::Unknown),
|
Err(error) => Err(FetchError::Unknown(Arc::new(error))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BasicXlsDownloader {
|
impl BasicXlsDownloader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
BasicXlsDownloader { url: None }
|
BasicXlsDownloader {
|
||||||
|
url: None,
|
||||||
|
user_agent: env::var("REQWEST_USER_AGENT").expect("USER_AGENT must be set"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,17 +77,12 @@ impl XLSDownloader for BasicXlsDownloader {
|
|||||||
if self.url.is_none() {
|
if self.url.is_none() {
|
||||||
Err(FetchError::NoUrlProvided)
|
Err(FetchError::NoUrlProvided)
|
||||||
} else {
|
} else {
|
||||||
fetch_specified(
|
fetch_specified(self.url.as_ref().unwrap(), &self.user_agent, head).await
|
||||||
self.url.as_ref().unwrap(),
|
|
||||||
"t.me/polytechnic_next".to_string(),
|
|
||||||
head,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_url(&mut self, url: String) -> FetchResult {
|
async fn set_url(&mut self, url: String) -> FetchResult {
|
||||||
let result = fetch_specified(&url, "t.me/polytechnic_next".to_string(), true).await;
|
let result = fetch_specified(&url, &self.user_agent, true).await;
|
||||||
|
|
||||||
if let Ok(_) = result {
|
if let Ok(_) = result {
|
||||||
self.url = Some(url);
|
self.url = Some(url);
|
||||||
@@ -86,7 +94,7 @@ impl XLSDownloader for BasicXlsDownloader {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::xls_downloader::basic_impl::{BasicXlsDownloader, fetch_specified};
|
use crate::xls_downloader::basic_impl::{fetch_specified, BasicXlsDownloader};
|
||||||
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -95,8 +103,8 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
@@ -109,21 +117,17 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
assert!(results[1].is_err());
|
assert!(results[1].is_err());
|
||||||
|
|
||||||
assert_eq!(
|
let expected_error = FetchError::BadStatusCode(404);
|
||||||
*results[0].as_ref().err().unwrap(),
|
|
||||||
FetchError::BadStatusCode
|
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
|
||||||
);
|
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
|
||||||
assert_eq!(
|
|
||||||
*results[1].as_ref().err().unwrap(),
|
|
||||||
FetchError::BadStatusCode
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -132,15 +136,17 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
assert!(results[1].is_err());
|
assert!(results[1].is_err());
|
||||||
|
|
||||||
assert_eq!(*results[0].as_ref().err().unwrap(), FetchError::BadHeaders);
|
let expected_error = FetchError::BadHeaders("ETag".to_string());
|
||||||
assert_eq!(*results[1].as_ref().err().unwrap(), FetchError::BadHeaders);
|
|
||||||
|
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
|
||||||
|
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -149,21 +155,12 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
assert!(results[1].is_err());
|
assert!(results[1].is_err());
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*results[0].as_ref().err().unwrap(),
|
|
||||||
FetchError::BadContentType
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
*results[1].as_ref().err().unwrap(),
|
|
||||||
FetchError::BadContentType
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -172,8 +169,8 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_ok());
|
assert!(results[0].is_ok());
|
||||||
|
|||||||
@@ -1,22 +1,38 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
|
use std::mem::discriminant;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
/// XLS data retrieval errors.
|
/// XLS data retrieval errors.
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(Clone, Debug, ToSchema, Display)]
|
||||||
pub enum FetchError {
|
pub enum FetchError {
|
||||||
/// File url is not set.
|
/// File url is not set.
|
||||||
|
#[display("The link to the timetable was not provided earlier.")]
|
||||||
NoUrlProvided,
|
NoUrlProvided,
|
||||||
|
|
||||||
/// Unknown error.
|
/// Unknown error.
|
||||||
Unknown,
|
#[display("An unknown error occurred while downloading the file.")]
|
||||||
|
#[schema(value_type = String)]
|
||||||
|
Unknown(Arc<reqwest::Error>),
|
||||||
|
|
||||||
/// Server returned a status code different from 200.
|
/// Server returned a status code different from 200.
|
||||||
BadStatusCode,
|
#[display("Server returned a status code {_0}.")]
|
||||||
|
BadStatusCode(u16),
|
||||||
|
|
||||||
/// The url leads to a file of a different type.
|
/// The url leads to a file of a different type.
|
||||||
BadContentType,
|
#[display("The link leads to a file of type '{_0}'.")]
|
||||||
|
BadContentType(String),
|
||||||
|
|
||||||
/// Server doesn't return expected headers.
|
/// Server doesn't return expected headers.
|
||||||
BadHeaders,
|
#[display("Server doesn't return expected header(s) '{_0}'.")]
|
||||||
|
BadHeaders(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for FetchError {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
discriminant(self) == discriminant(other)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Result of XLS data retrieval.
|
/// Result of XLS data retrieval.
|
||||||
|
|||||||
Reference in New Issue
Block a user