Skip to content

Commit 771de04

Browse files
committed
加入PostgreSQL
1 parent 3b0e3eb commit 771de04

40 files changed

+945
-101
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ dmypy.json
152152
# Cython debug symbols
153153
cython_debug/
154154

155+
log/
156+
155157
# PyCharm
156158
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157159
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158160
# and can be added to the global gitignore or merged into this file. For a more nuclear
159161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
#.idea/
162+
.idea/

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
you may not use this file except in compliance with the License.
193193
You may obtain a copy of the License at
194194

195-
http://www.apache.org/licenses/LICENSE-2.0
195+
https://www.apache.org/licenses/LICENSE-2.0
196196

197197
Unless required by applicable law or agreed to in writing, software
198198
distributed under the License is distributed on an "AS IS" BASIS,

app/__init__.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
https://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,5 +17,11 @@
1717
from . import main
1818

1919
__all__ = [
20-
"main",
20+
'main',
2121
]
22+
23+
__version__ = '0.0.1'
24+
__author__ = 'Maner·Fan'
25+
__author_email__ = '[email protected]'
26+
__license__ = 'Apache License 2.0'
27+
__copyright__ = 'Copyright 2024 Maner·Fan'

app/routers/__init__.py renamed to app/api/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
https://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,8 +14,8 @@
1414
limitations under the License.
1515
"""
1616

17-
from .register import register
17+
from . import routers
1818

1919
__all__ = [
20-
"register",
20+
'routers',
2121
]

app/api/routers/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Copyright 2024 Maner·Fan
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
__all__ = [
18+
'register',
19+
]

app/routers/auth/__init__.py renamed to app/api/routers/auth/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
https://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,6 +17,6 @@
1717
from . import login, users
1818

1919
__all = [
20-
"login",
21-
"users",
20+
'login',
21+
'users',
2222
]

app/routers/auth/login.py renamed to app/api/routers/auth/login.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
https://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,27 +14,33 @@
1414
limitations under the License.
1515
"""
1616

17+
from dependency_injector.wiring import inject, Provide
1718
from fastapi import APIRouter, Depends
1819
from fastapi.security import OAuth2PasswordRequestForm
1920

20-
from dependency_injector.wiring import inject, Provide
2121
from app_container import AppContainer
22-
from services.account import AccountService
22+
from services import AccountService
2323

2424
router = APIRouter()
2525

2626

27-
@router.post("/login")
27+
@router.post('/login')
2828
@inject
2929
def login(
30-
form_data: OAuth2PasswordRequestForm = Depends(),
31-
accountService: AccountService = Depends(
32-
Provide[AppContainer.serviceContainer.accountContainer.accountService]
33-
),
30+
form_data: OAuth2PasswordRequestForm = Depends(),
31+
account_service: AccountService = Depends(
32+
Provide[AppContainer.service_container.account_service]
33+
),
3434
):
35-
return accountService.authenticate(form_data.username, form_data.password)
35+
"""
36+
登录
37+
:param form_data: 登录提交的参数
38+
:param account_service: 账号服务
39+
:return:
40+
"""
41+
return account_service.authenticate(form_data.username, form_data.password)
3642

3743

38-
@router.get("/logout")
44+
@router.get('/logout')
3945
def logout():
4046
return

