mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 19:14:16 +00:00
implement server limits
This commit is contained in:
parent
cc543e865f
commit
80d79a5553
57
packages/app/src/components/LimitAlert/index.jsx
Normal file
57
packages/app/src/components/LimitAlert/index.jsx
Normal 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} />
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
24
packages/server/src/models/serverLimit/index.js
Normal file
24
packages/server/src/models/serverLimit/index.js
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user