Skip to content

Commit

Permalink
feat: improve configs
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed Mar 12, 2024
1 parent fbbf161 commit 8461b4f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 29 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CLERK_SECRET_KEY=""
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY="pk_"

# 数据库信息,评论系统必需
# 数据库信息,评论/留言墙功能必需
DATABASE_URL=""

# Sanity CMS 必需
Expand All @@ -13,11 +13,12 @@ NEXT_PUBLIC_SANITY_USE_CDN="false"
# Email 通知必需
RESEND_API_KEY=""
SITE_NOTIFICATION_EMAIL_TO="[email protected]"
NEXT_PUBLIC_SITE_EMAIL_FROM="[email protected]"
NEXT_PUBLIC_SITE_URL="https://example.com"

# Upstash Redis 必需
UPSTASH_REDIS_REST_TOKEN=""
UPSTASH_REDIS_REST_URL="https://example.upstash.io"

# Vercel 必需
VERCEL_ENV="development"
EDGE_CONFIG=""
8 changes: 5 additions & 3 deletions config/email.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { env } from '~/env.mjs'

export const emailConfig = {
from: '[email protected]',
from: env.NEXT_PUBLIC_SITE_EMAIL_FROM,
baseUrl:
process.env.VERCEL_ENV === 'production'
? `https://cali.so`
env.VERCEL_ENV === 'production'
? env.NEXT_PUBLIC_SITE_URL
: 'http://localhost:3000',
}
37 changes: 37 additions & 0 deletions config/redirects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"source": "/twitter",
"destination": "https://x.com/thecalicastle",
"permanent": true
},
{
"source": "/x",
"destination": "https://x.com/thecalicastle",
"permanent": true
},
{
"source": "/youtube",
"destination": "https://youtube.com/@calicastle",
"permanent": true
},
{
"source": "/tg",
"destination": "https://t.me/cali_so",
"permanent": true
},
{
"source": "/linkedin",
"destination": "https://www.linkedin.com/in/calicastle/",
"permanent": true
},
{
"source": "/github",
"destination": "https://github.com/CaliCastle",
"permanent": true
},
{
"source": "/bilibili",
"destination": "https://space.bilibili.com/8350251",
"permanent": true
}
]
4 changes: 4 additions & 0 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const client = z.object({
NEXT_PUBLIC_SANITY_PROJECT_ID: z.string().min(1),
NEXT_PUBLIC_SANITY_DATASET: z.string().min(1),
NEXT_PUBLIC_SANITY_USE_CDN: z.boolean(),
NEXT_PUBLIC_SITE_URL: z.string().min(1),
NEXT_PUBLIC_SITE_EMAIL_FROM: z.string().min(1),
})

/**
Expand All @@ -36,6 +38,8 @@ const processEnv = {
NEXT_PUBLIC_SANITY_PROJECT_ID: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
NEXT_PUBLIC_SANITY_DATASET: process.env.NEXT_PUBLIC_SANITY_DATASET,
NEXT_PUBLIC_SANITY_USE_CDN: process.env.NEXT_PUBLIC_SANITY_USE_CDN == 'true',
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
NEXT_PUBLIC_SITE_EMAIL_FROM: process.env.NEXT_PUBLIC_SITE_EMAIL_FROM,
LINK_PREVIEW_API_BASE_URL: process.env.LINK_PREVIEW_API_BASE_URL,
SITE_NOTIFICATION_EMAIL_TO: process.env.SITE_NOTIFICATION_EMAIL_TO,
}
Expand Down
34 changes: 18 additions & 16 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@ export const config = {

async function beforeAuthMiddleware(req: NextRequest) {
const { geo, nextUrl } = req

const blockedIPs = await get<string[]>('blocked_ips')
const ip = getIP(req)
const isApi = nextUrl.pathname.startsWith('/api/')

if (blockedIPs?.includes(ip)) {
if (isApi) {
return NextResponse.json(
{ error: 'You have been blocked.' },
{ status: 403 }
)
}
if (process.env.EDGE_CONFIG) {
const blockedIPs = await get<string[]>('blocked_ips')
const ip = getIP(req)

nextUrl.pathname = '/blocked'
return NextResponse.rewrite(nextUrl)
}
if (blockedIPs?.includes(ip)) {
if (isApi) {
return NextResponse.json(
{ error: 'You have been blocked.' },
{ status: 403 }
)
}

if (nextUrl.pathname === '/blocked') {
nextUrl.pathname = '/'
return NextResponse.redirect(nextUrl)
nextUrl.pathname = '/blocked'
return NextResponse.rewrite(nextUrl)
}

if (nextUrl.pathname === '/blocked') {
nextUrl.pathname = '/'
return NextResponse.redirect(nextUrl)
}
}

if (geo && !isApi && env.VERCEL_ENV === 'production') {
Expand Down
10 changes: 2 additions & 8 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get } from '@vercel/edge-config'
import redirects from './config/redirects.json'

/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
Expand All @@ -16,13 +16,7 @@ const nextConfig = {
domains: ['cdn.sanity.io'],
},

async redirects() {
try {
return (await get('redirects')) ?? []
} catch {
return []
}
},
redirects,

rewrites() {
return [
Expand Down

0 comments on commit 8461b4f

Please sign in to comment.