Skip to content

Commit

Permalink
Change to add frontend admin panel
Browse files Browse the repository at this point in the history
Wait until add extra to sea-query [PR](SeaQL/sea-query#611)
Wait until fix cors to tonic_web [PR](hyperium/tonic#1286)
  • Loading branch information
nrot committed Feb 25, 2023
1 parent 937d75a commit c51a132
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 39 deletions.
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.2"
actix-web = "4.3"
chrono = "0.4"
sqlx = {version="0.6", features=["runtime-tokio-rustls", "postgres", "migrate", "uuid", "chrono", "json"]}
sqlx = {version="0.6", features=["runtime-tokio-native-tls", "postgres", "migrate", "uuid", "chrono", "json"]}
log = "0.4"
serde = {version="1.0", features=["derive"]}
serde_json = "1.0"
simple_logger = { version = "4.0", features = ["colors"] }
tokio = { version = "1.22", features = ["rt", "time", "sync", "signal"] }
tokio = { version = "1.25", features = ["rt", "time", "sync", "signal"] }
futures = "0.3"
dptree = "0.3"
mime = "0.3"
uuid = { version = "1.2", features = ["serde", "v4"] }
sea-orm = { version = "0.10", features = ["sqlx-postgres", "runtime-tokio-rustls", "sqlx-dep"] }
sea-query = {version="0.27", features=["derive", "thread-safe", "backend-postgres", "with-chrono", "with-json", "with-uuid"]} #"postgres-array"
uuid = { version = "1.3", features = ["serde", "v4"] }
sea-orm = { version = "0.11", features = ["sqlx-postgres", "runtime-tokio-native-tls", "sqlx-dep"] }
sea-query = {version="0.28", features=["derive", "thread-safe", "backend-postgres", "with-chrono", "with-json", "with-uuid"]} #"postgres-array"
ts-rs = {version = "6.2", features=["uuid", "uuid-impl", "format", "chrono", "chrono-impl", "bytes"]}
utoipa = { version="2.0", features = ["actix_extras"] }
utoipa = { version="3.0", features = ["actix_extras"] }
utoipa-swagger-ui = {version="3.0", features = ["actix-web"] }
actix-cors = "0.6"
syntect="5.0"
cached="0.40"
cached="0.42"
dotenv = "0.15"
cron="0.12"
tonic = "0.8"
prost = "0.11"
prost-types = "0.11"
tracing = { version = "0.1.37", features = ["log"] }
pbjson = "0.5.1"
tonic-web = "0.5.0"


[build-dependencies]
Expand Down
1 change: 1 addition & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_GRPCSERVER_BIND_URI=[::1]:50053
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions frontend/src/lib/trpc/transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";

console.log(import.meta.env);

export const baseUrl = import.meta.env.VITE_GRPCSERVER_BIND_URI;

export const GrpcTransport = new GrpcWebFetchTransport({
baseUrl: "http://" + baseUrl,
format: "binary"
});
9 changes: 9 additions & 0 deletions frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<script setup>
import TheWelcome from '../components/TheWelcome.vue'
import {AdminPanelClient} from "../lib/trpc/proto/admin.client";
import { IndexListRequest } from "../lib/trpc/proto/index";
import {GrpcTransport} from '../lib/trpc/transport';
const admin = new AdminPanelClient(GrpcTransport);
console.log(admin.getIndexes(IndexListRequest.create()));
</script>

<template>
Expand Down
19 changes: 13 additions & 6 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '');
process.env = {...process.env, ...env};
return {
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
define: {
// __APP_ENV__: env.APP_ENV,
},
}
}
})
Empty file modified gen-entity.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion gen-rust-rpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ echo "Generate frontend part"
PROTOC_GEN_TS_PATH="./node/node_modules/.bin/protoc-gen-ts"

# Directory to write generated code to (.js and .d.ts files)
OUT_DIR="./src/lib/trpc/proto"
OUT_DIR="./src/lib/trpc"

cd frontend/
npx protoc --ts_opt optimize_code_size --ts_out $OUT_DIR -I../ --proto_path ../proto proto/index.proto proto/admin.proto --experimental_allow_proto3_optional
Expand Down
1 change: 1 addition & 0 deletions migrations/20230225_add_citus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE EXTENSION IF NOT EXISTS citus;
1 change: 0 additions & 1 deletion proto/index.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
syntax = "proto3";
package index;
// import "google/protobuf/timestamp.proto";

