diff --git a/e-voting-system/frontend/app/api/votes/blockchain/route.ts b/e-voting-system/frontend/app/api/votes/blockchain/route.ts new file mode 100644 index 0000000..c62d8c7 --- /dev/null +++ b/e-voting-system/frontend/app/api/votes/blockchain/route.ts @@ -0,0 +1,30 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' + +/** + * Proxy API route for getting blockchain state + * Forwards GET requests to the backend API + */ +export async function GET(request: NextRequest) { + try { + const backendUrl = getBackendUrl() + const searchParams = request.nextUrl.searchParams + const electionId = searchParams.get('election_id') + + if (!electionId) { + return NextResponse.json({ detail: 'election_id is required' }, { status: 400 }) + } + + const response = await fetch(`${backendUrl}/api/votes/blockchain?election_id=${electionId}`, { + method: 'GET', + headers: { 'Content-Type': 'application/json' }, + }) + + const data = await response.json() + return NextResponse.json(data, { status: response.status }) + } catch (error) { + console.error('[Blockchain]', error) + const msg = error instanceof Error ? error.message : 'Unknown error' + return NextResponse.json({ detail: msg }, { status: 500 }) + } +} diff --git a/e-voting-system/frontend/app/api/votes/public-keys/route.ts b/e-voting-system/frontend/app/api/votes/public-keys/route.ts new file mode 100644 index 0000000..f511596 --- /dev/null +++ b/e-voting-system/frontend/app/api/votes/public-keys/route.ts @@ -0,0 +1,30 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' + +/** + * Proxy API route for getting public keys + * Forwards GET requests to the backend API + */ +export async function GET(request: NextRequest) { + try { + const backendUrl = getBackendUrl() + const searchParams = request.nextUrl.searchParams + const electionId = searchParams.get('election_id') + + if (!electionId) { + return NextResponse.json({ detail: 'election_id is required' }, { status: 400 }) + } + + const response = await fetch(`${backendUrl}/api/votes/public-keys?election_id=${electionId}`, { + method: 'GET', + headers: { 'Content-Type': 'application/json' }, + }) + + const data = await response.json() + return NextResponse.json(data, { status: response.status }) + } catch (error) { + console.error('[PublicKeys]', error) + const msg = error instanceof Error ? error.message : 'Unknown error' + return NextResponse.json({ detail: msg }, { status: 500 }) + } +} diff --git a/e-voting-system/frontend/app/api/votes/results/route.ts b/e-voting-system/frontend/app/api/votes/results/route.ts new file mode 100644 index 0000000..5de3543 --- /dev/null +++ b/e-voting-system/frontend/app/api/votes/results/route.ts @@ -0,0 +1,36 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' + +/** + * Proxy API route for getting election results + * Forwards GET requests to the backend API + */ +export async function GET(request: NextRequest) { + try { + const backendUrl = getBackendUrl() + const searchParams = request.nextUrl.searchParams + const electionId = searchParams.get('election_id') + + if (!electionId) { + return NextResponse.json({ detail: 'election_id is required' }, { status: 400 }) + } + + const token = request.headers.get('Authorization') + const headers: Record = { 'Content-Type': 'application/json' } + if (token) { + headers['Authorization'] = token + } + + const response = await fetch(`${backendUrl}/api/votes/results?election_id=${electionId}`, { + method: 'GET', + headers, + }) + + const data = await response.json() + return NextResponse.json(data, { status: response.status }) + } catch (error) { + console.error('[Results]', error) + const msg = error instanceof Error ? error.message : 'Unknown error' + return NextResponse.json({ detail: msg }, { status: 500 }) + } +} diff --git a/e-voting-system/frontend/app/api/votes/route.ts b/e-voting-system/frontend/app/api/votes/route.ts index f6c0786..b429048 100644 --- a/e-voting-system/frontend/app/api/votes/route.ts +++ b/e-voting-system/frontend/app/api/votes/route.ts @@ -1,8 +1,9 @@ import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' export async function GET(request: NextRequest) { try { - const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000' + const backendUrl = getBackendUrl() const searchParams = request.nextUrl.searchParams const url = new URL('/api/votes', backendUrl) searchParams.forEach((value, key) => url.searchParams.append(key, value)) diff --git a/e-voting-system/frontend/app/api/votes/setup/route.ts b/e-voting-system/frontend/app/api/votes/setup/route.ts index d20c08f..bda8034 100644 --- a/e-voting-system/frontend/app/api/votes/setup/route.ts +++ b/e-voting-system/frontend/app/api/votes/setup/route.ts @@ -1,8 +1,9 @@ import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' export async function POST(request: NextRequest) { try { - const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000' + const backendUrl = getBackendUrl() const searchParams = request.nextUrl.searchParams const url = new URL('/api/votes/setup', backendUrl) searchParams.forEach((value, key) => url.searchParams.append(key, value)) diff --git a/e-voting-system/frontend/app/api/votes/submit/route.ts b/e-voting-system/frontend/app/api/votes/submit/route.ts index ac1e5cc..829fffb 100644 --- a/e-voting-system/frontend/app/api/votes/submit/route.ts +++ b/e-voting-system/frontend/app/api/votes/submit/route.ts @@ -1,8 +1,9 @@ import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' export async function POST(request: NextRequest) { try { - const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000' + const backendUrl = getBackendUrl() const body = await request.json() const headers: HeadersInit = { 'Content-Type': 'application/json' } const authHeader = request.headers.get('authorization') diff --git a/e-voting-system/frontend/app/api/votes/transaction-status/route.ts b/e-voting-system/frontend/app/api/votes/transaction-status/route.ts new file mode 100644 index 0000000..0dfdb85 --- /dev/null +++ b/e-voting-system/frontend/app/api/votes/transaction-status/route.ts @@ -0,0 +1,40 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' + +/** + * Proxy API route for getting transaction status + * Forwards GET requests to the backend API + */ +export async function GET(request: NextRequest) { + try { + const backendUrl = getBackendUrl() + const searchParams = request.nextUrl.searchParams + const transactionId = searchParams.get('transaction_id') + const electionId = searchParams.get('election_id') + + if (!transactionId || !electionId) { + return NextResponse.json({ detail: 'transaction_id and election_id are required' }, { status: 400 }) + } + + const token = request.headers.get('Authorization') + const headers: Record = { 'Content-Type': 'application/json' } + if (token) { + headers['Authorization'] = token + } + + const response = await fetch( + `${backendUrl}/api/votes/transaction-status?transaction_id=${transactionId}&election_id=${electionId}`, + { + method: 'GET', + headers, + } + ) + + const data = await response.json() + return NextResponse.json(data, { status: response.status }) + } catch (error) { + console.error('[TransactionStatus]', error) + const msg = error instanceof Error ? error.message : 'Unknown error' + return NextResponse.json({ detail: msg }, { status: 500 }) + } +} diff --git a/e-voting-system/frontend/app/api/votes/verify-blockchain/route.ts b/e-voting-system/frontend/app/api/votes/verify-blockchain/route.ts index 761e152..99b6f12 100644 --- a/e-voting-system/frontend/app/api/votes/verify-blockchain/route.ts +++ b/e-voting-system/frontend/app/api/votes/verify-blockchain/route.ts @@ -1,8 +1,9 @@ import { NextRequest, NextResponse } from 'next/server' +import { getBackendUrl } from '@/lib/api-config' export async function POST(request: NextRequest) { try { - const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000' + const backendUrl = getBackendUrl() const searchParams = request.nextUrl.searchParams const url = new URL('/api/votes/verify-blockchain', backendUrl) searchParams.forEach((value, key) => url.searchParams.append(key, value)) diff --git a/e-voting-system/frontend/lib/api.ts b/e-voting-system/frontend/lib/api.ts index 6de59c9..93960a5 100644 --- a/e-voting-system/frontend/lib/api.ts +++ b/e-voting-system/frontend/lib/api.ts @@ -251,6 +251,19 @@ export const votesApi = { async getTransactionStatus(transactionId: string, electionId: number) { return apiRequest(`/api/votes/transaction-status?transaction_id=${transactionId}&election_id=${electionId}`) }, + + async getPublicKeys(electionId: number) { + return apiRequest(`/api/votes/public-keys?election_id=${electionId}`, { + skipAuth: true, + }) + }, + + async setupElection(electionId: number) { + return apiRequest("/api/votes/setup", { + method: "POST", + body: JSON.stringify({ election_id: electionId }), + }) + }, } /**