Made a bunch of changes to the source code. And polish the deploying

process.
This commit is contained in:
yuanhau 2025-05-18 16:59:03 +08:00
parent 1200505451
commit fc7b835d68
5 changed files with 72 additions and 20 deletions

View file

@ -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:

7
server/api/health.ts Normal file
View file

@ -0,0 +1,7 @@
export default defineEventHandler(async () => {
return {
status: "healthy",
timestamp: new Date().toISOString(),
version: process.env.npm_package_version || "unknown",
};
});

View file

@ -0,0 +1,3 @@
export default defineEventHandler(async (event) => {
return {};
});

View file

@ -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
};
});

View file

@ -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",
};
}
});