@@ -47,59 +47,62 @@ class CoCreateLazyLoader {
47
47
} ) ;
48
48
}
49
49
50
- this . server . on ( 'request' , async ( req , res ) => {
51
- try {
52
- const valideUrl = new URL ( `http://${ req . headers . host } ${ req . url } ` ) ;
53
- const hostname = valideUrl . hostname ;
54
-
55
- let organization = hosts [ hostname ] ;
56
- if ( ! organization ) {
57
- let org = await this . crud . send ( {
58
- method : 'object.read' ,
59
- array : 'organizations' ,
60
- $filter : {
61
- query : [
62
- { key : "host" , value : [ hostname ] , operator : "$in" }
63
- ]
64
- } ,
65
- organization_id : process . env . organization_id
66
- } )
67
-
68
- if ( ! org || ! org . object || ! org . object [ 0 ] ) {
69
- // TODO: hostNotFound is not defined
70
- if ( ! hostNotFound )
71
- hostNotFound = await getDefaultFile ( '/hostNotFound.html' )
72
- return sendResponse ( hostNotFound . object [ 0 ] . src , 404 , { 'Content-Type' : 'text/html' , 'storage' : organization . storage } )
73
- } else {
74
- organization = org . object [ 0 ]
75
- organizations [ organization . _id ] = organization
76
- }
77
- }
50
+ this . server . https . on ( 'request' , ( req , res ) => this . request ( req , res ) )
51
+ this . server . http . on ( 'request' , ( req , res ) => this . request ( req , res ) )
78
52
79
- hosts [ hostname ] = organization
53
+ }
54
+
55
+ async request ( req , res ) {
56
+ try {
57
+ const valideUrl = new URL ( `http://${ req . headers . host } ${ req . url } ` ) ;
58
+ const hostname = valideUrl . hostname ;
59
+
60
+ let organization = hosts [ hostname ] ;
61
+ if ( ! organization ) {
62
+ let org = await this . crud . send ( {
63
+ method : 'object.read' ,
64
+ array : 'organizations' ,
65
+ $filter : {
66
+ query : [
67
+ { key : "host" , value : [ hostname ] , operator : "$in" }
68
+ ]
69
+ } ,
70
+ organization_id : process . env . organization_id
71
+ } )
72
+
73
+ if ( ! org || ! org . object || ! org . object [ 0 ] ) {
74
+ // TODO: hostNotFound is not defined
75
+ if ( ! hostNotFound )
76
+ hostNotFound = await getDefaultFile ( '/hostNotFound.html' )
77
+ return sendResponse ( hostNotFound . object [ 0 ] . src , 404 , { 'Content-Type' : 'text/html' , 'storage' : organization . storage } )
78
+ } else {
79
+ organization = org . object [ 0 ]
80
+ organizations [ organization . _id ] = organization
81
+ }
82
+ }
80
83
81
- await this . acme . checkCertificate ( hostname , organization . _id , req . url )
84
+ hosts [ hostname ] = organization
82
85
83
- if ( valideUrl . pathname . startsWith ( '/webhooks/' ) ) {
84
- let name = req . url . split ( '/' ) [ 2 ] ; // Assuming URL structure is /webhook/name/...
85
- if ( this . modules [ name ] ) {
86
- this . executeScriptWithTimeout ( name , { req, res, organization, valideUrl, organization_id : organization . _id } )
87
- } else {
88
- // Handle unknown module or missing webhook method
89
- res . writeHead ( 404 , { 'Content-Type' : 'application/json' } ) ;
90
- res . end ( JSON . stringify ( { error : 'Not found' } ) ) ;
91
- }
86
+ await this . acme . checkCertificate ( hostname , organization . _id , req . url )
92
87
88
+ if ( valideUrl . pathname . startsWith ( '/webhooks/' ) ) {
89
+ let name = req . url . split ( '/' ) [ 2 ] ; // Assuming URL structure is /webhook/name/...
90
+ if ( this . modules [ name ] ) {
91
+ this . executeScriptWithTimeout ( name , { req, res, organization, valideUrl, organization_id : organization . _id } )
93
92
} else {
94
- this . files . send ( req , res , this . crud , organization , valideUrl )
93
+ // Handle unknown module or missing webhook method
94
+ res . writeHead ( 404 , { 'Content-Type' : 'application/json' } ) ;
95
+ res . end ( JSON . stringify ( { error : 'Not found' } ) ) ;
95
96
}
96
97
97
- } catch ( error ) {
98
- res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } ) ;
99
- res . end ( 'Invalid host format' ) ;
98
+ } else {
99
+ this . files . send ( req , res , this . crud , organization , valideUrl )
100
100
}
101
- } )
102
101
102
+ } catch ( error ) {
103
+ res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } ) ;
104
+ res . end ( 'Invalid host format' ) ;
105
+ }
103
106
}
104
107
105
108
async executeScriptWithTimeout ( name , data ) {
0 commit comments