Skip to content

Commit

Permalink
feat: login configable
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhunter2333 committed May 15, 2024
1 parent 8ce76a6 commit 4862718
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
20 changes: 12 additions & 8 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const fetchSettings = async () => {
}
}
const showAd = computed(() => !isMobile.value && settings.value.ad_client);
onMounted(async () => {
await fetchSettings();
if (!isMobile.value && settings.value.ad_client) {
Expand All @@ -59,23 +61,25 @@ onMounted(async () => {
<n-spin description="加载中..." :show="loading">
<n-global-style />
<n-message-provider>
<n-grid :x-gap="12" :cols="isMobile ? 4 : 6">
<n-gi v-if="!isMobile">
<div class="side">
<n-grid :x-gap="12" :cols="isMobile ? 6 : 8">
<n-gi :span="1">
<div class="side" v-if="showAd">
<ins class="adsbygoogle" style="display:block" :data-ad-client="settings.ad_client"
:data-ad-slot="settings.ad_slot" data-ad-format="auto" data-full-width-responsive="true"></ins>
</div>
</n-gi>
<n-gi :span="4">
<n-gi :span="6">
<div class="main">
<n-page-header :subtitle="isMobile ? '' : '本项目仅供娱乐'">
<template #title>
<h3>AI 占卜</h3>
</template>
<template #extra>
<n-space>
<n-button v-if="settings.user_name" @click="logOut">登出</n-button>
<n-button v-else type="primary" @click="router.push('/login')">登录</n-button>
<div v-if="settings.enable_login">
<n-button v-if="settings.user_name" @click="logOut">登出</n-button>
<n-button v-else type="primary" @click="router.push('/login')">登录</n-button>
</div>
<n-button @click="themeStorage = (themeStorage == 'dark' ? 'light' : 'dark')">
{{ themeStorage == 'dark' ? '亮色' : '暗色' }}
</n-button>
Expand All @@ -89,7 +93,7 @@ onMounted(async () => {
<n-alert v-if="settings.user_name" type="success">
你好, {{ settings.login_type }} 用户 {{ settings.user_name }}
</n-alert>
<n-alert v-else type="warning">
<n-alert v-else-if="settings.enable_login && settings.enable_rate_limit" type="warning">
当前未登录, 处于限流模式 ({{ settings.rate_limit }})
</n-alert>
</template>
Expand All @@ -98,7 +102,7 @@ onMounted(async () => {
</div>
</n-gi>
<n-gi :span="1" v-if="!isMobile">
<div class="side">
<div class="side" v-if="showAd">
<ins class="adsbygoogle" style="display:block" :data-ad-client="settings.ad_client"
:data-ad-slot="settings.ad_slot" data-ad-format="auto" data-full-width-responsive="true"></ins>
</div>
Expand Down
17 changes: 9 additions & 8 deletions src/chatgpt_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ async def divination(

real_ip = get_real_ipaddr(request)
# rate limit when not login
if not user:
max_reqs, time_window_seconds = settings.rate_limit
check_rate_limit(real_ip, time_window_seconds, max_reqs)
else:
max_reqs, time_window_seconds = settings.user_rate_limit
check_rate_limit(
f"{user.login_type}:{user.user_name}", time_window_seconds, max_reqs
)
if settings.enable_rate_limit:
if not user:
max_reqs, time_window_seconds = settings.rate_limit
check_rate_limit(real_ip, time_window_seconds, max_reqs)
else:
max_reqs, time_window_seconds = settings.user_rate_limit
check_rate_limit(
f"{user.login_type}:{user.user_name}", time_window_seconds, max_reqs
)

_logger.info(
f"Request from {real_ip}, "
Expand Down
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Settings(BaseSettings):
api_key: str = Field(default="sk-xxx", exclude=True)
api_base: str = "https://api.openai.com/v1"
model: str = "gpt-3.5-turbo"
enable_rate_limit: bool = True
# rate limit xxx request per xx seconds
rate_limit: Tuple[int, int] = (60, 60 * 60)
user_rate_limit: Tuple[int, int] = (600, 60 * 60)
Expand Down
2 changes: 2 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class SettingsInfo(BaseModel):
user_rate_limit: str
ad_client: str = ""
ad_slot: str = ""
enable_login: bool = False
enable_rate_limit: bool = False


class OauthBody(BaseModel):
Expand Down
2 changes: 2 additions & 0 deletions src/user_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ async def info(user: Optional[User] = Depends(get_user)):
ad_slot=settings.ad_slot,
rate_limit=settings.get_human_rate_limit(),
user_rate_limit=settings.get_human_user_rate_limit(),
enable_login=bool(settings.github_client_id),
enable_rate_limit=settings.enable_rate_limit
)


Expand Down

0 comments on commit 4862718

Please sign in to comment.