Add .env file

This commit is contained in:
2024-05-03 02:08:55 +01:00
parent c5a04a691e
commit 4790602f4e
15 changed files with 55 additions and 13 deletions

6
.env Normal file
View File

@@ -0,0 +1,6 @@
CLIENT_GRAPH_STEPS=150
CLIENT_PORT=3000
CLIENT_REFETCH_INTERVAL=500
SERVER_ACTIVE_WINDOW=5000
SERVER_PORT=3001
SERVER_REFRESH_INTERVAL=500

7
Cargo.lock generated
View File

@@ -198,6 +198,12 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "either" name = "either"
version = "1.8.1" version = "1.8.1"
@@ -792,6 +798,7 @@ name = "stats_server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"dotenv",
"serde_json", "serde_json",
"sysinfo", "sysinfo",
"tokio", "tokio",

View File

@@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
axum = { version = "0.6.9", features = ["macros", "ws"] } axum = { version = "0.6.9", features = ["macros", "ws"] }
dotenv = "0.15.0"
serde_json = "1.0.93" serde_json = "1.0.93"
sysinfo = "0.30.11" sysinfo = "0.30.11"
tokio = { version = "1.25.0", features = ["full"] } tokio = { version = "1.25.0", features = ["full"] }
@@ -14,4 +15,4 @@ tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
[[bin]] [[bin]]
name = "stats_server" name = "stats_server"
path = "src/stats-server/main.rs" path = "src/stats-server/main.rs"

View File

