From fc7b835d681b03b3c70da9dd6f4f953199b20611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B3=E5=85=83=E7=9A=93?= Date: Sun, 18 May 2025 16:59:03 +0800 Subject: [PATCH] Made a bunch of changes to the source code. And polish the deploying process. --- docker-compose.yml | 11 ++++--- server/api/health.ts | 7 +++++ server/api/search/news.ts | 3 ++ server/api/user/checkcookie.ts | 19 ++++++++++++- server/api/user/login.ts | 52 ++++++++++++++++++++++++---------- 5 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 server/api/health.ts create mode 100644 server/api/search/news.ts diff --git a/docker-compose.yml b/docker-compose.yml index 58f0342..4ced30e 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,21 @@ services: newsanalyze-service: - image: ghcr.io/hpware/news-analyze:latest + image: ghcr.io/hpware/news-analyze:master healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:3000/health"] + test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"] interval: 1m timeout: 10s retries: 3 networks: - - app-network + - web labels: - "traefik.enable=true" - "traefik.http.routers.newsanalyze.rule=Host(`news.yuanhau.com`)" - - "traefik.http.routers.newsanalyze.entrypoints=webinternal" + - "traefik.http.routers.newsanalyze.entrypoints=websecure" + - "traefik.http.routers.newsanalyze.tls=true" + - "traefik.http.routers.newsanalyze.tls.certresolver=myresolver" - "traefik.http.services.newsanalyze.loadbalancer.server.port=3000" + restart: unless-stopped networks: web: diff --git a/server/api/health.ts b/server/api/health.ts new file mode 100644 index 0000000..277a99c --- /dev/null +++ b/server/api/health.ts @@ -0,0 +1,7 @@ +export default defineEventHandler(async () => { + return { + status: "healthy", + timestamp: new Date().toISOString(), + version: process.env.npm_package_version || "unknown", + }; +}); diff --git a/server/api/search/news.ts b/server/api/search/news.ts new file mode 100644 index 0000000..7636adf --- /dev/null +++ b/server/api/search/news.ts @@ -0,0 +1,3 @@ +export default defineEventHandler(async (event) => { + return {}; +}); diff --git a/server/api/user/checkcookie.ts b/server/api/user/checkcookie.ts index ef4a53f..1c021bb 100644 --- a/server/api/user/checkcookie.ts +++ b/server/api/user/checkcookie.ts @@ -1,5 +1,5 @@ // This should be hooked up to a database soon. -import postgres from "~/server/components/postgres"; +import sql from "~/server/components/postgres"; // Parse Date Function function checkDate(dateString: string) { @@ -40,8 +40,25 @@ export default defineEventHandler(async (event) => { path: "/", }); } + if (!loginCookie) { + setCookie(event, "lastCheckCookie", nowDate, { + httpOnly: true, + secure: process.env.NODE_ENV === "production", + path: "/", + }); + return { + auth: false, + user: null, + }; + } + const loginCookieStore = atob(loginCookie); + /*const findUser = sql` + select * from userlogintokens + where token = ${loginCookieStore} + `;*/ return { auth: true, user: "testing", + loginCookie: loginCookieStore, // Debug }; }); diff --git a/server/api/user/login.ts b/server/api/user/login.ts index f45f4a9..7210064 100644 --- a/server/api/user/login.ts +++ b/server/api/user/login.ts @@ -1,34 +1,56 @@ import sql from "~/server/components/postgres"; +import { v4 as uuidv4 } from "uuid"; import argon2 from "argon2"; export default defineEventHandler(async (event) => { const salt = process.env.PASSWORD_HASH_SALT; if (!salt) { - throw createError({ - statusCode: 500, - message: "Internal server error", - }); + return { + error: "SALT_NOT_FOUND", + }; } const body = await readBody(event); const { username, password } = body; if (!username || !password) { - throw createError({ - statusCode: 400, - message: "Username and password are required", - }); + return { + error: "NO_USER_AND_PASSWORD_SUBMITED", + }; } const USERNAME_PATTERN = /^[a-zA-Z0-9_]{3,20}$/; if (!USERNAME_PATTERN.test(username)) { - throw createError({ - statusCode: 400, - message: "Invalid username.", - }); + return { + error: "INVALD_USER_ACCOUNT", + }; } // Server side hashing const hashedPassword = await argon2.hash(salt, password); // Check if user exists, if not, create a user try { - console.log(username); - console.log(hashedPassword); - } catch (e) {} + const fetchUserInfo = await sql` + select * from users + where user = ${username}`; + if (!fetchUserInfo) { + /*const createNewUser = await sql` + insert + `*/ + // INSERT USER CREATING STUFF HERE LATER + } else { + if (fetchUserInfo.password !== hashedPassword) { + return { + error: "PASSWORD_NO_MATCH", + }; + } else { + const newToken = uuidv4(); + const newToken64 = atob(newToken); + const saveNewToken = await sql``; + return { + user: fetchUserInfo.user, + }; + } + } + } catch (e) { + return { + error: "UNABLE_TO_PROCESS", + }; + } });