message Index{
string id = 1;
Expand Down
1 change: 1 addition & 0 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{credentials, model, schemes, handlers::{self, ReportPull}, event};

async fn sqlx_migrate() {
let p = sqlx::postgres::PgPoolOptions::new()
.test_before_acquire(true)
.max_connections(credentials::postgres_connections())
.connect(&credentials::postgres_uri())
.await
Expand Down
2 changes: 2 additions & 0 deletions src/rest/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async fn create_index(
ColumnDef::new(LogLog::Column::Id)
.uuid()
.extra("DEFAULT uuid_generate_v4()".into())
.primary_key()
.not_null(),
)
.col(
Expand All @@ -101,6 +102,7 @@ async fn create_index(
.json_binary()
.not_null(),
)
//TODO: Добавить extra и USING columnar для работы с citus
.to_owned();
db.execute(sea_orm::Statement::from_string(
db.get_database_backend(),
Expand Down
4 changes: 2 additions & 2 deletions src/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use super::schemes as gschemes;

pub fn routing(cfg: &mut web::ServiceConfig) {
cfg.service(insert_json)
.service(ping)
.service(SwaggerUi::new("/swagger/{_:.*}").url("insert-json", InsertJson::openapi()));
.service(ping);
// .service(SwaggerUi::new("/swagger/{_:.*}").url("insert-json", InsertJson::openapi()));
admin::routing(cfg);
}

Expand Down
44 changes: 43 additions & 1 deletion src/rpc/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
use super::RequestResult;
use crate::model::log_index as LogIndex;
use sea_orm::{DatabaseConnection, EntityTrait};
use tonic::{Response, Status};

mod admin;
mod index;
mod index;

pub use admin::admin_panel_server::AdminPanelServer;

pub struct AdminRpcServer {
pub db: DatabaseConnection,
}

#[tonic::async_trait]
impl admin::admin_panel_server::AdminPanel for AdminRpcServer {
///
async fn get_indexes(
&self,
request: tonic::Request<index::IndexListRequest>,
) -> RequestResult<index::IndexListResponse> {
match LogIndex::Entity::find().all(&self.db).await {
Ok(data) => Ok(Response::new(index::IndexListResponse {
indexes: data
.into_iter()
.map(|m| index::Index {
id: m.id.to_string(),
name: m.name.unwrap_or_default(),
})
.collect(),
})),
Err(e) => Err(Status::internal(format!("Database error: {}", e))),
}
}
///
async fn search(
&self,
request: tonic::Request<index::IndexSearchRequest>,
) -> RequestResult<index::IndexSearchResult> {
Ok(Response::new(index::IndexSearchResult {
response: Vec::new(),
}))
}
}
49 changes: 29 additions & 20 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
use sea_orm::DatabaseConnection;
use tonic::{Request, Response, Status, transport::Server};
use tonic::{transport::Server, Request, Response, Status};

mod rpc;
mod admin;
mod rpc;

use crate::{core, credentials};

pub struct RpcServer{
pub struct RpcServer {
db: DatabaseConnection,
}

type RequestResult<T> = Result<Response<T>, Status>;

#[tonic::async_trait]
impl rpc::saver_server::Saver for RpcServer{
async fn send_message(&self, request: Request<rpc::Message>)->RequestResult<rpc::ResultMessage>{
impl rpc::saver_server::Saver for RpcServer {
async fn send_message(
&self,
request: Request<rpc::Message>,
) -> RequestResult<rpc::ResultMessage> {
let m = request.get_ref();
log::info!("rpc request: {:?}", m);
let v = if let Ok(v) = serde_json::from_str(m.data.as_str()) {
v
} else{
return Err(Status::invalid_argument("Can`t convert json from data"))
} else {
return Err(Status::invalid_argument("Can`t convert json from data"));
};
if let Err(e) = core::insert_message(&self.db, &m.index, v).await{
if let Err(e) = core::insert_message(&self.db, &m.index, v).await {
log::error!("Can`t inser value: {}", e);
return Err(Status::internal("Can`t insert value"));
};
Ok(Response::new(rpc::ResultMessage{
ok: true
}))
Ok(Response::new(rpc::ResultMessage { ok: true }))
}
}

pub async fn run(db: DatabaseConnection){
pub async fn run(db: DatabaseConnection) {
log::info!("Rpc server starting");
let addr = credentials::grpcserver_bind_uri().parse().unwrap();
let server = RpcServer{
db
};
let server = RpcServer { db: db.clone() };
let admin = admin::AdminRpcServer { db: db.clone() };

let svc = rpc::saver_server::SaverServer::new(server);

Server::builder().trace_fn(|_|{
tracing::info_span!("Grpc request: ")
}).add_service(svc).serve(addr).await.unwrap();
}
let admin_server = admin::AdminPanelServer::new(admin);
let corsed = tonic_web::GrpcWebLayer::new();


Server::builder()
.trace_fn(|_| tracing::info_span!("Grpc request: {}"))
.accept_http1(true)
.layer(corsed)
.add_service(admin_server)
.add_service(svc)
.serve(addr)
.await
.unwrap();
}

0 comments on commit c51a132

Please sign in to comment.