Skip to content

Commit 3154183

Browse files
committed
feat: Added '@cocreate-server' handle http and https
1 parent dd34f68 commit 3154183

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

src/server.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,59 +47,62 @@ class CoCreateLazyLoader {
4747
});
4848
}
4949

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))
7852

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+
}
8083

81-
await this.acme.checkCertificate(hostname, organization._id, req.url)
84+
hosts[hostname] = organization
8285

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)
9287

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 })
9392
} 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' }));
9596
}
9697

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)
100100
}
101-
})
102101

102+
} catch (error) {
103+
res.writeHead(400, { 'Content-Type': 'text/plain' });
104+
res.end('Invalid host format');
105+
}
103106
}
104107

105108
async executeScriptWithTimeout(name, data) {

0 commit comments

Comments
 (0)