init:init to origin
This commit is contained in:
8
.gitignore
vendored
8
.gitignore
vendored
@@ -136,3 +136,11 @@ dist
|
|||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# package
|
||||||
|
package-lock.json
|
||||||
|
pnpm-lock.yaml
|
||||||
|
|
||||||
|
# config
|
||||||
|
.env
|
||||||
|
config.json
|
||||||
|
|
||||||
|
|||||||
6
config.example.json
Normal file
6
config.example.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"express":{
|
||||||
|
"port": 3000,
|
||||||
|
"jwtSecret": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
27
package.json
Normal file
27
package.json
Normal 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
23
src/config.ts
Normal 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
20
src/main.ts
Normal 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
16
src/middleware/Auth.ts
Normal 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
10
src/router/auth.ts
Normal 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
15
src/utils/jwt.ts
Normal 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
16
tsconfig.json
Normal 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/**/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user