bump, add /info endpoint, cleanup

This commit is contained in:
Aidan 2025-07-08 20:49:30 -04:00
parent 0ceb0b61b4
commit da82475262
4 changed files with 74 additions and 12 deletions

8
server/.gitignore vendored
View file

@ -37,4 +37,10 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
bun.lock*
# Files
serve/
serve/
# db
dev.db
# config
config/config.ts

View file

@ -0,0 +1,25 @@
export const config: Config = {
name: "Example Server", // what your server should be named in BeeSrv
author: "example", // who owns this server?
channelLink: "https://t.me/example", // telegram channel link
currentBox: {
enabled: true, // if you want to serve the box, set to true
name: "Example Beebox", // what your box should be named in BeeSrv
originalAuthor: "Author", // who originally shared the beebox, if applicable
},
}
// don't change this
export interface Config {
name: string
author: string
channelLink: string
currentBox: {
enabled: boolean
name: string
originalAuthor: string
}
}

View file

@ -8,17 +8,17 @@
"start": "bun run src/index.ts"
},
"devDependencies": {
"@types/bun": "^1.2.9",
"@types/bun": "^1.2.18",
"drizzle-kit": "^0.30.6"
},
"peerDependencies": {
"typescript": "^5.8.3"
},
"dependencies": {
"@types/express": "^5.0.1",
"better-sqlite3": "^11.9.1",
"@types/express": "^5.0.3",
"better-sqlite3": "^11.10.0",
"chalk": "^5.4.1",
"dotenv": "^16.5.0",
"dotenv": "^16.6.1",
"drizzle-orm": "^0.41.0",
"express": "^5.1.0"
}

View file

@ -8,6 +8,7 @@ import { eq, and, gte } from "drizzle-orm"
import chalk from "chalk"
import * as schema from './db/schema'
import path from 'path'
import { config } from '../config/config'
const app = express()
const sqlite = new Database(process.env.DB_FILE_NAME || 'dev.db')
@ -29,20 +30,50 @@ enum ReqStatus {
function checkRateLimit(email: string): boolean {
const now = Date.now()
const userLimit = rateLimitMap.get(email)
if (!userLimit || now > userLimit.resetTime) {
rateLimitMap.set(email, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS })
return true
}
if (userLimit.count >= MAX_REQUESTS_PER_WINDOW) {
return false
}
userLimit.count++
return true
}
app.get("/info", (async (req, res) => {
const info: Record<string, any> = {
name: config.name,
author: config.author,
channelLink: config.channelLink,
};
if (config.currentBox.enabled) {
info.currentBox = config.currentBox;
try {
const beebox = await Bun.file(path.join(__dirname, '../serve/beebox.xml'))
const bbxContent = await beebox.text()
if (!bbxContent) {
console.log(chalk.red(`[INFO]`) + ` Error reading beebox file`)
info.error = "Unable to find beebox file on server"
} else {
const hasher = new Bun.CryptoHasher("sha256")
const bbxHash = await hasher.update(bbxContent).digest()
const bbxHashHex = bbxHash.toString('hex')
info.currentBox.hash = bbxHashHex
}
} catch (error) {
console.log(chalk.red(`[INFO]`) + ` Error reading beebox file`)
info.error = "Unable to find beebox file on server"
}
}
res.json(info);
}) as RequestHandler)
app.post("/bee-req", (async (req, res) => {
const { deviceID, version, email } = req.body
const requestID = randomUUID()
@ -75,7 +106,7 @@ app.post("/bee-req", (async (req, res) => {
console.log(chalk.yellow(`[REQ | ${requestID}]`) + ` Existing user: ${existingUser ? "true" : "false"}`)
let userId: string;
if (!existingUser) {
try {
// Create new user
@ -83,11 +114,11 @@ app.post("/bee-req", (async (req, res) => {
id: randomUUID(),
email: email as string,
}).returning()
if (!newUser[0]) {
throw new Error("Failed to create user")
}
userId = newUser[0].id
console.log(chalk.green(`[REQ | ${requestID}]`) + ` Created new user for request: ${userId}`)
} catch (error) {
@ -165,6 +196,6 @@ app.listen(process.env.PORT || 3000, async () => {
const pkgJson = Bun.file(path.join(__dirname, '../package.json'))
const version = JSON.parse(await pkgJson.text()).version
console.log(chalk.bgBlue("BEESRV") + " v" + version + "\n")
console.log(chalk.green(`[SERVER] `) + `Running on port ${process.env.PORT || 3000}`)
})