Fix db not closing
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import mysql from 'mysql2'
|
import mysql from 'mysql2'
|
||||||
import { drizzle } from 'drizzle-orm/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), {
|
||||||
@@ -17,6 +16,7 @@ export function getDatabaseConnection () {
|
|||||||
password: process.env.DB_PASS ?? '',
|
password: process.env.DB_PASS ?? '',
|
||||||
database: process.env.DB_NAME ?? ''
|
database: process.env.DB_NAME ?? ''
|
||||||
})
|
})
|
||||||
|
const db = drizzle(connection)
|
||||||
|
|
||||||
return drizzle(connection)
|
return { connection, db }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { desc, eq } from 'drizzle-orm'
|
|||||||
import { getDatabaseConnection } from '../../lib/util'
|
import { getDatabaseConnection } from '../../lib/util'
|
||||||
|
|
||||||
export async function handler () {
|
export async function handler () {
|
||||||
const db = getDatabaseConnection()
|
const { connection, db } = getDatabaseConnection()
|
||||||
|
|
||||||
const version = await db
|
const version = await db
|
||||||
.select({
|
.select({
|
||||||
@@ -14,5 +14,7 @@ export async function handler () {
|
|||||||
.orderBy(desc(launcherUpdates.place))
|
.orderBy(desc(launcherUpdates.place))
|
||||||
.limit(1)
|
.limit(1)
|
||||||
|
|
||||||
|
connection.end()
|
||||||
|
|
||||||
return version[0].id
|
return version[0].id
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
|
|
||||||
export async function handler (context: Context) {
|
export async function handler (context: Context) {
|
||||||
const db = getDatabaseConnection()
|
const { connection, db } = getDatabaseConnection()
|
||||||
|
|
||||||
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
|
||||||
@@ -20,6 +20,7 @@ export async function handler (context: Context) {
|
|||||||
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 {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ error: 'Unsupported architecture for Windows' },
|
{ error: 'Unsupported architecture for Windows' },
|
||||||
400
|
400
|
||||||
@@ -28,6 +29,7 @@ export async function handler (context: Context) {
|
|||||||
} else if (platform == 'linux') {
|
} else if (platform == 'linux') {
|
||||||
if (arch == 'x86_64') platString = 'linux'
|
if (arch == 'x86_64') platString = 'linux'
|
||||||
else {
|
else {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ error: 'Unsupported architecture for Linux' },
|
{ error: 'Unsupported architecture for Linux' },
|
||||||
400
|
400
|
||||||
@@ -37,12 +39,14 @@ export async function handler (context: Context) {
|
|||||||
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 {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ error: 'Unsupported architecture for macOS' },
|
{ error: 'Unsupported architecture for macOS' },
|
||||||
400
|
400
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
connection.end()
|
||||||
return jsonResponse({ error: 'Unsupported platform' }, 400)
|
return jsonResponse({ error: 'Unsupported platform' }, 400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,5 +92,7 @@ export async function handler (context: Context) {
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
|
connection.end()
|
||||||
|
|
||||||
return jsonResponse(versions[0])
|
return jsonResponse(versions[0])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { getDatabaseConnection, jsonResponse } from '../../lib/util'
|
|||||||
import { Context } from 'elysia'
|
import { Context } from 'elysia'
|
||||||
|
|
||||||
export async function handler (context: Context) {
|
export async function handler (context: Context) {
|
||||||
const db = getDatabaseConnection()
|
const { connection, db } = getDatabaseConnection()
|
||||||
|
|
||||||
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
|
||||||
@@ -20,6 +20,7 @@ export async function handler (context: Context) {
|
|||||||
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 {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{
|
{
|
||||||
message: 'Unsupported architecture for Windows',
|
message: 'Unsupported architecture for Windows',
|
||||||
@@ -32,6 +33,7 @@ export async function handler (context: Context) {
|
|||||||
} else if (platform == 'linux') {
|
} else if (platform == 'linux') {
|
||||||
if (arch == 'x86_64') platString = 'linux'
|
if (arch == 'x86_64') platString = 'linux'
|
||||||
else {
|
else {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{
|
{
|
||||||
message: 'Unsupported architecture for Linux',
|
message: 'Unsupported architecture for Linux',
|
||||||
@@ -44,6 +46,7 @@ export async function handler (context: Context) {
|
|||||||
} 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 {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{
|
{
|
||||||
message: 'Unsupported architecture for macOS',
|
message: 'Unsupported architecture for macOS',
|
||||||
@@ -56,6 +59,7 @@ export async function handler (context: Context) {
|
|||||||
} 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 {
|
||||||
|
connection.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ message: 'Unsupported platform', versions: null, games: null },
|
{ message: 'Unsupported platform', versions: null, games: null },
|
||||||
400
|
400
|
||||||
@@ -119,5 +123,7 @@ export async function handler (context: Context) {
|
|||||||
|
|
||||||
const games = await db.select().from(launcherGames).execute()
|
const games = await db.select().from(launcherGames).execute()
|
||||||
|
|
||||||
|
connection.end()
|
||||||
|
|
||||||
return jsonResponse({ versions, games })
|
return jsonResponse({ versions, games })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user