Switch to using a different method of database access

This commit is contained in:
2025-11-08 19:13:23 -07:00
parent 01e0c233d7
commit 3fe9788a4f
9 changed files with 304 additions and 240 deletions

View File

@@ -11,7 +11,7 @@
"mysql2": "3.15.3", "mysql2": "3.15.3",
}, },
"devDependencies": { "devDependencies": {
"bun-types": "1.3.1", "bun-types": "1.3.2",
}, },
}, },
}, },
@@ -32,7 +32,7 @@
"aws-ssl-profiles": ["aws-ssl-profiles@1.1.2", "", {}, "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g=="], "aws-ssl-profiles": ["aws-ssl-profiles@1.1.2", "", {}, "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g=="],
"bun-types": ["bun-types@1.3.1", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-NMrcy7smratanWJ2mMXdpatalovtxVggkj11bScuWuiOoXTiKIu2eVS1/7qbyI/4yHedtsn175n4Sm4JcdHLXw=="], "bun-types": ["bun-types@1.3.2", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-i/Gln4tbzKNuxP70OWhJRZz1MRfvqExowP7U6JKoI8cntFrtxg7RJK3jvz7wQW54UuvNC8tbKHHri5fy74FVqg=="],
"cookie": ["cookie@1.0.2", "", {}, "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA=="], "cookie": ["cookie@1.0.2", "", {}, "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA=="],

View File

@@ -13,7 +13,7 @@
"mysql2": "3.15.3" "mysql2": "3.15.3"
}, },
"devDependencies": { "devDependencies": {
"bun-types": "1.3.1" "bun-types": "1.3.2"
}, },
"module": "src/index.js" "module": "src/index.js"
} }

View File

@@ -1,37 +1,35 @@
import { Elysia } from "elysia"; import { Elysia } from 'elysia'
import { cors } from "@elysiajs/cors" import { cors } from '@elysiajs/cors'
import { jsonResponse } from "./lib/util"; import { jsonResponse } from './lib/util'
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import dotenv from 'dotenv' import dotenv from 'dotenv'
import { handler as launcherVersionsHandler } from "./routes/launcher/versions"; import { handler as launcherVersionsHandler } from './routes/launcher/versions'
import { handler as launcherLatestHandler } from "./routes/launcher/latest"; import { handler as launcherLatestHandler } from './routes/launcher/latest'
import { handler as launcherLoaderLatestHandler } from "./routes/launcher/loader/latest"; import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader/latest'
import { handler as launcherLoaderUpdateDataHandler } from "./routes/launcher/loader/update-data"; import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data'
dotenv.config() dotenv.config()
const connection = await mysql.createConnection({ const app = new Elysia().use(
host: process.env.DB_HOST ?? 'localhost', cors({
port: Number(process.env.DB_PORT) || 3306, origin: '*',
user: process.env.DB_USER ?? '', methods: ['POST', 'GET']
password: process.env.DB_PASS ?? '', })
database: process.env.DB_NAME ?? '' )
});
const db = drizzle(connection); app.get('/launcher/versions', context => launcherVersionsHandler(context))
app.get('/launcher/latest', launcherLatestHandler)
const app = new Elysia() app.get('/launcher/loader/latest', launcherLoaderLatestHandler)
.use(cors({ app.get('/launcher/loader/update-data', context =>
origin: "*", launcherLoaderUpdateDataHandler(context)
methods: ["POST", "GET"] )
})) app.all('*', () =>
jsonResponse(
app.get("/launcher/versions", (context) => launcherVersionsHandler(context, db)) {
app.get("/launcher/latest", () => launcherLatestHandler(db)) message: 'No endpoint found (are you using the correct request method?)'
app.get("/launcher/loader/latest", launcherLoaderLatestHandler) },
app.get("/launcher/loader/update-data", (context) => launcherLoaderUpdateDataHandler(context, db)) 404
app.all("*", () => jsonResponse({ message: "No endpoint found (are you using the correct request method?)" }, 404)) )
)
app.listen(3342) app.listen(3342)

View File