app/routers/auth/users.py renamed to app/api/routers/auth/users.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
https://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,11 +14,11 @@
1414
limitations under the License.
1515
"""
1616

17-
from fastapi import APIRouter, Depends
17+
from fastapi import APIRouter
1818

1919
router = APIRouter()
2020

2121

22-
@router.get("/me")
22+
@router.get('/me')
2323
def me():
24-
return {"name": "Maner·Fan"}
24+
return {'name': 'Maner·Fan'}

app/api/routers/register.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Copyright 2024 Maner·Fan
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
import logging
18+
19+
from fastapi import FastAPI
20+
from fastapi.exceptions import RequestValidationError
21+
from starlette.responses import JSONResponse
22+
23+
from utils.errors.base_error import BaseServiceError
24+
from . import auth
25+
26+
log = logging.getLogger()
27+
28+
29+
def register(app: FastAPI):
30+
"""
31+
注册路由
32+
:param app: FastAPI
33+
"""
34+
35+
exception_handler(app)
36+
37+
app.include_router(auth.login.router, prefix='/api', tags=['auth | 认证'])
38+
app.include_router(auth.users.router, prefix='/api/user', tags=['user | 用户'])
39+
40+
41+
def exception_handler(app: FastAPI):
42+
"""
43+
定义全局异常处理
44+
:param app: FastAPI
45+
"""
46+
47+
@app.exception_handler(RequestValidationError)
48+
async def validation_exception_handler(request, exc: RequestValidationError):
49+
log.warning('RequestValidationError %s, %s', exc.__class__, str(exc))
50+
return JSONResponse(str(exc), status_code=400)
51+
52+
@app.exception_handler(BaseServiceError)
53+
async def service_exception_handler(request, exc: BaseServiceError):
54+
log.warning('ServiceError %s, %s', exc.__class__, exc.message)
55+
return JSONResponse({'message': exc.message}, status_code=exc.status_code)
56+
57+
@app.exception_handler(Exception)
58+
async def common_exception_handler(request, exc: Exception):
59+
log.exception('ExceptionError %s', str(exc))
60+
return JSONResponse({'message': str(exc)}, status_code=500)

app/app_container.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
https://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,15 +15,30 @@
1515
"""
1616

1717
from dependency_injector import containers, providers
18+
19+
import repositories
1820
import services
1921

2022

2123
class AppContainer(containers.DeclarativeContainer):
24+
"""
25+
应用容器
26+
"""
27+
2228
# 全局配置
23-
config = providers.Configuration(yaml_files=["./config.yml"])
29+
config = providers.Configuration(yaml_files=['./config.yml'])
30+
31+
# 数据容器
32+
repository_container: repositories.RepositoryContainer = providers.Container(
33+
repositories.RepositoryContainer,
34+
config=config,
35+
)
2436

2537
# 服务容器
26-
serviceContainer: services.ServiceContainer = providers.Container(
38+
service_container: services.ServiceContainer = providers.Container(
2739
services.ServiceContainer,
2840
config=config,
41+
data_container=repository_container.data_container,
42+
oss_container=repository_container.oss_container,
43+
vector_container=repository_container.vector_container,
2944
)

app/config.yml

+26-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# https://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,18 +16,35 @@
1616
app:
1717
title: Cube Chat | Speek FREELY with Me!
1818
description: Think as you think and Reach as you reach | 想你所想及你所及
19-
summary:
19+
summary:
2020
version: '0.0.1'
21-
terms_of_service:
21+
terms_of_service:
2222
contact:
23-
name: Maner·Fan
24-
url: https://github.com/manerfan
25-
23+
name: Maner·Fan
24+
url: https://github.com/manerfan
25+
2626
license_info:
27-
name: Apache 2.0
28-
url: http://www.apache.org/licenses/LICENSE-2.0
27+
name: Apache 2.0
28+
url: https://www.apache.org/licenses/LICENSE-2.0
2929

3030
# 服务配置
3131
server:
3232
host: ${SERVER_HOST:0.0.0.0}
33-
port: ${SERVER_PORT:8000}
33+
port: ${SERVER_PORT:8000}
34+
35+
repository:
36+
# data
37+
data:
38+
type: postgres
39+
postgres:
40+
host: ${POSTGRES_HOST:localhost}
41+
port: ${POSTGRES_PORT:5432}
42+
database: ${POSTGRES_DATABASE:cube_chat}
43+
username: ${POSTGRES_USERNAME:cube_chat}
44+
password: ${POSTGRES_PASSWORD:cube_chat}
45+
# oss
46+
oss:
47+
type: aliyun
48+
# vector
49+
vector:
50+
type: postgres

app/logging.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: 1
2+
formatters:
3+
default:
4+
format: '%(asctime)s [%(levelname)s] [%(name)s] %(message)s'
5+
handlers:
6+
console:
7+
class: logging.StreamHandler
8+
level: INFO
9+
formatter: default
10+
stream: ext://sys.stdout
11+
app:
12+
class: logging.handlers.RotatingFileHandler
13+
level: INFO
14+
formatter: default
15+
encoding: utf8
16+
filename: log/cube-chat/app.log
17+
maxBytes: 104857600
18+
backupCount: 10
19+
root:
20+
level: INFO
21+
handlers:
22+
- console
23+
- app

0 commit comments

Comments
 (0)