mirror of
https://github.com/OSMA-D/osma-server.git
synced 2024-12-24 19:53:46 +03:00
feat: get All apps (OS-5)
This commit is contained in:
parent
f2137f1bc0
commit
70fba74aa8
4 changed files with 125 additions and 0 deletions
49
src/core.rs
Normal file
49
src/core.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
use actix_web::web::Json;
|
||||
|
||||
use bson::{doc, Document};
|
||||
use chrono::Utc;
|
||||
use futures::{StreamExt, TryStreamExt};
|
||||
use jsonwebtoken::{encode, EncodingKey, Header};
|
||||
use mongodb::{
|
||||
options::{FindOneOptions, FindOptions, UpdateOptions},
|
||||
Collection, Database,
|
||||
};
|
||||
use serde_json::json;
|
||||
use sha3::{Digest, Sha3_256};
|
||||
use std::env;
|
||||
|
||||
use crate::types::*;
|
||||
pub struct Core {
|
||||
users: Collection<Document>,
|
||||
apps: Collection<Document>,
|
||||
reviews: Collection<Document>,
|
||||
personal_libraries: Collection<Document>,
|
||||
jwt_secret: String,
|
||||
salt: String,
|
||||
}
|
||||
|
||||
impl Core {
|
||||
pub fn new(db: &Database) -> Core {
|
||||
Core {
|
||||
users: db.collection("users"),
|
||||
apps: db.collection("apps"),
|
||||
reviews: db.collection("reviews"),
|
||||
personal_libraries: db.collection("personal_libraries"),
|
||||
jwt_secret: env::var("JWT_SECRET").expect("JWT_SECRET not found"),
|
||||
salt: env::var("SALT").expect("Hash salt not found"),
|
||||
}
|
||||
}
|
||||
pub async fn get_apps(&self) -> Vec<Document> {
|
||||
self.get_collection(&self.apps).await
|
||||
}
|
||||
|
||||
async fn get_collection(&self, collection: &Collection<Document>) -> Vec<Document> {
|
||||
let options = FindOptions::builder().projection(doc! {"_id" : 0}).build();
|
||||
let cursor = match collection.find(None, options).await {
|
||||
Ok(cursor) => cursor,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
cursor.try_collect().await.unwrap_or_else(|_| vec![])
|
||||
}
|
||||
}
|
52
src/main.rs
Normal file
52
src/main.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use actix_cors::Cors;
|
||||
use actix_web::{web, App, Error, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
|
||||
use actix_web::dev::ServiceRequest;
|
||||
use actix_web::http::header::HeaderName;
|
||||
use actix_web::http::header::HeaderValue;
|
||||
use actix_web_grants::permissions::AttachPermissions;
|
||||
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
|
||||
mod core;
|
||||
mod routes;
|
||||
mod types;
|
||||
|
||||
pub struct AppState {
|
||||
core: core::Core,
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
|
||||
let port: u16 = env::var("PORT")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("PORT must be a number");
|
||||
|
||||
let client_options = mongodb::options::ClientOptions::parse(
|
||||
env::var("MONGODB_URI").expect("Mongodb uri not found"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let client = mongodb::Client::with_options(client_options).unwrap();
|
||||
let db = client.database("osma");
|
||||
|
||||
HttpServer::new(move || {
|
||||
let cors = Cors::default().allow_any_origin();
|
||||
App::new()
|
||||
.app_data(web::Data::new(AppState {
|
||||
core: core::Core::new(&db),
|
||||
}))
|
||||
.wrap(cors)
|
||||
.service(routes::apps)
|
||||
})
|
||||
.bind(("0.0.0.0", port))
|
||||
.expect("Can not bind to port")
|
||||
.run()
|
||||
.await
|
||||
}
|
7
src/routes.rs
Normal file
7
src/routes.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use crate::types::*;
|
||||
use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder};
|
||||
use actix_web_grants::proc_macro::{has_any_permission, has_permissions};
|
||||
#[get("/apps")]
|
||||
pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder {
|
||||
HttpResponse::Ok().json(app_data.core.get_apps().await)
|
||||
}
|
17
src/types.rs
Normal file
17
src/types.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct App {
|
||||
id: String,
|
||||
name: String,
|
||||
description: String,
|
||||
version: String,
|
||||
platform: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
pub password: String,
|
||||
pub email: String,
|
||||
}
|
Loading…
Add table
Reference in a new issue