@@ -4,9 +4,9 @@
"version": "0.0.0", "version": "0.0.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite --host & cargo run", "dev": "vite --port $CLIENT_PORT --host & cargo run",
"build": "tsc && vite build && cargo build --release", "build": "tsc && vite build && cargo build --release",
"preview": "vite preview --host & cargo run --release" "preview": "vite --port $CLIENT_PORT preview --host & cargo run --release"
}, },
"dependencies": { "dependencies": {
"@tanstack/react-query": "^5.32.0", "@tanstack/react-query": "^5.32.0",

View File

@@ -19,7 +19,7 @@ const Main = () => {
const dynamicQuery = useQuery({ const dynamicQuery = useQuery({
queryKey: ['dynamic'], queryKey: ['dynamic'],
queryFn: fetchDynamicData, queryFn: fetchDynamicData,
refetchInterval: 500, refetchInterval: Number(import.meta.env.CLIENT_REFETCH_INTERVAL),
}); });
const isLoading = staticQuery.isLoading || dynamicQuery.isLoading; const isLoading = staticQuery.isLoading || dynamicQuery.isLoading;

View File

@@ -1,6 +1,6 @@
const getApiUrl = (path: string) => { const getApiUrl = (path: string) => {
const url = new URL(window.location.href); const url = new URL(window.location.href);
url.port = '3001'; url.port = import.meta.env.SERVER_PORT;
url.pathname = path; url.pathname = path;
return url; return url;

View File

@@ -5,7 +5,7 @@ import { ChartCard } from './index';
export const Cpu = () => { export const Cpu = () => {
const { data: staticData } = useQuery<StaticData>({ queryKey: ['static'] }); const { data: staticData } = useQuery<StaticData>({ queryKey: ['static'] });
const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] }); const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] });
const [history, setHistory] = useState<number[][]>(new Array(150).fill([])); const [history, setHistory] = useState<number[][]>(new Array(Number(import.meta.env.CLIENT_GRAPH_STEPS)).fill([]));
useEffect(() => { useEffect(() => {
if (dynamicData) { if (dynamicData) {

View File

@@ -4,7 +4,7 @@ import { ChartCard } from './index';
export const Disks = () => { export const Disks = () => {
const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] }); const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] });
const [history, setHistory] = useState<number[][]>(new Array(150).fill([])); const [history, setHistory] = useState<number[][]>(new Array(Number(import.meta.env.CLIENT_GRAPH_STEPS)).fill([]));
useEffect(() => { useEffect(() => {
if (dynamicData) { if (dynamicData) {

View File

@@ -8,7 +8,7 @@ const formatOptions = { units: 'B' };
export const Memory = () => { export const Memory = () => {
const { data: staticData } = useQuery<StaticData>({ queryKey: ['static'] }); const { data: staticData } = useQuery<StaticData>({ queryKey: ['static'] });
const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] }); const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] });
const [history, setHistory] = useState<number[][]>(new Array(150).fill([])); const [history, setHistory] = useState<number[][]>(new Array(Number(import.meta.env.CLIENT_GRAPH_STEPS)).fill([]));
useEffect(() => { useEffect(() => {
if (dynamicData) { if (dynamicData) {

View File

@@ -4,7 +4,7 @@ import { ChartCard } from './index';
export const Network = () => { export const Network = () => {
const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] }); const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] });
const [history, setHistory] = useState<number[][]>(new Array(150).fill([])); const [history, setHistory] = useState<number[][]>(new Array(Number(import.meta.env.CLIENT_GRAPH_STEPS)).fill([]));
useEffect(() => { useEffect(() => {
if (dynamicData) { if (dynamicData) {

View File

@@ -5,7 +5,7 @@ import { ChartCard } from './index';
export const Temps = () => { export const Temps = () => {
const { data: staticData } = useQuery<StaticData>({ queryKey: ['static'] }); const { data: staticData } = useQuery<StaticData>({ queryKey: ['static'] });
const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] }); const { data: dynamicData } = useQuery<DynamicData>({ queryKey: ['dynamic'] });
const [history, setHistory] = useState<number[][]>(new Array(150).fill([])); const [history, setHistory] = useState<number[][]>(new Array(Number(import.meta.env.CLIENT_GRAPH_STEPS)).fill([]));
useEffect(() => { useEffect(() => {
if (dynamicData) { if (dynamicData) {

View File

@@ -1 +1,14 @@
/// <reference types="vite/client" /> /// <reference types="vite/client" />
interface ImportMetaEnv {
readonly CLIENT_GRAPH_STEPS: string;
readonly CLIENT_PORT: string;
readonly CLIENT_REFETCH_INTERVAL: string;
readonly SERVER_ACTIVE_WINDOW: string;
readonly SERVER_PORT: string;
readonly SERVER_REFRESH_INTERVAL: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}

View File

@@ -1,4 +1,5 @@
use axum::{extract::State, http::Response, response::IntoResponse, routing::get, Router, Server}; use axum::{extract::State, http::Response, response::IntoResponse, routing::get, Router, Server};
use dotenv::dotenv;
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::{ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
@@ -27,6 +28,18 @@ impl Default for AppState {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
dotenv().ok();
let refresh_interval = Duration::from_millis(
std::env::var("SERVER_REFRESH_INTERVAL")
.unwrap()
.parse::<u64>()
.unwrap(),
);
let active_window = std::env::var("SERVER_ACTIVE_WINDOW")
.unwrap()
.parse::<u128>()
.unwrap();
let port = std::env::var("SERVER_PORT").unwrap();
let app_state = AppState::default(); let app_state = AppState::default();
let router = Router::new() let router = Router::new()
@@ -45,7 +58,7 @@ async fn main() {
let now = Instant::now(); let now = Instant::now();
let latest = app_state.latest.lock().unwrap().clone(); let latest = app_state.latest.lock().unwrap().clone();
if (now - latest).as_millis() < 10000 { if (now - latest).as_millis() < active_window {
sys.refresh_cpu(); sys.refresh_cpu();
sys.refresh_memory(); sys.refresh_memory();
components.refresh(); components.refresh();
@@ -82,11 +95,12 @@ async fn main() {
} }
} }
std::thread::sleep(Duration::from_millis(200)); std::thread::sleep(refresh_interval);
} }
}); });
let server = Server::bind(&"0.0.0.0:3001".parse().unwrap()).serve(router.into_make_service()); let server = Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap())
.serve(router.into_make_service());
let addr = server.local_addr(); let addr = server.local_addr();
println!("Listening on {addr}"); println!("Listening on {addr}");

View File

@@ -4,6 +4,7 @@ import { defineConfig } from 'vite';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
envPrefix: ['CLIENT', 'SERVER'],
plugins: [react()], plugins: [react()],
resolve: { resolve: {
alias: { alias: {