Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tap4-ai committed Jun 20, 2024
1 parent 6a843b5 commit 3b47c7d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=
CRAWLER_API=
CRAWLER_API_KEY=

CRON_AUTH_KEY=
CRON_AUTH_KEY=

SUBMIT_AUTH_KEY=
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ CRAWLER_API_KEY="xxxx"
# Custom interface verification key
CRON_AUTH_KEY="keyxxxx"

# Submit API verification key
SUBMIT_AUTH_KEY="xxxx"

```

**Note: This version uses Vercel's scheduled tasks to automatically read and submit websites and generate website
Expand Down Expand Up @@ -157,6 +160,9 @@ CRAWLER_API_KEY="xxxx"
# Custom interface verification key
CRON_AUTH_KEY="keyxxxx"

# Submit API verification key
SUBMIT_AUTH_KEY="xxxx"

```

#### (5) runs on dev mode
Expand Down
6 changes: 6 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ CRAWLER_API_KEY="xxxx"
# Custom interface verification key
CRON_AUTH_KEY="keyxxxx"

# Submit API verification key
SUBMIT_AUTH_KEY="xxxx"

```

**注:此版本采用了vercel的定时任务用来自动读取自动提交的网站并生成网站结果**
Expand Down Expand Up @@ -151,6 +154,9 @@ CRAWLER_API_KEY="xxxx"
# Custom interface verification key
CRON_AUTH_KEY="keyxxxx"

# Submit API verification key
SUBMIT_AUTH_KEY="xxxx"

```

#### (4)在开发模式下运行
Expand Down
1 change: 0 additions & 1 deletion app/[locale]/(with-footer)/category/[code]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default async function Page({ params }: { params: { code: string } }) {
return (
<Content
headerTitle={categoryList[0]!.title || params.code}
code={params.code}
navigationList={navigationList!}
currentPage={1}
total={count!}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export default async function Page({ params }: { params: { code: string; pageNum
return (
<Content
headerTitle={categoryList[0]!.title || params.code}
code={params.code}
navigationList={navigationList!}
currentPage={currentPage}
total={count!}
Expand Down
4 changes: 2 additions & 2 deletions app/api/cron/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/prefer-default-export */
import { NextResponse } from 'next/server';
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/db/supabase/client';

import crawler from './crawler';
Expand All @@ -14,7 +14,7 @@ import crawler from './crawler';
// insert web_nav table (tags <- tags[0] or 'other')
// update submit table status

export async function POST(req) {
export async function POST(req: NextRequest) {
try {
// 获取请求头中的 Authorization
const authHeader = req.headers.get('Authorization');
Expand Down
74 changes: 74 additions & 0 deletions app/api/submit/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable import/prefer-default-export */
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/db/supabase/client';

// submit table empty -> stop

// filter status
// isFeature (priority)
// time order

// when crawler is done
// insert web_nav table (tags <- tags[0] or 'other')
// update submit table status

export async function POST(req: NextRequest) {
try {
// Get Authorization
const authHeader = req.headers.get('Authorization');

// Check Authorization and Verify token
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return NextResponse.json({ error: 'Authorization header is missing or malformed' }, { status: 401 });
}

const token = authHeader.split(' ')[1];
const submitKey = process.env.SUBMIT_AUTH_KEY;
// check key
const isValid = submitKey === token;
if (!isValid) {
return NextResponse.json({ error: 'Invalid token' }, { status: 401 });
}

const supabase = createClient();

// 从请求体中获取参数
const { email, url, name } = await req.json();

// 检查参数是否存在
if (!email || !url || !name) {
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 });
}

// 检查 URL 是否已存在
const { data: existingEntry, error: existingEntryError } = await supabase
.from('web_navigation')
.select()
.eq('url', url)
.single();

if (existingEntryError && existingEntryError.code !== 'PGRST116') {
// PGRST116 means no rows found
throw new Error(existingEntryError.message);
}

if (existingEntry) {
return NextResponse.json({ message: 'Success' });
}

// 插入新数据
const { error: insertError } = await supabase.from('submit').insert({
email,
url,
name,
});

if (insertError) {
throw new Error(insertError.message);
}

return NextResponse.json({ message: 'Success' });
} catch (error) {
return NextResponse.json({ error: Error }, { status: 500 });
}
}

0 comments on commit 3b47c7d

Please sign in to comment.