11import { BaseController , TemplateHelpers } from './base_controller' ;
22import { ConfigService } from '../modules/system/config_service' ;
33import express from 'express' ;
4+ import os from 'os' ;
5+ import fs from 'fs' ;
6+ import path from 'path' ;
47
58export class SettingsController extends BaseController {
69 constructor (
@@ -11,26 +14,124 @@ export class SettingsController extends BaseController {
1114 }
1215
1316 registerRoutes ( router : express . Router ) : void {
14- router . get ( '/settings' , this . getSettings . bind ( this ) ) ;
15- router . post ( '/settings' , this . saveSettings . bind ( this ) ) ;
17+ router . get ( '/settings' , this . getIndex . bind ( this ) ) ;
18+ router . get ( '/settings/webserver' , this . getWebserver . bind ( this ) ) ;
19+ router . post ( '/settings/webserver' , this . saveWebserver . bind ( this ) ) ;
20+ router . get ( '/settings/notifications' , this . getNotifications . bind ( this ) ) ;
21+ router . post ( '/settings/notifications' , this . saveNotifications . bind ( this ) ) ;
1622 }
1723
18- private getSettings ( req : express . Request , res : express . Response ) : void {
24+ private getIndex ( req : express . Request , res : express . Response ) : void {
25+ const sysInfo = this . getSystemInfo ( ) ;
26+
27+ this . render ( res , 'settings/index' , {
28+ activePage : 'settings' ,
29+ activeSettingsPage : '' ,
30+ title : 'Settings | Crypto Bot' ,
31+ sysInfo
32+ } ) ;
33+ }
34+
35+ private getSystemInfo ( ) {
36+ const totalMem = os . totalmem ( ) ;
37+ const freeMem = os . freemem ( ) ;
38+ const usedMem = totalMem - freeMem ;
39+ const cpus = os . cpus ( ) ;
40+ const uptimeSec = os . uptime ( ) ;
41+
42+ const dbPath = path . join ( this . configService [ 'projectDir' ] , 'var' , 'bot.db' ) ;
43+ let dbSize : number | null = null ;
44+ try {
45+ dbSize = fs . statSync ( dbPath ) . size ;
46+ } catch ( _ ) { }
47+
48+ let diskTotal : number | null = null ;
49+ let diskFree : number | null = null ;
50+ try {
51+ const stat = ( fs as any ) . statfsSync ( '/' ) ;
52+ diskTotal = stat . blocks * stat . bsize ;
53+ diskFree = stat . bfree * stat . bsize ;
54+ } catch ( _ ) { }
55+
56+ return {
57+ memory : {
58+ total : totalMem ,
59+ used : usedMem ,
60+ free : freeMem ,
61+ usedPercent : Math . round ( ( usedMem / totalMem ) * 100 )
62+ } ,
63+ cpu : {
64+ count : cpus . length ,
65+ model : cpus [ 0 ] ?. model ?. replace ( / \s + / g, ' ' ) . trim ( ) ?? 'Unknown'
66+ } ,
67+ disk : diskTotal !== null && diskFree !== null
68+ ? {
69+ total : diskTotal ,
70+ free : diskFree ,
71+ used : diskTotal - diskFree ,
72+ usedPercent : Math . round ( ( ( diskTotal - diskFree ) / diskTotal ) * 100 )
73+ }
74+ : null ,
75+ db : {
76+ path : dbPath ,
77+ size : dbSize
78+ } ,
79+ uptime : uptimeSec ,
80+ nodeVersion : process . version ,
81+ platform : os . platform ( )
82+ } ;
83+ }
84+
85+ private getWebserver ( req : express . Request , res : express . Response ) : void {
1986 const settings = this . configService . getBotSettings ( ) ;
2087 const saved = req . query . saved === '1' ;
2188
22- this . render ( res , 'settings' , {
89+ this . render ( res , 'settings/webserver ' , {
2390 activePage : 'settings' ,
24- title : 'Settings | Crypto Bot' ,
91+ activeSettingsPage : 'webserver' ,
92+ title : 'Webserver Settings | Crypto Bot' ,
2593 settings,
2694 saved
2795 } ) ;
2896 }
2997
30- private saveSettings ( req : express . Request , res : express . Response ) : void {
98+ private saveWebserver ( req : express . Request , res : express . Response ) : void {
3199 const body = req . body ;
100+ const current = this . configService . getBotSettings ( ) ;
32101
33102 const settings = {
103+ ...current ,
104+ webserver : {
105+ ip : this . nullIfEmpty ( body . webserver_ip ) ,
106+ port : this . parseIntOrNull ( body . webserver_port ) ,
107+ username : this . nullIfEmpty ( body . webserver_username ) ,
108+ password : this . nullIfEmpty ( body . webserver_password )
109+ }
110+ } ;
111+
112+ this . configService . saveBotSettings ( settings ) ;
113+ res . redirect ( '/settings/webserver?saved=1' ) ;
114+ }
115+
116+ private getNotifications ( req : express . Request , res : express . Response ) : void {
117+ const settings = this . configService . getBotSettings ( ) ;
118+ const saved = req . query . saved === '1' ;
119+
120+ this . render ( res , 'settings/notifications' , {
121+ activePage : 'settings' ,
122+ activeSettingsPage : 'notifications' ,
123+ title : 'Notification Settings | Crypto Bot' ,
124+ settings,
125+ saved
126+ } ) ;
127+ }
128+
129+ private saveNotifications ( req : express . Request , res : express . Response ) : void {
130+ const body = req . body ;
131+ const current = this . configService . getBotSettings ( ) ;
132+
133+ const settings = {
134+ ...current ,
34135 notify : {
35136 slack : {
36137 webhook : this . nullIfEmpty ( body . slack_webhook ) ,
@@ -48,17 +149,11 @@ export class SettingsController extends BaseController {
48149 chat_id : this . nullIfEmpty ( body . telegram_chat_id ) ,
49150 token : this . nullIfEmpty ( body . telegram_token )
50151 }
51- } ,
52- webserver : {
53- ip : this . nullIfEmpty ( body . webserver_ip ) ,
54- port : this . parseIntOrNull ( body . webserver_port ) ,
55- username : this . nullIfEmpty ( body . webserver_username ) ,
56- password : this . nullIfEmpty ( body . webserver_password )
57152 }
58153 } ;
59154
60155 this . configService . saveBotSettings ( settings ) ;
61- res . redirect ( '/settings?saved=1' ) ;
156+ res . redirect ( '/settings/notifications ?saved=1' ) ;
62157 }
63158
64159 private nullIfEmpty ( value : string | undefined ) : string | null {
0 commit comments