97 lines
3.8 KiB
TypeScript
97 lines
3.8 KiB
TypeScript
import pool from './appDb'
|
|
|
|
export interface Site {
|
|
id: string
|
|
name: string
|
|
url: string
|
|
interval_min: number
|
|
}
|
|
|
|
export interface Database {
|
|
id: string
|
|
name: string
|
|
host: string
|
|
port: number
|
|
database: string
|
|
username: string
|
|
password: string
|
|
ssl: boolean
|
|
color: string
|
|
db_type: 'postgres' | 'mysql'
|
|
}
|
|
|
|
export interface Service {
|
|
id: string
|
|
name: string
|
|
url: string
|
|
icon: string
|
|
description: string
|
|
}
|
|
|
|
export interface DockerHost {
|
|
id: string
|
|
name: string
|
|
url: string
|
|
}
|
|
|
|
export interface Config {
|
|
sites: Site[]
|
|
databases: Database[]
|
|
services: Service[]
|
|
docker_hosts: DockerHost[]
|
|
}
|
|
|
|
export function generateId(): string {
|
|
return `${Date.now()}-${Math.random().toString(36).slice(2, 7)}`
|
|
}
|
|
|
|
export async function readConfig(): Promise<Config> {
|
|
// DB'den oku
|
|
const [sitesRes, dbsRes, svcsRes, dockerRes] = await Promise.all([
|
|
pool.query(`SELECT * FROM config_sites`),
|
|
pool.query(`SELECT * FROM config_databases`),
|
|
pool.query(`SELECT * FROM config_services`),
|
|
pool.query(`SELECT * FROM config_docker_hosts`),
|
|
])
|
|
|
|
return {
|
|
sites: sitesRes.rows as Site[],
|
|
databases: dbsRes.rows as Database[],
|
|
services: svcsRes.rows as Service[],
|
|
docker_hosts: dockerRes.rows as DockerHost[]
|
|
}
|
|
}
|
|
|
|
export async function addConfigItem(type: 'site' | 'database' | 'service' | 'docker', item: any): Promise<any> {
|
|
const newItem = { ...item, id: generateId() }
|
|
if (type === 'site') {
|
|
await pool.query(`INSERT INTO config_sites (id, name, url, interval_min) VALUES ($1, $2, $3, $4)`, [newItem.id, newItem.name, newItem.url, newItem.interval_min || 5])
|
|
} else if (type === 'database') {
|
|
await pool.query(`INSERT INTO config_databases (id, name, host, port, database, username, password, ssl, color, db_type) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`, [newItem.id, newItem.name, newItem.host, newItem.port, newItem.database, newItem.username, newItem.password, newItem.ssl || false, newItem.color, newItem.db_type || 'postgres'])
|
|
} else if (type === 'service') {
|
|
await pool.query(`INSERT INTO config_services (id, name, url, icon, description) VALUES ($1, $2, $3, $4, $5)`, [newItem.id, newItem.name, newItem.url, newItem.icon, newItem.description])
|
|
} else if (type === 'docker') {
|
|
await pool.query(`INSERT INTO config_docker_hosts (id, name, url) VALUES ($1, $2, $3)`, [newItem.id, newItem.name, newItem.url])
|
|
}
|
|
return newItem
|
|
}
|
|
|
|
export async function updateConfigItem(type: 'site' | 'database' | 'service' | 'docker', id: string, item: any): Promise<void> {
|
|
if (type === 'site') {
|
|
await pool.query(`UPDATE config_sites SET name=$1, url=$2, interval_min=$3 WHERE id=$4`, [item.name, item.url, item.interval_min || 5, id])
|
|
} else if (type === 'database') {
|
|
await pool.query(`UPDATE config_databases SET name=$1, host=$2, port=$3, database=$4, username=$5, password=$6, ssl=$7, color=$8, db_type=$9 WHERE id=$10`, [item.name, item.host, item.port, item.database, item.username, item.password, item.ssl || false, item.color, item.db_type || 'postgres', id])
|
|
} else if (type === 'service') {
|
|
await pool.query(`UPDATE config_services SET name=$1, url=$2, icon=$3, description=$4 WHERE id=$5`, [item.name, item.url, item.icon, item.description, id])
|
|
} else if (type === 'docker') {
|
|
await pool.query(`UPDATE config_docker_hosts SET name=$1, url=$2 WHERE id=$3`, [item.name, item.url, id])
|
|
}
|
|
}
|
|
|
|
export async function deleteConfigItem(type: 'site' | 'database' | 'service' | 'docker', id: string): Promise<void> {
|
|
if (type === 'site') await pool.query(`DELETE FROM config_sites WHERE id=$1`, [id])
|
|
else if (type === 'database') await pool.query(`DELETE FROM config_databases WHERE id=$1`, [id])
|
|
else if (type === 'service') await pool.query(`DELETE FROM config_services WHERE id=$1`, [id])
|
|
else if (type === 'docker') await pool.query(`DELETE FROM config_docker_hosts WHERE id=$1`, [id])
|
|
}
|