maxserver 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/getAddress.js +19 -11
- package/src/index.js +1 -0
- package/templates/env +1 -1
- package/templates/src/hello.js +10 -4
- package/templates/src/{greet.schema.js → hello.schema.js} +10 -8
- package/templates/src/welcome.js +14 -0
- package/templates/src/greet.js +0 -11
package/package.json
CHANGED
package/src/getAddress.js
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
|
-
import os from "os";
|
|
2
|
-
|
|
1
|
+
import os from "node:os";
|
|
3
2
|
|
|
4
3
|
export function setupGetAddress(app) {
|
|
5
|
-
app.decorate("getAddress",
|
|
6
|
-
const addr =
|
|
4
|
+
app.decorate("getAddress", () => {
|
|
5
|
+
const addr = app.server?.address();
|
|
6
|
+
const protocol = app.initialConfig?.https ? "https" : "http";
|
|
7
7
|
|
|
8
8
|
if (!addr) return null;
|
|
9
9
|
if (typeof addr === "string") return addr;
|
|
10
10
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
11
|
+
const isPublicBind = addr.address === "0.0.0.0" || addr.address === "::";
|
|
12
|
+
const isLoopback = addr.address === "127.0.0.1" || addr.address === "::1";
|
|
13
|
+
|
|
14
|
+
const envIp = String(process.env.PUBLIC_IP || "").trim() || null;
|
|
15
|
+
const detectedIp = envIp || getLanIp();
|
|
16
|
+
|
|
17
|
+
const ip = (isPublicBind && !isLoopback)
|
|
18
|
+
? (detectedIp || addr.address)
|
|
19
|
+
: "localhost";
|
|
20
|
+
|
|
21
|
+
const host = ip.includes(":") ? `[${ip}]` : ip;
|
|
13
22
|
|
|
14
23
|
return `${protocol}://${host}:${addr.port}`;
|
|
15
24
|
});
|
|
16
25
|
}
|
|
17
26
|
|
|
18
|
-
|
|
19
|
-
function getExternalIp() {
|
|
27
|
+
function getLanIp() {
|
|
20
28
|
const nets = os.networkInterfaces();
|
|
21
29
|
|
|
22
30
|
for (const name of Object.keys(nets)) {
|
|
23
|
-
for (const net of nets[name]) {
|
|
24
|
-
if (net
|
|
31
|
+
for (const net of (nets[name] || [])) {
|
|
32
|
+
if (net?.family === "IPv4" && !net.internal) return net.address;
|
|
25
33
|
}
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
return null;
|
|
29
|
-
}
|
|
37
|
+
}
|
package/src/index.js
CHANGED
package/templates/env
CHANGED
package/templates/src/hello.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
//
|
|
1
|
+
// POST /hello
|
|
2
2
|
|
|
3
3
|
export default async function handler(req, rep) {
|
|
4
4
|
|
|
5
|
-
console.log("
|
|
5
|
+
console.log("POST /hello");
|
|
6
6
|
|
|
7
|
-
return {
|
|
8
|
-
}
|
|
7
|
+
return {
|
|
8
|
+
message: `Hello ${req.body.name} 🙋♂️`,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// Try POST with and without name, to see how the schema works
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
export
|
|
1
|
+
export default {
|
|
2
|
+
|
|
3
|
+
// These 3 fields are for the documentation
|
|
4
|
+
// Not must have, but your auto generated documentation will be great
|
|
5
|
+
|
|
2
6
|
tags: ["Test"],
|
|
3
|
-
summary: "
|
|
7
|
+
summary: "Post hello",
|
|
4
8
|
description: "Accepts a name and returns a greeting.",
|
|
5
9
|
|
|
6
10
|
body: {
|
|
@@ -22,13 +26,11 @@ export const schema = {
|
|
|
22
26
|
type: "string",
|
|
23
27
|
example: "Hello Max",
|
|
24
28
|
},
|
|
25
|
-
user: {
|
|
26
|
-
type: "object",
|
|
27
|
-
example: {
|
|
28
|
-
userId: "64f1c2e9b1a2c3d4e5f67890",
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
29
|
},
|
|
32
30
|
},
|
|
33
31
|
},
|
|
34
32
|
};
|
|
33
|
+
|
|
34
|
+
// Hint - You don't need to write these ourself
|
|
35
|
+
// Just ask chat gpt or gemini to generate them
|
|
36
|
+
// In docs you will find little instruction for it
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// GET /welcome
|
|
2
|
+
|
|
3
|
+
export default async function handler(req, rep) {
|
|
4
|
+
|
|
5
|
+
console.log("GET /welcome");
|
|
6
|
+
return {
|
|
7
|
+
message: "Weclome to maxserver 😉",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// Remember the very first line of the file must be the ROUTE COMMENT
|
|
14
|
+
// other imports if needed after it
|