feat: 写了点后端
This commit is contained in:
1 parent
e2079c5388
commit
79cc9c34d6
20 files changed
+219
-20
No files matched your search
@@ -0,0 +1,4 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
return
|
||||
})
|
||||
@@ -1,7 +1,9 @@
|
||||
import fs from 'fs';
|
||||
import checkUserData from '~/server/utils/checkUserData';
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const res = fs.readdirSync('./')
|
||||
checkUserData()
|
||||
const res = fs.readdirSync('userData')
|
||||
return {
|
||||
res
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import serverConfig from "../server.config"
|
||||
|
||||
const pathNeedAuth = serverConfig.auth.api
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
// console.log('New request: ' + getRequestURL(event))
|
||||
const path = getRequestURL(event).pathname
|
||||
if(pathNeedAuth.has(path)){
|
||||
// console.log('Auth needed')
|
||||
return true
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,4 @@
|
||||
export default defineEventHandler((event) => {
|
||||
checkUserData()
|
||||
// console.log('New request: ' + getRequestURL(event))
|
||||
})
|
||||
@@ -0,0 +1,8 @@
|
||||
export default {
|
||||
auth: {
|
||||
// 需要进行鉴权的api
|
||||
api: new Set([
|
||||
'/api/test/testDir'
|
||||
])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import mkfileIfNotExist from "./mkfileIfNotExist"
|
||||
|
||||
let checked = false
|
||||
|
||||
/**
|
||||
* 检查并完善userData的目录结构,若不存在密钥则生成随机的密钥
|
||||
* @returns {void}
|
||||
*/
|
||||
const checkUserData = () => {
|
||||
if (checked) return
|
||||
checked = true
|
||||
// console.log('check userData')
|
||||
const dirList = [
|
||||
'./userData/',
|
||||
'./userData/db/',
|
||||
'./userData/md/',
|
||||
]
|
||||
dirList.forEach(mkdirIfNotExist)
|
||||
mkfileIfNotExist('./userData/key.txt', new Array(2048).fill(0).map(() => Math.floor(Math.random() * 16).toString(16)).join(''))
|
||||
}
|
||||
|
||||
export default checkUserData
|
||||
@@ -0,0 +1,15 @@
|
||||
import fs from 'fs';
|
||||
|
||||
export default function mkdirIfNotExists(dir: string | string[]): void {
|
||||
if (typeof dir !== 'string') {
|
||||
dir.forEach((d) => mkdirIfNotExists(d));
|
||||
return;
|
||||
}
|
||||
if (!fs.existsSync(dir)) {
|
||||
try {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import mkdirIfNotExist from './mkdirIfNotExist';
|
||||
|
||||
export default function mkfileIfNotExist(filePath: string | string[], content?: string): void {
|
||||
content = content || '';
|
||||
if (Array.isArray(filePath)) {
|
||||
filePath.forEach((file) => mkfileIfNotExist(file, content));
|
||||
return;
|
||||
}
|
||||
if (typeof filePath !== 'string') {
|
||||
console.error('Invalid file path:', filePath);
|
||||
return;
|
||||
}
|
||||
if (fs.existsSync(filePath)) {
|
||||
return;
|
||||
}
|
||||
const dir = path.dirname(filePath);
|
||||
mkdirIfNotExist(dir);
|
||||
try {
|
||||
fs.writeFileSync(filePath, content);
|
||||
} catch (error) {
|
||||
console.error('Error creating file:', error);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user