fixed config.json issues 😭
This commit is contained in:
parent
9ec514dcf2
commit
bc05b45d00
9 changed files with 39 additions and 42 deletions
|
|
@ -1,32 +1,20 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { readFile, writeFile } from 'node:fs/promises'
|
import { writeFile } from 'node:fs/promises'
|
||||||
import path from 'node:path'
|
import { CONFIG_PATH, readConfig } from '@/utils/config'
|
||||||
|
|
||||||
export const runtime = 'nodejs'
|
export const runtime = 'nodejs'
|
||||||
|
|
||||||
type ConfigData = {
|
type ConfigData = Awaited<ReturnType<typeof readConfig>>
|
||||||
meetId: string
|
|
||||||
csvFileName: string
|
|
||||||
authString: string
|
|
||||||
adminPassword: string
|
|
||||||
competitionName: string
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getConfig(): Promise<ConfigData> {
|
|
||||||
const configPath = path.join(process.cwd(), 'src', 'config', 'config.json')
|
|
||||||
const content = await readFile(configPath, 'utf8')
|
|
||||||
return JSON.parse(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
const password = req.headers.get('x-admin-password') || ''
|
const password = req.headers.get('x-admin-password') || ''
|
||||||
const config = await getConfig()
|
const config = await readConfig()
|
||||||
if (password !== config.adminPassword) {
|
if (password !== config.adminPassword) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const config = await getConfig()
|
const config = await readConfig()
|
||||||
return NextResponse.json(config)
|
return NextResponse.json(config)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return NextResponse.json({ error: 'Failed to read config' }, { status: 500 })
|
return NextResponse.json({ error: 'Failed to read config' }, { status: 500 })
|
||||||
|
|
@ -35,7 +23,7 @@ export async function GET(req: NextRequest) {
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
const password = req.headers.get('x-admin-password') || ''
|
const password = req.headers.get('x-admin-password') || ''
|
||||||
const config = await getConfig()
|
const config = await readConfig()
|
||||||
if (password !== config.adminPassword) {
|
if (password !== config.adminPassword) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +35,6 @@ export async function POST(req: NextRequest) {
|
||||||
return NextResponse.json({ error: 'All fields are required' }, { status: 400 })
|
return NextResponse.json({ error: 'All fields are required' }, { status: 400 })
|
||||||
}
|
}
|
||||||
|
|
||||||
const configPath = path.join(process.cwd(), 'src', 'config', 'config.json')
|
|
||||||
const newConfig: ConfigData = {
|
const newConfig: ConfigData = {
|
||||||
meetId: body.meetId,
|
meetId: body.meetId,
|
||||||
csvFileName: body.csvFileName,
|
csvFileName: body.csvFileName,
|
||||||
|
|
@ -56,7 +43,7 @@ export async function POST(req: NextRequest) {
|
||||||
competitionName: body.competitionName
|
competitionName: body.competitionName
|
||||||
}
|
}
|
||||||
|
|
||||||
await writeFile(configPath, JSON.stringify(newConfig, null, 2))
|
await writeFile(CONFIG_PATH, JSON.stringify(newConfig, null, 2))
|
||||||
return NextResponse.json({ success: true, message: '✓ Configuração atualizada com sucesso!' })
|
return NextResponse.json({ success: true, message: '✓ Configuração atualizada com sucesso!' })
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return NextResponse.json({ error: `Failed to save config: ${e.message}` }, { status: 500 })
|
return NextResponse.json({ error: `Failed to save config: ${e.message}` }, { status: 500 })
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { getLifterStats } from '@/services/adminStats'
|
import { getLifterStats } from '@/services/adminStats'
|
||||||
import config from '@/config/config.json'
|
import { readConfig } from '@/utils/config'
|
||||||
|
|
||||||
export const runtime = 'nodejs'
|
export const runtime = 'nodejs'
|
||||||
|
|
||||||
|
|
@ -8,6 +8,7 @@ export async function GET(req: NextRequest) {
|
||||||
const { searchParams } = new URL(req.url)
|
const { searchParams } = new URL(req.url)
|
||||||
const password = searchParams.get('password') || ''
|
const password = searchParams.get('password') || ''
|
||||||
|
|
||||||
|
const config = await readConfig()
|
||||||
if (password !== config.adminPassword) {
|
if (password !== config.adminPassword) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,13 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { writeFile, readFile, mkdir } from 'node:fs/promises'
|
import { writeFile, mkdir } from 'node:fs/promises'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
import { readConfig } from '@/utils/config'
|
||||||
|
|
||||||
export const runtime = 'nodejs'
|
export const runtime = 'nodejs'
|
||||||
|
|
||||||
async function getConfig() {
|
|
||||||
const configPath = path.join(process.cwd(), 'src', 'config', 'config.json')
|
|
||||||
const content = await readFile(configPath, 'utf8')
|
|
||||||
return JSON.parse(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
const password = req.headers.get('x-admin-password') || ''
|
const password = req.headers.get('x-admin-password') || ''
|
||||||
const config = await getConfig()
|
const config = await readConfig()
|
||||||
|
|
||||||
if (password !== config.adminPassword) {
|
if (password !== config.adminPassword) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import config from '@/config/config.json'
|
import { readConfig } from '@/utils/config'
|
||||||
|
|
||||||
export const runtime = 'nodejs'
|
export const runtime = 'nodejs'
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
const { searchParams } = new URL(req.url)
|
const { searchParams } = new URL(req.url)
|
||||||
const auth = searchParams.get('auth') || ''
|
const auth = searchParams.get('auth') || ''
|
||||||
|
const config = await readConfig()
|
||||||
const ok = auth === config.authString
|
const ok = auth === config.authString
|
||||||
if (!ok) return NextResponse.json({ ok: false }, { status: 401 })
|
if (!ok) return NextResponse.json({ ok: false }, { status: 401 })
|
||||||
return NextResponse.json({ ok: true })
|
return NextResponse.json({ ok: true })
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { readFile } from 'node:fs/promises'
|
import { readFile } from 'node:fs/promises'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import Papa from 'papaparse'
|
import Papa from 'papaparse'
|
||||||
import config from '@/config/config.json'
|
import { readConfig } from '@/utils/config'
|
||||||
import { normalizeString } from '@/utils/stringNormalization'
|
import { normalizeString } from '@/utils/stringNormalization'
|
||||||
import type { Lifter } from '@/types/lifter'
|
import type { Lifter } from '@/types/lifter'
|
||||||
|
|
||||||
|
|
@ -11,6 +11,7 @@ export const runtime = 'nodejs'
|
||||||
type CsvRow = Record<string, string>
|
type CsvRow = Record<string, string>
|
||||||
|
|
||||||
async function loadLifters(): Promise<Lifter[]> {
|
async function loadLifters(): Promise<Lifter[]> {
|
||||||
|
const config = await readConfig()
|
||||||
const csvPath = path.join(process.cwd(), 'src', 'data', config.csvFileName)
|
const csvPath = path.join(process.cwd(), 'src', 'data', config.csvFileName)
|
||||||
const content = await readFile(csvPath, 'utf8')
|
const content = await readFile(csvPath, 'utf8')
|
||||||
const parsed = Papa.parse<CsvRow>(content, { header: true, skipEmptyLines: true })
|
const parsed = Papa.parse<CsvRow>(content, { header: true, skipEmptyLines: true })
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import config from '@/config/config.json'
|
import { readConfig } from '@/utils/config'
|
||||||
import type { RackHeightsPayload } from '@/types/rackHeights'
|
import type { RackHeightsPayload } from '@/types/rackHeights'
|
||||||
|
|
||||||
export const runtime = 'nodejs'
|
export const runtime = 'nodejs'
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
|
const config = await readConfig()
|
||||||
const body = (await req.json()) as RackHeightsPayload
|
const body = (await req.json()) as RackHeightsPayload
|
||||||
const requiredFields: (keyof RackHeightsPayload)[] = [
|
const requiredFields: (keyof RackHeightsPayload)[] = [
|
||||||
'memberNumber',
|
'memberNumber',
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,8 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { readFile } from 'node:fs/promises'
|
import { readConfig } from '@/utils/config'
|
||||||
import path from 'node:path'
|
|
||||||
|
|
||||||
async function getConfig() {
|
|
||||||
const configPath = path.join(process.cwd(), 'src', 'config', 'config.json')
|
|
||||||
const content = await readFile(configPath, 'utf8')
|
|
||||||
return JSON.parse(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
const config = await getConfig()
|
const config = await readConfig()
|
||||||
const url = req.nextUrl.clone()
|
const url = req.nextUrl.clone()
|
||||||
url.pathname = '/'
|
url.pathname = '/'
|
||||||
url.search = ''
|
url.search = ''
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,10 @@ import { readFile } from 'node:fs/promises'
|
||||||
import { readdirSync } from 'node:fs'
|
import { readdirSync } from 'node:fs'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import Papa from 'papaparse'
|
import Papa from 'papaparse'
|
||||||
import config from '@/config/config.json'
|
import { readConfig } from '@/utils/config'
|
||||||
|
|
||||||
export async function getLifterStats() {
|
export async function getLifterStats() {
|
||||||
|
const config = await readConfig()
|
||||||
try {
|
try {
|
||||||
const csvPath = path.join(process.cwd(), 'src', 'data', config.csvFileName)
|
const csvPath = path.join(process.cwd(), 'src', 'data', config.csvFileName)
|
||||||
const content = await readFile(csvPath, 'utf8')
|
const content = await readFile(csvPath, 'utf8')
|
||||||
|
|
|
||||||
17
src/utils/config.ts
Normal file
17
src/utils/config.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { readFile } from 'node:fs/promises'
|
||||||
|
import path from 'node:path'
|
||||||
|
|
||||||
|
export type AppConfig = {
|
||||||
|
meetId: string
|
||||||
|
csvFileName: string
|
||||||
|
authString: string
|
||||||
|
adminPassword: string
|
||||||
|
competitionName: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CONFIG_PATH = path.join(process.cwd(), 'src', 'config', 'config.json')
|
||||||
|
|
||||||
|
export async function readConfig(): Promise<AppConfig> {
|
||||||
|
const content = await readFile(CONFIG_PATH, 'utf8')
|
||||||
|
return JSON.parse(content) as AppConfig
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue