commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
12
.idea/dataSources.xml
generated
Normal file
12
.idea/dataSources.xml
generated
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="postgres@home.hzer.xyz" uuid="ba0d4fe5-fba9-4548-96e8-838b7bdc054a">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://home.hzer.xyz:5432/postgres</jdbc-url>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/serv.iml" filepath="$PROJECT_DIR$/.idea/serv.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
11
.idea/serv.iml
generated
Normal file
11
.idea/serv.iml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<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>
|
||||
6
.idea/sqldialects.xml
generated
Normal file
6
.idea/sqldialects.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="PROJECT" dialect="PostgreSQL" />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"actix"
|
||||
]
|
||||
}
|
||||
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "init"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.11.0"
|
||||
actix-files = "0.6.6"
|
||||
actix-cors = "0.7.0"
|
||||
log = "0.4"
|
||||
env_logger = "0.11.8"
|
||||
r2d2 = "0.8.10"
|
||||
r2d2_sqlite = "0.30.0"
|
||||
rusqlite = { version = "0.36", features = ["bundled"] }
|
||||
BIN
database.sqlite
Normal file
BIN
database.sqlite
Normal file
Binary file not shown.
64958
resources/ID/id.txt
Normal file
64958
resources/ID/id.txt
Normal file
File diff suppressed because it is too large
Load Diff
0
resources/ID/id2.txt
Normal file
0
resources/ID/id2.txt
Normal file
11
src/dto/location.rs
Normal file
11
src/dto/location.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
pub struct Location {
|
||||
latitude: f64,
|
||||
longitude: f64,
|
||||
altitude: f64,
|
||||
accuracy: f64,
|
||||
vertical_accuracy: f64,
|
||||
bearing: f64,
|
||||
speed: f64,
|
||||
elapsedMs: i64,
|
||||
provider: String,
|
||||
}
|
||||
1
src/dto/mod.rs
Normal file
1
src/dto/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod location;
|
||||
13
src/handler/location.rs
Normal file
13
src/handler/location.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use crate::utils::database::SqliteState;
|
||||
use actix_web::{get, web};
|
||||
use log::info;
|
||||
|
||||
// 上报location位置
|
||||
#[get("/{user}/addr/post")]
|
||||
pub async fn location(
|
||||
user_id: web::Path<String>,
|
||||
db: web::Data<SqliteState>,
|
||||
) -> Result<String, actix_web::Error> {
|
||||
info!("user_id: {}", user_id);
|
||||
Ok(user_id.into_inner())
|
||||
}
|
||||
1
src/handler/mod.rs
Normal file
1
src/handler/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod location;
|
||||
22
src/main.rs
Normal file
22
src/main.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::utils::database::init_database;
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
mod handler;
|
||||
mod utils;
|
||||
mod test;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// 初始化 logger 内容
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let db = init_database();
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(db.clone())
|
||||
.configure(utils::router::location)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
1
src/test/mod.rs
Normal file
1
src/test/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod sqlite;
|
||||
32
src/test/sqlite.rs
Normal file
32
src/test/sqlite.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
mod sqlite {
|
||||
use std::{
|
||||
fs::{File, read_to_string},
|
||||
io::{BufRead, BufReader, Read},
|
||||
};
|
||||
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
#[test]
|
||||
fn init() {
|
||||
let manager = SqliteConnectionManager::file("database.sqlite");
|
||||
let pool = r2d2::Pool::new(manager).unwrap();
|
||||
|
||||
let file = File::open("./resources/ID/id.txt").unwrap();
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
for line in reader.lines() {
|
||||
match line {
|
||||
Ok(line) => {
|
||||
let line = line.split("----").collect::<Vec<&str>>();
|
||||
let (name, id) = (line[0], line[1]);
|
||||
let conn = pool.get().unwrap();
|
||||
let mut stmt = conn.prepare("INSERT INTO id (name, id) VALUES (?1, ?2)").unwrap();
|
||||
stmt.execute((name, id)).unwrap();
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/utils/database.rs
Normal file
15
src/utils/database.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use actix_web::web;
|
||||
use r2d2::{Pool, State};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use std::sync::Mutex;
|
||||
use actix_web::web::Data;
|
||||
|
||||
pub struct SqliteState {
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
pub fn init_database() -> Data<SqliteState> {
|
||||
let db = SqliteConnectionManager::file("./database.sqlite");
|
||||
let pool = Pool::new(db).unwrap();
|
||||
Data::new(SqliteState { pool: pool.clone() })
|
||||
}
|
||||
2
src/utils/mod.rs
Normal file
2
src/utils/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod router;
|
||||
pub mod database;
|
||||
7
src/utils/router.rs
Normal file
7
src/utils/router.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use actix_web::{web};
|
||||
|
||||
use crate::handler::location;
|
||||
|
||||
pub fn location(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(web::scope("/location").service(location::location));
|
||||
}
|
||||
Reference in New Issue
Block a user