@@ -1,4 +1,11 @@
import { bigint, boolean, int, mysqlTable, text, varchar } from "drizzle-orm/mysql-core"; import {
bigint,
boolean,
int,
mysqlTable,
text,
varchar
} from 'drizzle-orm/mysql-core'
export const launcherGames = mysqlTable('launchergames', { export const launcherGames = mysqlTable('launchergames', {
id: int('id').primaryKey().autoincrement(), id: int('id').primaryKey().autoincrement(),
@@ -17,10 +24,13 @@ export const launcherVersions = mysqlTable('launcherversions', {
platforms: text('platforms').notNull(), platforms: text('platforms').notNull(),
executables: text('executables').notNull(), executables: text('executables').notNull(),
hidden: boolean('hidden').notNull().default(false), hidden: boolean('hidden').notNull().default(false),
game: int('game').notNull().default(0).references(() => launcherGames.id), game: int('game')
.notNull()
.default(0)
.references(() => launcherGames.id),
place: int('place').notNull().default(0), place: int('place').notNull().default(0),
sha512sums: text('sha512sums').notNull().default("[]"), sha512sums: text('sha512sums').notNull().default('[]'),
sizes: text('sizes').notNull().default("[]") sizes: text('sizes').notNull().default('[]')
}) })
export const launcherUpdates = mysqlTable('launcherupdates', { export const launcherUpdates = mysqlTable('launcherupdates', {
@@ -31,5 +41,5 @@ export const launcherUpdates = mysqlTable('launcherupdates', {
executables: text('executables').notNull(), executables: text('executables').notNull(),
hidden: boolean('hidden').notNull().default(false), hidden: boolean('hidden').notNull().default(false),
place: int('place').notNull().default(0), place: int('place').notNull().default(0),
sha512sums: text('sha512sums').notNull().default("[]") sha512sums: text('sha512sums').notNull().default('[]')
}) })

View File

@@ -1,6 +1,22 @@
import mysql from 'mysql2'
import { drizzle } from 'drizzle-orm/mysql2'
import { Connection } from 'mysql2/typings/mysql/lib/Connection'
export function jsonResponse (data: any, status = 200) { export function jsonResponse (data: any, status = 200) {
return new Response(JSON.stringify(data, null, 2), { return new Response(JSON.stringify(data, null, 2), {
status, status,
headers: { "Content-Type": "application/json" } headers: { 'Content-Type': 'application/json' }
}) })
} }
export function getDatabaseConnection () {
const connection = mysql.createConnection({
host: process.env.DB_HOST ?? 'localhost',
port: Number(process.env.DB_PORT) || 3306,
user: process.env.DB_USER ?? '',
password: process.env.DB_PASS ?? '',
database: process.env.DB_NAME ?? ''
})
return drizzle(connection)
}

View File

@@ -1,9 +1,12 @@
import { MySql2Database } from "drizzle-orm/mysql2" import { launcherUpdates } from '../../lib/tables'
import { launcherUpdates } from "../../lib/tables" import { desc, eq } from 'drizzle-orm'
import { desc, eq } from "drizzle-orm" import { getDatabaseConnection } from '../../lib/util'
export async function handler(db: MySql2Database) { export async function handler () {
const version = await db.select({ const db = getDatabaseConnection()
const version = await db
.select({
id: launcherUpdates.id id: launcherUpdates.id
}) })
.from(launcherUpdates) .from(launcherUpdates)

View File

@@ -1,3 +1,3 @@
export async function handler () { export async function handler () {
return "1.0.1" return '1.0.1'
} }

View File

@@ -1,10 +1,11 @@
import { MySql2Database } from "drizzle-orm/mysql2"; import { launcherUpdates } from '../../../lib/tables'
import { launcherUpdates } from "../../../lib/tables"; import { desc, eq } from 'drizzle-orm'
import { desc, eq } from "drizzle-orm"; import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
import { jsonResponse } from "../../../lib/util"; import { Context } from 'elysia'
import { Context } from "elysia";
export async function handler (context: Context) {
const db = getDatabaseConnection()
export async function handler(context: Context, db: MySql2Database) {
const platform = context.query.platform as string | undefined const platform = context.query.platform as string | undefined
const arch = context.query.arch as string | undefined const arch = context.query.arch as string | undefined
let showAll = false let showAll = false
@@ -15,29 +16,39 @@ export async function handler(context: Context, db: MySql2Database) {
let platString = null let platString = null
if (!showAll) { if (!showAll) {
if (platform == "windows") { if (platform == 'windows') {
if (arch == "x86_64") platString = "windows" if (arch == 'x86_64') platString = 'windows'
else if (arch == "aarch64") platString = "windows-arm64" else if (arch == 'aarch64') platString = 'windows-arm64'
else { else {
return jsonResponse({ error: "Unsupported architecture for Windows" }, 400) return jsonResponse(
{ error: 'Unsupported architecture for Windows' },
400
)
} }
} else if (platform == "linux") { } else if (platform == 'linux') {
if (arch == "x86_64") platString = "linux" if (arch == 'x86_64') platString = 'linux'
else { else {
return jsonResponse({ error: "Unsupported architecture for Linux" }, 400) return jsonResponse(
{ error: 'Unsupported architecture for Linux' },
400
)
} }
} else if (platform == "macos") { } else if (platform == 'macos') {
if (arch == "x86_64") platString = "macos-intel" if (arch == 'x86_64') platString = 'macos-intel'
else if (arch == "aarch64") platString = "macos-silicon" else if (arch == 'aarch64') platString = 'macos-silicon'
else { else {
return jsonResponse({ error: "Unsupported architecture for macOS" }, 400) return jsonResponse(
{ error: 'Unsupported architecture for macOS' },
400
)
} }
} else { } else {
return jsonResponse({ error: "Unsupported platform" }, 400) return jsonResponse({ error: 'Unsupported platform' }, 400)
} }
} }
const versionsRaw = await db.select({ const versionsRaw = await db
.select({
id: launcherUpdates.id, id: launcherUpdates.id,
releaseDate: launcherUpdates.releaseDate, releaseDate: launcherUpdates.releaseDate,
downloadUrls: launcherUpdates.downloadUrls, downloadUrls: launcherUpdates.downloadUrls,
@@ -50,7 +61,8 @@ export async function handler(context: Context, db: MySql2Database) {
.limit(1) .limit(1)
.execute() .execute()
const versions = versionsRaw.map(v => ({ const versions = versionsRaw
.map(v => ({
...v, ...v,
downloadUrls: JSON.parse(v.downloadUrls), downloadUrls: JSON.parse(v.downloadUrls),
platforms: JSON.parse(v.platforms), platforms: JSON.parse(v.platforms),

View File

@@ -1,10 +1,11 @@
import { MySql2Database } from "drizzle-orm/mysql2"; import { launcherGames, launcherVersions } from '../../lib/tables'
import { launcherGames, launcherVersions } from "../../lib/tables"; import { asc, desc, eq } from 'drizzle-orm'
import { asc, desc, eq } from "drizzle-orm"; import { getDatabaseConnection, jsonResponse } from '../../lib/util'
import { jsonResponse } from "../../lib/util"; import { Context } from 'elysia'
import { Context } from "elysia";
export async function handler (context: Context) {
const db = getDatabaseConnection()
export async function handler(context: Context, db: MySql2Database) {
const platform = context.query.platform as string | undefined const platform = context.query.platform as string | undefined
const arch = context.query.arch as string | undefined const arch = context.query.arch as string | undefined
let showAll = false let showAll = false
@@ -15,30 +16,55 @@ export async function handler(context: Context, db: MySql2Database) {
let platString = null let platString = null
if (!showAll) { if (!showAll) {
if (platform == "windows") { if (platform == 'windows') {
if (arch == "x86_64") platString = "windows" if (arch == 'x86_64') platString = 'windows'
else if (arch == "aarch64") platString = "windows-arm64" else if (arch == 'aarch64') platString = 'windows-arm64'
else { else {
return jsonResponse({ message: "Unsupported architecture for Windows", versions: null, games: null }, 400) return jsonResponse(
{
message: 'Unsupported architecture for Windows',
versions: null,
games: null
},
400
)
} }
} else if (platform == "linux") { } else if (platform == 'linux') {
if (arch == "x86_64") platString = "linux" if (arch == 'x86_64') platString = 'linux'
else { else {
return jsonResponse({ message: "Unsupported architecture for Linux", versions: null, games: null }, 400) return jsonResponse(
{
message: 'Unsupported architecture for Linux',
versions: null,
games: null
},
400
)
} }
} else if (platform == "macos") { } else if (platform == 'macos') {
if (arch == "x86_64" || arch == "aarch64") platString = "macos" if (arch == 'x86_64' || arch == 'aarch64') platString = 'macos'
else { else {
return jsonResponse({ message: "Unsupported architecture for macOS", versions: null, games: null }, 400) return jsonResponse(
{
message: 'Unsupported architecture for macOS',
versions: null,
games: null
},
400
)
} }
} else if (platform == "android") platString = "android" } else if (platform == 'android') platString = 'android'
else if (platform == "ios") platString = "ios" else if (platform == 'ios') platString = 'ios'
else { else {
return jsonResponse({ message: "Unsupported platform", versions: null, games: null }, 400) return jsonResponse(
{ message: 'Unsupported platform', versions: null, games: null },
400
)
} }
} }
const versionsRaw = await db.select({ const versionsRaw = await db
.select({
id: launcherVersions.id, id: launcherVersions.id,
versionName: launcherVersions.versionName, versionName: launcherVersions.versionName,
releaseDate: launcherVersions.releaseDate, releaseDate: launcherVersions.releaseDate,
@@ -48,15 +74,14 @@ export async function handler(context: Context, db: MySql2Database) {
executables: launcherVersions.executables, executables: launcherVersions.executables,
sha512sums: launcherVersions.sha512sums, sha512sums: launcherVersions.sha512sums,
sizes: launcherVersions.sizes sizes: launcherVersions.sizes
}).from(launcherVersions) })
.from(launcherVersions)
.where(eq(launcherVersions.hidden, false)) .where(eq(launcherVersions.hidden, false))
.orderBy( .orderBy(asc(launcherVersions.game), desc(launcherVersions.place))
asc(launcherVersions.game),
desc(launcherVersions.place)
)
.execute() .execute()
const versions = versionsRaw.map(v => ({ const versions = versionsRaw
.map(v => ({
...v, ...v,
downloadUrls: JSON.parse(v.downloadUrls), downloadUrls: JSON.parse(v.downloadUrls),
platforms: JSON.parse(v.platforms), platforms: JSON.parse(v.platforms),