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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maxserver",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "Node server setup based fastify",
5
5
  "author": "Max Matinpalo",
6
6
  "type": "module",
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", function () {
6
- const addr = this.server.address();
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 protocol = this.initialConfig.https ? "https" : "http";
12
- const host = getExternalIp() || "localhost";
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.family === "IPv4" && !net.internal) return net.address;
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
@@ -20,6 +20,7 @@ export default async function maxserver(options = {}) {
20
20
 
21
21
  const app = Fastify({
22
22
  trustProxy: true,
23
+ host: "0.0.0.0",
23
24
  https: getHttpsOptions() || undefined,
24
25
 
25
26
  // To allow writing example value fields to schemas for doucumentation
package/templates/env CHANGED
@@ -8,7 +8,7 @@ PORT = 3000
8
8
  # CORS_ORIGIN = https://app.example.com
9
9
 
10
10
  # JWT;
11
- JWT_SECRET = supersecretkey
11
+ # JWT_SECRET = supersecretkey
12
12
 
13
13
  # MongoDB;
14
14
  # MONGODB_URI = mongodb://127.0.0.1:27017/testdb
@@ -1,8 +1,14 @@
1
- // GET /hello
1
+ // POST /hello
2
2
 
3
3
  export default async function handler(req, rep) {
4
4
 
5
- console.log("GET /hello");
5
+ console.log("POST /hello");
6
6
 
7
- return { message: `Hello`, };
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 const schema = {
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: "Create greeting",
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
@@ -1,11 +0,0 @@
1
- // POST /hello
2
-
3
- export default async function handler(req, rep) {
4
-
5
- console.log("POST /hello");
6
-
7
- return {
8
- message: `Hello ${req.body.name}`,
9
- user: req.user || null,
10
- };
11
- }