feat: project init

This commit is contained in:
2024-03-15 09:05:49 +07:00
parent 01e5ae1a19
commit 9402b650b6
25 changed files with 1036 additions and 219 deletions
+175
View File
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store
BIN
View File
Binary file not shown.
+113
View File
@@ -0,0 +1,113 @@
import { Hono } from "hono";
import { cors } from "hono/cors";
import { zValidator } from "@hono/zod-validator";
import z from "zod";
import si from "systeminformation";
const formatBytes = (bytes: number) => {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes === 0) return "n/a";
const i = parseInt(String(Math.floor(Math.log(bytes) / Math.log(1024))), 10);
if (i === 0) return `${bytes} ${sizes[i]}`;
return `${(bytes / 1024 ** i).toFixed(2)} ${sizes[i]}`;
};
const secondsToTime = (seconds: number) => {
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor((seconds % (3600 * 24)) / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.floor(seconds % 60);
return `${d}d ${h}h ${m}m`;
};
const app = new Hono()
.use(cors())
.get("/", (c) => c.text("It works!"))
.get("/system", async (c) => {
const date = new Date().toISOString();
const uptime = secondsToTime(si.time().uptime || 0);
const system = await si.system();
const cpuSpeed = await si.cpuCurrentSpeed();
const cpuTemp = await si.cpuTemperature();
const cpuLoad = await si.currentLoad();
const mem = await si.mem();
const perf = {
cpu: {
load: cpuLoad.currentLoad,
speed: cpuSpeed.avg,
temp: cpuTemp.main,
},
mem: {
total: formatBytes(mem.total),
percent: (mem.active / mem.total) * 100,
used: formatBytes(mem.active),
free: formatBytes(mem.total - mem.active),
},
};
const fsMounts = await si.fsSize();
const storage = fsMounts
.filter((i) => i.size > 32 * 1024 * 1024 * 1024)
.map((i) => ({
type: i.type,
mount: i.mount,
used: formatBytes(i.used),
percent: (i.used / i.size) * 100,
total: formatBytes(i.size),
free: formatBytes(i.available),
}));
return c.json({ uptime, date, system, perf, storage });
})
.get(
"/process",
zValidator(
"query",
z
.object({ sort: z.enum(["cpu", "mem"]), limit: z.coerce.number() })
.partial()
.optional()
),
async (c) => {
const memTotal = (await si.mem()).total;
const sort = c.req.query("sort") || "mem";
const limit = parseInt(c.req.query("limit") || "") || 10;
let processList = (await si.processes()).list;
if (sort) {
switch (sort) {
case "cpu":
processList = processList.sort((a, b) => b.cpu - a.cpu);
break;
case "mem":
processList = processList.sort((a, b) => b.mem - a.mem);
break;
}
}
const list = processList
.map((p) => ({
name: p.name,
cmd: [p.name, p.params].filter(Boolean).join(" "),
cpu: p.cpu,
cpuPercent: p.cpu.toFixed(1) + "%",
mem: p.mem,
memUsage: formatBytes((p.mem / 100) * memTotal),
path: p.path,
user: p.user,
}))
.slice(0, limit);
return c.json({ list });
}
);
export type AppType = typeof app;
export default app;
+20
View File
@@ -0,0 +1,20 @@
{
"name": "backend",
"module": "index.ts",
"type": "module",
"scripts": {
"dev": "bun run --watch index.ts"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@hono/zod-validator": "^0.2.0",
"hono": "^4.1.0",
"systeminformation": "^5.22.2",
"zod": "^3.22.4"
}
}
+27
View File
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}