First commit

This commit is contained in:
DarkCat09 2023-07-21 18:50:01 +04:00
commit 4f226ee8f5
13 changed files with 1901 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

3
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/webtgbot.iml" filepath="$PROJECT_DIR$/webtgbot.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

1729
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "webtgbot"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
askama = "0.12.0"
axum = "0.6.19"
once_cell = "1.18.0"
teloxide = { version = "0.12.2", features = ["macros"] }
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }

17
src/bot.rs Normal file
View file

@ -0,0 +1,17 @@
use teloxide::Bot;
use teloxide::prelude::Requester;
use teloxide::types::Message;
use super::common::STATE;
pub async fn main() {
let bot = Bot::from_env();
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
if msg.text().is_some() {
STATE.lock().unwrap().set_name(msg.text().unwrap().to_string());
bot.send_message(msg.chat.id, msg.text().unwrap()).await?;
}
Ok(())
}).await;
}

23
src/common.rs Normal file
View file

@ -0,0 +1,23 @@
use std::sync::Mutex;
use once_cell::sync::Lazy;
#[derive(Clone)]
pub struct State {
name: String,
}
impl State {
pub fn name(self) -> String {
self.name
}
pub fn set_name(&mut self, name: String) {
self.name = name
}
}
pub static STATE: Lazy<Mutex<State>> = Lazy::new(||
Mutex::new(
State { name: String::from("world") }
)
);

10
src/main.rs Normal file
View file

@ -0,0 +1,10 @@
mod bot;
mod web;
mod common;
use tokio::join;
#[tokio::main]
async fn main() {
join!(bot::main(), web::main());
}

37
src/web.rs Normal file
View file

@ -0,0 +1,37 @@
use std::net::SocketAddr;
use axum::Router;
use axum::routing::get;
use askama::Template;
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse};
use super::common::STATE;
pub async fn main() {
let app = Router::new()
.route("/", get(root));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn root() -> impl IntoResponse {
let name = STATE.lock().unwrap().clone().name();
let tmpl = RootTemplate { name };
match tmpl.render() {
Ok(html) => Html(html).into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
Html(format!("Template error:<br><pre>{}</pre>", err))
).into_response(),
}
}
#[derive(Template)]
#[template(path = "index.html")]
struct RootTemplate {
name: String
}

36
templates/index.html Normal file
View file

@ -0,0 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
<style>
body {
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: #202022;
color: #eee;
font-family: sans-serif;
}
h1 {
margin: 0;
padding: 0;
font-size: 2rem;
font-weight: 600;
}
</style>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>

12
webtgbot.iml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUST_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>