implement server limits

This commit is contained in:
SrGooglo 2023-04-26 23:10:03 +00:00
parent cc543e865f
commit 80d79a5553
3 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,57 @@
import { Alert } from "antd"
const FetchLimit = async (limit_id) => {
if (!limit_id) {
throw new Error("limit_id is required")
}
const response = await app.cores.api.customRequest({
method: "GET",
url: `/global_server_limits/${limit_id}`,
})
return response.data
}
const Limit = (props) => {
const { limit_id } = props
if (!limit_id) {
console.error("limit_id is required")
return null
}
const [L_Limit, R_Limit, E_Limit, M_Limit] = app.cores.api.useRequest(FetchLimit, limit_id)
if (E_Limit) {
console.log(`Failed to fetch limit ${limit_id} >`, E_Limit)
return null
}
if (L_Limit) {
return null
}
const componentProps = {
type: "warning",
closable: true,
...R_Limit?.data?.componentProps ?? {},
}
return <Alert
{...componentProps}
message={R_Limit?.data?.message ?? "Warning"}
description={R_Limit?.data?.description ?? "An limit has been reached"}
/>
}
export default (props) => {
const { limit_id } = props
if (Array.isArray(limit_id)) {
return limit_id.map((limit_id) => <Limit key={limit_id} limit_id={limit_id} />)
}
return <Limit {...props} />
}

View File

@ -0,0 +1,27 @@
import { ServerLimit } from "@models"
export default {
method: "GET",
route: "/global_server_limits/:limitkey",
fn: async (req, res) => {
const { limitkey } = req.params
const serverLimit = await ServerLimit.findOne({
key: limitkey,
active: true,
})
.catch(err => {
return res.status(500).json({
error: err.message
})
})
if (!serverLimit) {
return res.status(404).json({
error: "Server limit not found or inactive"
})
}
return res.json(serverLimit)
}
}

View File

@ -0,0 +1,24 @@
import { Schema } from "mongoose"
export default {
name: "ServerLimit",
collection: "serverLimits",
schema: {
key: {
type: String,
required: true,
},
value: {
type: Schema.Types.Mixed,
required: true,
},
active: {
type: Boolean,
default: true,
},
data: {
type: Object,
required: false,
}
}
}