| Server IP : 159.69.118.108 / Your IP : 216.73.216.200 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu-4gb-fsn1-1 6.8.0-90-generic #91-Ubuntu SMP PREEMPT_DYNAMIC Tue Nov 18 13:53:54 UTC 2025 aarch64 User : root ( 0) PHP Version : 8.2.32 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/lauthiamkok.net/redis.lauthiamkok.net/dist/ |
Upload File : |
import http from "http";
import fs from "fs";
import findmyway from "find-my-way";
import { createClient } from "redis";
import url from "url";
const ctxHandler = (callback) => {
return async (ctx) => {
let data = null;
let statusCode = 200;
try {
data = await callback(ctx);
} catch (e) {
statusCode = e.statusCode || 500;
data = {
name: e.name,
message: e.message,
stack: e.stack
};
}
data = JSON.stringify(data);
return {
statusCode,
data
};
};
};
const index = ctxHandler((ctx) => {
return {
message: ctx.message
};
});
async function connect() {
const client2 = createClient({
// username: 'default', // use your Redis user. More info https://redis.io/docs/management/security/acl/
// password: 'secret', // use your password here
socket: {
// host: 'my-redis.cloud.redislabs.com',
// port: 6379,
// tls: false,
// key: readFileSync('./redis_user_private.key'),
// cert: readFileSync('./redis_user.crt'),
// ca: [readFileSync('./redis_ca.pem')]
}
});
client2.on("error", (err) => console.log("Redis Client Error", err));
client2.on("end", () => console.log("Redis connection ended"));
client2.on("connect", () => console.log("Redis connected"));
await client2.connect();
return client2;
}
const useRedis = () => {
return {
connect
};
};
const cartByKey = ctxHandler(async (ctx) => {
const key = ctx.params.key;
if (!key) {
return null;
}
const { connect: connect2 } = useRedis();
const client2 = await connect2();
const data = JSON.parse(await client2.get(key));
client2.quit();
return data;
});
const isJson = (str) => {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
};
const normalizeBody = async (req) => {
var _a;
let body = [];
const requestMethods = ["PATCH", "POST", "PUT", "DELETE"];
if (requestMethods.includes(req.method)) {
for await (const chunk of req) {
body += chunk;
}
if ((_a = req.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) {
body = isJson(body) ? JSON.parse(body) : body;
}
}
return body;
};
const useCookie = (options) => {
function get(name, cookie) {
const resource = cookie ?? document.cookie;
let string = resource.match(`(?:(?:^|.*; *)${name} *= *([^;]*).*$)|^.*$`)[1];
if (string) {
string = decodeURIComponent(string);
}
if (string === "undefined" || string === void 0 || string === "") {
string = null;
}
return JSON.parse(string);
}
function set(name, value) {
let maxAge = 0;
if (options.maxAge) {
maxAge = options.maxAge;
}
if (options.days) {
maxAge = options.days * 60 * 60 * 24;
}
const domain = options.domain ?? "localhost";
document.cookie = `${name}=${encodeURIComponent(value)}; Domain=${domain}; max-age=${maxAge}; path=/`;
}
function drop(name) {
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
}
function observe(ref, name) {
watch(ref, (newVal, prevVal) => {
set(name, JSON.stringify(ref.value));
}, {
// Force deep traversal of the source if it is an object, so that the
// callback fires on deep mutations.
deep: true
});
}
return {
get,
set,
drop,
observe
};
};
const cartSet = ctxHandler(async (ctx) => {
const namespace = "lauthiamok.net:cart:1";
const { get } = useCookie();
const urlParsed = url.parse(ctx.req.url, true);
const cookieValue = urlParsed.query.k ?? null;
const key = cookieValue ? `${namespace}:${cookieValue}` : null;
if (!key) {
return null;
}
const body = await normalizeBody(ctx.req);
const { connect: connect2 } = useRedis();
const client2 = await connect2();
const days = 30;
const expire = days * 24 * 60 * 60;
const result = await client2.set(key, JSON.stringify(body), { "EX": expire });
client2.quit();
return result;
});
const errorByKey = ctxHandler(async (ctx) => {
const key = ctx.params.key;
if (!key) {
return null;
}
const { connect: connect2 } = useRedis();
const client2 = await connect2();
return JSON.parse(await client2.get(key));
});
const errorSet = ctxHandler(async (ctx) => {
const namespace = "lauthiamok.net:error";
const cookieValue = ctx.req.headers["x-cookie-value"] ?? null;
const key = cookieValue ? `${namespace}:${cookieValue}` : null;
if (!key) {
return null;
}
const body = await normalizeBody(ctx.req);
const { connect: connect2 } = useRedis();
const clients = await connect2();
const expire = 5 * 60;
return await client.set(key, JSON.stringify(body), { "EX": expire });
});
const errorDel = ctxHandler(async (ctx) => {
const namespace = "lauthiamok.net:error";
const cookieValue = ctx.req.headers["x-cookie-value"] ?? null;
const key = cookieValue ? `${namespace}:${cookieValue}` : null;
if (!key) {
return null;
}
const { connect: connect2 } = useRedis();
const clients = await connect2();
return await client.del(key);
});
const router = findmyway();
router.on("GET", "/", async (req, res) => {
const ctx = {
req,
message: "Hello World!!"
};
const { data, statusCode } = await index(ctx);
res.statusCode = statusCode;
res.end(data);
});
router.on("GET", "/carts/:key", async (req, res, params) => {
const ctx = { req, params };
const { data, statusCode } = await cartByKey(ctx);
res.statusCode = statusCode;
res.end(data);
});
router.on("POST", "/carts/set", async (req, res) => {
const ctx = { req };
const { data, statusCode } = await cartSet(ctx);
res.statusCode = statusCode;
res.end(data);
});
router.on("GET", "/errors/:key", async (req, res, params) => {
const ctx = { req, params };
const { data, statusCode } = await errorByKey(ctx);
res.statusCode = statusCode;
res.end(data);
});
router.on("POST", "/errors/set", async (req, res) => {
const ctx = { req };
const { data, statusCode } = await errorSet(ctx);
res.statusCode = statusCode;
res.end(data);
});
router.on("POST", "/errors/del", async (req, res) => {
const ctx = { req };
const { data, statusCode } = await errorDel(ctx);
res.statusCode = statusCode;
res.end(data);
});
router.on("GET", "/public/*", (req, res) => {
fs.readFile(`./${decodeURIComponent(req.url)}`, (err, data) => {
if (err) {
res.statusCode = 404;
res.end(JSON.stringify({
message: "File not found or you made an invalid request.",
data: err
}));
return;
}
res.setHeader("Content-Type", "text/plain");
res.end(data);
});
});
router.on("GET", "*", (req, res) => {
res.statusCode = 404;
res.end('{"message":"page not found!"}');
});
const requestListener = (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "https://lauthiamkok.net");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials, X-Cookie-Value");
res.setHeader("Access-Control-Allow-Credentials", true);
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
if (req.method === "OPTIONS") {
res.end();
} else {
router.lookup(req, res);
}
};
{
const host = "localhost";
const port = "7000";
const server = http.createServer(requestListener);
server.listen(port, () => {
console.log(`🚀 Server ready at ${host}:${port}`);
});
}
const viteNodeApp = requestListener;
export {
viteNodeApp
};