@@ -26,57 +26,17 @@ const globalState = {
2626
2727// Read maintenance and banner states from in-memory storage
2828async function getMaintenanceState ( env , host , useCache = true ) {
29- // Read cache config from env
30- const cacheEnabled = env . ENABLE_CACHE === undefined ? true : env . ENABLE_CACHE === true || env . ENABLE_CACHE === 'true' ;
31- const cacheTtl = env . CACHE_TTL_MS ? parseInt ( env . CACHE_TTL_MS , 10 ) : 60000 ;
32-
33- const now = Date . now ( ) ;
34- let stateObj ;
35- if ( useCache && cacheEnabled ) {
36- if ( cache . maintenance . value && ( now - cache . maintenance . ts < cacheTtl ) ) {
37- stateObj = cache . maintenance . value ;
38- } else {
39- const stateRaw = await env . MAINTENANCE_KV . get ( 'MAINTENANCE_STATE' ) ;
40- stateObj = safeJsonParse ( stateRaw , {
41- isGlobalMaintenance : false ,
42- subdomainsMaintenance : [ ] ,
43- bannerSubdomains : [ ] ,
44- bannerMessage : ''
45- } ) ;
46- cache . maintenance . value = stateObj ;
47- cache . maintenance . ts = now ;
48- }
49- } else {
50- const stateRaw = await env . MAINTENANCE_KV . get ( 'MAINTENANCE_STATE' ) ;
51- stateObj = safeJsonParse ( stateRaw , {
52- isGlobalMaintenance : false ,
53- subdomainsMaintenance : [ ] ,
54- bannerSubdomains : [ ] ,
55- bannerMessage : ''
56- } ) ;
57- }
58-
59- // Cache for wan-is-4g
60- let is4gMode ;
61- if ( useCache && cacheEnabled ) {
62- if ( cache . is4g . value !== null && ( now - cache . is4g . ts < cacheTtl ) ) {
63- is4gMode = cache . is4g . value ;
64- } else {
65- is4gMode = await env . MAINTENANCE_KV . get ( 'wan-is-4g' ) ;
66- cache . is4g . value = is4gMode ;
67- cache . is4g . ts = now ;
68- }
69- } else {
70- is4gMode = await env . MAINTENANCE_KV . get ( 'wan-is-4g' ) ;
71- }
29+ // Utilise l'état global en mémoire
30+ const stateObj = globalState . maintenanceState ;
31+ const is4gMode = globalState . is4gMode ;
7232
7333 return {
74- isGlobalMaintenance : stateObj . isGlobalMaintenance === true || stateObj . isGlobalMaintenance === 'true' ,
75- subdomainsMaintenance : Array . isArray ( stateObj . subdomainsMaintenance ) ? stateObj . subdomainsMaintenance : [ ] ,
76- isSubdomainMaintenance : Array . isArray ( stateObj . subdomainsMaintenance ) ? stateObj . subdomainsMaintenance . includes ( host ) : false ,
77- bannerSubdomains : Array . isArray ( stateObj . bannerSubdomains ) ? stateObj . bannerSubdomains : [ ] ,
78- bannerMessage : typeof stateObj . bannerMessage === 'string' ? stateObj . bannerMessage : '' ,
79- is4gMode : is4gMode === 'true'
34+ isGlobalMaintenance : stateObj . isGlobalMaintenance ,
35+ subdomainsMaintenance : stateObj . subdomainsMaintenance ,
36+ isSubdomainMaintenance : stateObj . subdomainsMaintenance . includes ( host ) ,
37+ bannerSubdomains : stateObj . bannerSubdomains ,
38+ bannerMessage : stateObj . bannerMessage ,
39+ is4gMode,
8040 } ;
8141}
8242
0 commit comments