mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Compare commits
11 Commits
ff12ee5da2
...
release/v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
b664ba578d
|
|||
|
983967f8b0
|
|||
|
e5760120e2
|
|||
|
a28fb66dd4
|
|||
|
3780fb3136
|
|||
|
6c71bc19f5
|
|||
|
2d0041dc8b
|
|||
|
b5d372e109
|
|||
|
84dca02c34
|
|||
|
6c9d3b3b31
|
|||
|
a348b1b99b
|
142
.github/workflows/build.yml
vendored
Normal file
142
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
name: build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
tags-ignore: [ "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: |
|
||||||
|
cargo test --verbose
|
||||||
|
env:
|
||||||
|
DATABASE_URL: ${{ env.TEST_DB }}
|
||||||
|
SCHEDULE_DISABLE_AUTO_UPDATE: 1
|
||||||
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
|
VK_ID_CLIENT_ID: 0
|
||||||
|
VK_ID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||||
|
TELEGRAM_BOT_ID: 0
|
||||||
|
TELEGRAM_MINI_APP_HOST: example.com
|
||||||
|
TELEGRAM_TEST_DC: false
|
||||||
|
YANDEX_CLOUD_API_KEY: ""
|
||||||
|
YANDEX_CLOUD_FUNC_ID: ""
|
||||||
|
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 }}"
|
||||||
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@@ -2,7 +2,7 @@ name: cargo test
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "master" ]
|
branches: [ "development" ]
|
||||||
tags-ignore: [ "release/v*" ]
|
tags-ignore: [ "release/v*" ]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
|
|||||||
863
Cargo.lock
generated
863
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
18
Cargo.toml
18
Cargo.toml
@@ -3,7 +3,7 @@ members = ["actix-macros", "actix-test", "providers"]
|
|||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "schedule-parser-rusted"
|
name = "schedule-parser-rusted"
|
||||||
version = "1.2.1"
|
version = "1.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ actix-macros = { path = "actix-macros" }
|
|||||||
actix-web = "4.11.0"
|
actix-web = "4.11.0"
|
||||||
|
|
||||||
# basic
|
# basic
|
||||||
chrono = { version = "0.4.41", features = ["serde"] }
|
chrono = { version = "0.4.42", features = ["serde"] }
|
||||||
derive_more = { version = "2.0.1", features = ["full"] }
|
derive_more = { version = "2.0.1", features = ["full"] }
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
|
|
||||||
@@ -48,13 +48,13 @@ reqwest = { version = "0.12.23", features = ["json"] }
|
|||||||
mime = "0.3.17"
|
mime = "0.3.17"
|
||||||
|
|
||||||
# error handling
|
# error handling
|
||||||
sentry = "0.42.0"
|
sentry = "0.43.0"
|
||||||
sentry-actix = "0.42.0"
|
sentry-actix = "0.43.0"
|
||||||
|
|
||||||
# [de]serializing
|
# [de]serializing
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1.0.143"
|
serde_json = "1"
|
||||||
serde_with = "3.14.0"
|
serde_with = "3.14"
|
||||||
|
|
||||||
sha1 = "0.11.0-rc.2"
|
sha1 = "0.11.0-rc.2"
|
||||||
|
|
||||||
@@ -65,12 +65,12 @@ utoipa-actix-web = "0.1.2"
|
|||||||
|
|
||||||
uuid = { version = "1.18.1", features = ["v4"] }
|
uuid = { version = "1.18.1", features = ["v4"] }
|
||||||
hex-literal = "1"
|
hex-literal = "1"
|
||||||
log = "0.4.27"
|
log = "0.4.28"
|
||||||
|
|
||||||
# telegram webdata deciding and verify
|
# telegram webdata deciding and verify
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
percent-encoding = "2.3.2"
|
percent-encoding = "2.3.2"
|
||||||
ed25519-dalek = "3.0.0-pre.0"
|
ed25519-dalek = "3.0.0-pre.1"
|
||||||
|
|
||||||
# development tracing
|
# development tracing
|
||||||
console-subscriber = { version = "0.4.1", optional = true }
|
console-subscriber = { version = "0.4.1", optional = true }
|
||||||
|
|||||||
@@ -100,6 +100,9 @@ pub enum LessonType {
|
|||||||
|
|
||||||
/// Защита курсового проекта.
|
/// Защита курсового проекта.
|
||||||
CourseProjectDefense,
|
CourseProjectDefense,
|
||||||
|
|
||||||
|
/// Практическое занятие.
|
||||||
|
Practice
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "provider-engels-polytechnic"
|
name = "provider-engels-polytechnic"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
@@ -20,7 +20,7 @@ derive_more = { version = "2.0.1", features = ["error", "display"] }
|
|||||||
|
|
||||||
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
|
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
|
||||||
|
|
||||||
calamine = "0.30.0"
|
calamine = "0.30"
|
||||||
async-trait = "0.1.89"
|
async-trait = "0.1.89"
|
||||||
|
|
||||||
reqwest = "0.12.23"
|
reqwest = "0.12.23"
|
||||||
@@ -28,5 +28,5 @@ ua_generator = "0.5.22"
|
|||||||
regex = "1.11.2"
|
regex = "1.11.2"
|
||||||
strsim = "0.11.1"
|
strsim = "0.11.1"
|
||||||
log = "0.4.27"
|
log = "0.4.27"
|
||||||
sentry = "0.42.0"
|
sentry = "0.43.0"
|
||||||
|
|
||||||
|
|||||||
@@ -240,6 +240,7 @@ fn guess_lesson_type(text: &str) -> Option<LessonType> {
|
|||||||
("экзамен", LessonType::ExamDefault),
|
("экзамен", LessonType::ExamDefault),
|
||||||
("курсовой проект", LessonType::CourseProject),
|
("курсовой проект", LessonType::CourseProject),
|
||||||
("защита курсового проекта", LessonType::CourseProjectDefense),
|
("защита курсового проекта", LessonType::CourseProjectDefense),
|
||||||
|
("практическое занятие", LessonType::Practice),
|
||||||
])
|
])
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -447,7 +448,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
|
|||||||
|
|
||||||
static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
Regex::new(
|
Regex::new(
|
||||||
r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(\s*\d\s*[а-я\s]+\))?(?:[\s,]+)?){1,2}+[\s.,]*",
|
r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(?\s*\d\s*[а-я\s]+\)?)?(?:[\s,.]+)?){1,2}+[\s.,]*",
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
});
|
});
|
||||||
@@ -456,7 +457,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
|
|||||||
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap());
|
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap());
|
||||||
|
|
||||||
let text = CLEAN_RE
|
let text = CLEAN_RE
|
||||||
.replace(&text.replace([' ', '\t', '\n'], " "), " ")
|
.replace(&text.replace([' ', '\t', '\n'], " ").replace(",", ""), " ")
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) {
|
let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) {
|
||||||
@@ -470,13 +471,15 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
|
|||||||
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
|
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
|
||||||
|
|
||||||
for name in src.split(',') {
|
for name in src.split(',') {
|
||||||
let open_bracket_index = name.find('(');
|
let digit_index = name.find(|c: char| c.is_ascii_digit());
|
||||||
|
|
||||||
let number: u8 = open_bracket_index
|
let number: u8 =
|
||||||
.map_or(0, |index| name[(index + 1)..(index + 2)].parse().unwrap());
|
digit_index.map_or(0, |index| name[(index)..(index + 1)].parse().unwrap());
|
||||||
|
|
||||||
let teacher_name = {
|
let teacher_name = {
|
||||||
let name_end = open_bracket_index.unwrap_or(name.len());
|
let name_end = name
|
||||||
|
.find(|c: char| !c.is_alphabetic())
|
||||||
|
.unwrap_or(name.len());
|
||||||
|
|
||||||
// Я ебал. Как же я долго до этого доходил.
|
// Я ебал. Как же я долго до этого доходил.
|
||||||
format!(
|
format!(
|
||||||
@@ -525,7 +528,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
|
|||||||
if result.is_none() {
|
if result.is_none() {
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
sentry::capture_message(
|
sentry::capture_message(
|
||||||
&*format!("Не удалось угадать тип пары '{}'!", extra),
|
&format!("Не удалось угадать тип пары '{}'!", extra),
|
||||||
sentry::Level::Warning,
|
sentry::Level::Warning,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -46,14 +46,17 @@ pub mod error {
|
|||||||
/// problems with the Yandex Cloud Function invocation.
|
/// problems with the Yandex Cloud Function invocation.
|
||||||
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
|
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
|
||||||
RequestFailed(reqwest::Error),
|
RequestFailed(reqwest::Error),
|
||||||
|
|
||||||
|
#[display("Unable to fetch Uri in 3 retries")]
|
||||||
|
UriFetchFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Errors that may occur during the creation of a schedule snapshot.
|
/// Errors that may occur during the creation of a schedule snapshot.
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
pub enum SnapshotCreationError {
|
pub enum SnapshotCreationError {
|
||||||
/// The URL is the same as the one already being used (no update needed).
|
/// The ETag is the same (no update needed).
|
||||||
#[display("The URL is the same as the one already being used.")]
|
#[display("The ETag is the same.")]
|
||||||
SameUrl,
|
Same,
|
||||||
|
|
||||||
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
|
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
|
||||||
#[display("Failed to fetch URL: {_0}")]
|
#[display("Failed to fetch URL: {_0}")]
|
||||||
@@ -86,10 +89,6 @@ impl Updater {
|
|||||||
downloader: &mut XlsDownloader,
|
downloader: &mut XlsDownloader,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<ScheduleSnapshot, SnapshotCreationError> {
|
) -> Result<ScheduleSnapshot, SnapshotCreationError> {
|
||||||
if downloader.url.as_ref().is_some_and(|_url| _url.eq(&url)) {
|
|
||||||
return Err(SnapshotCreationError::SameUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
let head_result = downloader.set_url(&url).await.map_err(|error| {
|
let head_result = downloader.set_url(&url).await.map_err(|error| {
|
||||||
if let FetchError::Unknown(error) = &error {
|
if let FetchError::Unknown(error) = &error {
|
||||||
sentry::capture_error(&error);
|
sentry::capture_error(&error);
|
||||||
@@ -98,6 +97,10 @@ impl Updater {
|
|||||||
SnapshotCreationError::FetchFailed(error)
|
SnapshotCreationError::FetchFailed(error)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
if downloader.etag == Some(head_result.etag) {
|
||||||
|
return Err(SnapshotCreationError::Same);
|
||||||
|
}
|
||||||
|
|
||||||
let xls_data = downloader
|
let xls_data = downloader
|
||||||
.fetch(false)
|
.fetch(false)
|
||||||
.await
|
.await
|
||||||
@@ -144,7 +147,21 @@ impl Updater {
|
|||||||
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
|
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let uri = client
|
let uri = {
|
||||||
|
// вот бы добавили named-scopes как в котлине,
|
||||||
|
// чтоб мне не пришлось такой хуйнёй страдать.
|
||||||
|
#[allow(unused_assignments)]
|
||||||
|
let mut uri = String::new();
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if counter == 3 {
|
||||||
|
return Err(QueryUrlError::UriFetchFailed);
|
||||||
|
}
|
||||||
|
|
||||||
|
counter += 1;
|
||||||
|
|
||||||
|
uri = client
|
||||||
.post(format!(
|
.post(format!(
|
||||||
"https://functions.yandexcloud.net/{}?integration=raw",
|
"https://functions.yandexcloud.net/{}?integration=raw",
|
||||||
func_id
|
func_id
|
||||||
@@ -157,6 +174,17 @@ impl Updater {
|
|||||||
.await
|
.await
|
||||||
.map_err(QueryUrlError::RequestFailed)?;
|
.map_err(QueryUrlError::RequestFailed)?;
|
||||||
|
|
||||||
|
if uri.is_empty() {
|
||||||
|
log::warn!("[{}] Unable to get uri! Retrying in 5 seconds...", counter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uri
|
||||||
|
};
|
||||||
|
|
||||||
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
|
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,7 +277,7 @@ impl Updater {
|
|||||||
|
|
||||||
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
|
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
|
||||||
Ok(snapshot) => snapshot,
|
Ok(snapshot) => snapshot,
|
||||||
Err(SnapshotCreationError::SameUrl) => {
|
Err(SnapshotCreationError::Same) => {
|
||||||
let mut clone = current_snapshot.clone();
|
let mut clone = current_snapshot.clone();
|
||||||
clone.update();
|
clone.update();
|
||||||
|
|
||||||
|
|||||||
@@ -66,25 +66,30 @@ pub struct FetchOk {
|
|||||||
/// Date data received.
|
/// Date data received.
|
||||||
pub requested_at: DateTime<Utc>,
|
pub requested_at: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Etag.
|
||||||
|
pub etag: String,
|
||||||
|
|
||||||
/// File data.
|
/// File data.
|
||||||
pub data: Option<Vec<u8>>,
|
pub data: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FetchOk {
|
impl FetchOk {
|
||||||
/// Result without file content.
|
/// Result without file content.
|
||||||
pub fn head(uploaded_at: DateTime<Utc>) -> Self {
|
pub fn head(uploaded_at: DateTime<Utc>, etag: String) -> Self {
|
||||||
FetchOk {
|
FetchOk {
|
||||||
uploaded_at,
|
uploaded_at,
|
||||||
requested_at: Utc::now(),
|
requested_at: Utc::now(),
|
||||||
|
etag,
|
||||||
data: None,
|
data: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Full result.
|
/// Full result.
|
||||||
pub fn get(uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
|
pub fn get(uploaded_at: DateTime<Utc>, etag: String, data: Vec<u8>) -> Self {
|
||||||
FetchOk {
|
FetchOk {
|
||||||
uploaded_at,
|
uploaded_at,
|
||||||
requested_at: Utc::now(),
|
requested_at: Utc::now(),
|
||||||
|
etag,
|
||||||
data: Some(data),
|
data: Some(data),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,11 +99,15 @@ pub type FetchResult = Result<FetchOk, FetchError>;
|
|||||||
|
|
||||||
pub struct XlsDownloader {
|
pub struct XlsDownloader {
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
|
pub etag: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XlsDownloader {
|
impl XlsDownloader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
XlsDownloader { url: None }
|
XlsDownloader {
|
||||||
|
url: None,
|
||||||
|
etag: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
|
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
|
||||||
@@ -124,9 +133,12 @@ impl XlsDownloader {
|
|||||||
.get("Content-Type")
|
.get("Content-Type")
|
||||||
.ok_or(FetchError::bad_headers("Content-Type"))?;
|
.ok_or(FetchError::bad_headers("Content-Type"))?;
|
||||||
|
|
||||||
if !headers.contains_key("etag") {
|
let etag = headers
|
||||||
return Err(FetchError::bad_headers("etag"));
|
.get("etag")
|
||||||
}
|
.ok_or(FetchError::bad_headers("etag"))?
|
||||||
|
.to_str()
|
||||||
|
.or(Err(FetchError::bad_headers("etag")))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
let last_modified = headers
|
let last_modified = headers
|
||||||
.get("last-modified")
|
.get("last-modified")
|
||||||
@@ -141,9 +153,13 @@ impl XlsDownloader {
|
|||||||
.with_timezone(&Utc);
|
.with_timezone(&Utc);
|
||||||
|
|
||||||
Ok(if head {
|
Ok(if head {
|
||||||
FetchOk::head(last_modified)
|
FetchOk::head(last_modified, etag)
|
||||||
} else {
|
} else {
|
||||||
FetchOk::get(last_modified, response.bytes().await.unwrap().to_vec())
|
FetchOk::get(
|
||||||
|
last_modified,
|
||||||
|
etag,
|
||||||
|
response.bytes().await.unwrap().to_vec(),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,6 @@ use jsonwebtoken::errors::ErrorKind;
|
|||||||
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
|
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
|
||||||
struct TokenData {
|
|
||||||
iis: String,
|
|
||||||
sub: i32,
|
|
||||||
app: i32,
|
|
||||||
exp: i32,
|
|
||||||
iat: i32,
|
|
||||||
jti: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: i32,
|
sub: i32,
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ mod tests {
|
|||||||
id: Set(id.clone()),
|
id: Set(id.clone()),
|
||||||
username: Set(username),
|
username: Set(username),
|
||||||
password: Set(Some(
|
password: Set(Some(
|
||||||
bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap(),
|
bcrypt::hash("example", bcrypt::DEFAULT_COST).unwrap(),
|
||||||
)),
|
)),
|
||||||
vk_id: Set(None),
|
vk_id: Set(None),
|
||||||
telegram_id: Set(None),
|
telegram_id: Set(None),
|
||||||
|
|||||||
@@ -49,10 +49,12 @@ pub async fn telegram_auth(
|
|||||||
let web_app_user =
|
let web_app_user =
|
||||||
serde_json::from_str::<WebAppUser>(init_data.data_map.get("user").unwrap()).unwrap();
|
serde_json::from_str::<WebAppUser>(init_data.data_map.get("user").unwrap()).unwrap();
|
||||||
|
|
||||||
let user =
|
let user = match Query::find_user_by_telegram_id(app_state.get_database(), web_app_user.id)
|
||||||
match Query::find_user_by_telegram_id(app_state.get_database(), web_app_user.id).await {
|
.await
|
||||||
Ok(Some(value)) => Ok(value),
|
.expect("Failed to find user by telegram id")
|
||||||
_ => {
|
{
|
||||||
|
Some(value) => value,
|
||||||
|
None => {
|
||||||
let new_user = ActiveUser {
|
let new_user = ActiveUser {
|
||||||
id: Set(ObjectId::new().unwrap().to_string()),
|
id: Set(ObjectId::new().unwrap().to_string()),
|
||||||
username: Set(format!("telegram_{}", web_app_user.id)), // можно оставить, а можно поменять
|
username: Set(format!("telegram_{}", web_app_user.id)), // можно оставить, а можно поменять
|
||||||
@@ -64,10 +66,12 @@ pub async fn telegram_auth(
|
|||||||
android_version: Set(None),
|
android_version: Set(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
new_user.insert(app_state.get_database()).await
|
new_user
|
||||||
|
.insert(app_state.get_database())
|
||||||
|
.await
|
||||||
|
.expect("Failed to insert user")
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
.expect("Failed to get or add user");
|
|
||||||
|
|
||||||
let access_token = utility::jwt::encode(&user.id);
|
let access_token = utility::jwt::encode(&user.id);
|
||||||
Ok(Response::new(&access_token, user.group.is_some())).into()
|
Ok(Response::new(&access_token, user.group.is_some())).into()
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ mod env;
|
|||||||
|
|
||||||
pub use crate::state::env::AppEnv;
|
pub use crate::state::env::AppEnv;
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
use database::sea_orm::{Database, DatabaseConnection};
|
use database::migration::{Migrator, MigratorTrait};
|
||||||
|
use database::sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||||
use providers::base::{ScheduleProvider, ScheduleSnapshot};
|
use providers::base::{ScheduleProvider, ScheduleSnapshot};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
/// Common data provided to endpoints.
|
/// Common data provided to endpoints.
|
||||||
@@ -55,9 +57,24 @@ impl AppState {
|
|||||||
database
|
database
|
||||||
} else {
|
} else {
|
||||||
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
Database::connect(&database_url)
|
|
||||||
|
let mut opt = ConnectOptions::new(database_url.clone());
|
||||||
|
|
||||||
|
opt.max_connections(4)
|
||||||
|
.min_connections(2)
|
||||||
|
.connect_timeout(Duration::from_secs(10))
|
||||||
|
.idle_timeout(Duration::from_secs(8))
|
||||||
|
.sqlx_logging(true);
|
||||||
|
|
||||||
|
let database = Database::connect(opt)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url));
|
||||||
|
|
||||||
|
Migrator::up(&database, None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to run database migrations");
|
||||||
|
|
||||||
|
database
|
||||||
},
|
},
|
||||||
env,
|
env,
|
||||||
providers,
|
providers,
|
||||||
|
|||||||
@@ -24,14 +24,13 @@ static ENCODING_KEY: LazyLock<EncodingKey> = LazyLock::new(|| {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/// Token verification errors.
|
/// Token verification errors.
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// The token has a different signature.
|
/// The token has a different signature.
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
|
|
||||||
/// Token reading error.
|
/// Token reading error.
|
||||||
InvalidToken(ErrorKind),
|
InvalidToken,
|
||||||
|
|
||||||
/// Token expired.
|
/// Token expired.
|
||||||
Expired,
|
Expired,
|
||||||
@@ -82,7 +81,7 @@ pub fn verify_and_decode(token: &str) -> Result<String, Error> {
|
|||||||
Err(err) => Err(match err.into_kind() {
|
Err(err) => Err(match err.into_kind() {
|
||||||
ErrorKind::InvalidSignature => Error::InvalidSignature,
|
ErrorKind::InvalidSignature => Error::InvalidSignature,
|
||||||
ErrorKind::ExpiredSignature => Error::Expired,
|
ErrorKind::ExpiredSignature => Error::Expired,
|
||||||
kind => Error::InvalidToken(kind),
|
_ => Error::InvalidToken,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,7 +114,7 @@ mod tests {
|
|||||||
fn test_encode() {
|
fn test_encode() {
|
||||||
test_env();
|
test_env();
|
||||||
|
|
||||||
assert_eq!(encode(&"test".to_string()).is_empty(), false);
|
assert!(!encode("test").is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -128,7 +127,7 @@ mod tests {
|
|||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result.err().unwrap(),
|
result.err().unwrap(),
|
||||||
Error::InvalidToken(ErrorKind::InvalidToken)
|
Error::InvalidToken
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user