init:init to origin

This commit is contained in:
2025-11-01 10:57:15 +08:00
parent f15daefed9
commit 6facd5f6cc
9 changed files with 141 additions and 0 deletions

8
.gitignore vendored
View File

@@ -136,3 +136,11 @@ dist
.yarn/install-state.gz
.pnp.*
# package
package-lock.json
pnpm-lock.yaml
# config
.env
config.json

6
config.example.json Normal file
View File

@@ -0,0 +1,6 @@
{
"express":{
"port": 3000,
"jwtSecret": ""
}
}

27
package.json Normal file
View File

@@ -0,0 +1,27 @@
{
"name": "Master",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "node dist/main.js",
"build": "tsc",
"test": "tsc && node dist/main.js"
},
"keywords": [],
"author": "Tianpao",
"license": "ISC",
"type": "module",
"packageManager": "pnpm@10.18.0",
"devDependencies": {
"@tsconfig/node22": "^22.0.2",
"@types/express": "^5.0.5",
"@types/jsonwebtoken": "^9.0.10",
"typescript": "^5.9.3"
},
"dependencies": {
"express": "^5.1.0",
"jsonwebtoken": "^9.0.2",
"socket.io": "^4.8.1"
}
}

23
src/config.ts Normal file
View File

@@ -0,0 +1,23 @@
import fs from "node:fs";
export interface Iconfig {
express: {
port: number;
jwtSecret: string;
}
}
const defaultConfig = {
"express":{
"port": 3000,
"jwtSecret": "JwtTianpaoMasterEasyTier000"
}
}
if(!fs.existsSync("./config.json")){
fs.writeFileSync("./config.json", JSON.stringify(defaultConfig));
}
const config:Iconfig = JSON.parse(fs.readFileSync("./config.json", "utf8"))
export default config

20
src/main.ts Normal file
View File

@@ -0,0 +1,20 @@
import express from 'express'
import { Server } from 'socket.io'
import http from 'node:http'
import config from './config.js'
import authRouter from './router/auth.js'
const app = express()
const server = http.createServer(app)
const io = new Server(server)
app.use(express.json()) //Loading JSON
/* Routes */
app.use('/cluster/auth', authRouter)
/* Routes */
server.listen(config.express.port,()=>{
console.log(`Server listening on port ${config.express.port}`)
})

16
src/middleware/Auth.ts Normal file
View File

@@ -0,0 +1,16 @@
import { Request,Response,NextFunction } from "express";
export class AuthMiddleware {
static Challenge(req: Request, res: Response, next: NextFunction) {
const id = req.query.clusterId
if (!id){
res.status(401).json({
message: "where is your clusterId?"
})
return;
}
next() //Latest
}
}

10
src/router/auth.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Router } from "express";
import { AuthMiddleware } from "../middleware/Auth.js";
const router = Router();
router.post('/challenge',AuthMiddleware.Challenge,(req,res)=>{
})
export default router;

15
src/utils/jwt.ts Normal file
View File

@@ -0,0 +1,15 @@
import jsonwebtoken from 'jsonwebtoken';
import config from '../config.js';
export class Jwt {
static sign(payload: object){
return jsonwebtoken.sign(payload,config.express.jwtSecret,{
expiresIn: 60 * 60 * 24, // 24 hours
algorithm: "HS256"
})
}
static verify(token: string) {
return jsonwebtoken.verify(token,config.express.jwtSecret)
}
}

16
tsconfig.json Normal file
View File

@@ -0,0 +1,16 @@
{
"extends": ["@tsconfig/node22"],
"compilerOptions": {
"sourceMap": true,
"noUnusedLocals": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"outDir": "dist",
"skipLibCheck": true
},
"exclude": [
"node_modules",
"public/**/*"
]
}