Skip to content

Commit e8b858e

Browse files
committed
fix bug
1 parent 4875087 commit e8b858e

File tree

9 files changed

+67
-37
lines changed

9 files changed

+67
-37
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![](https://img.shields.io/badge/Vue-2.x-green.svg)](https://cn.vuejs.org/index.html)
66
[![](https://img.shields.io/badge/ElementUI-2.13.2-blue.svg)](https://element.eleme.io/#/zh-CN)
77
[![](https://img.shields.io/badge/BootstrapVue-2.21.2-blueviolet.svg)](https://code.z01.com/bootstrap-vue/)
8+
[![](https://img.shields.io/badge/Elasticsearch-7.17.0-ff69b4.svg)](https://www.elastic.co/cn/elasticsearch/)
89

910

1011
PS: 日常维护中
@@ -93,9 +94,25 @@ git clone https://gitee.com/xuqihui/fastapi-vue-blog
9394
### 简单发布(可能不适合生产环境)
9495
```
9596
前提:安装好Docker和docker-compose
96-
1: cd fastapi-vue-blog
97-
2: docker-compose -f webappSimple-compose.yml up -d --build
98-
3: docker-compose -f webappSimple-compose.yml exec webapp python db/init_db.py # 如果数据没有初始化
97+
# 工作目录
98+
cd fastapi-vue-blog
99+
100+
# 构建容器和启动服务
101+
bash launcher.sh
102+
103+
# 构建镜像和启动服务
104+
docker-compose -f webappSimple-compose.yml up -d --build
105+
106+
docker-compose -f webappSimple-compose.yml exec webapp python db/init_db.py # 如果数据没有初始化
107+
108+
# 查看服务情况
109+
docker-compose -f webappSimple-compose.yml ps
110+
111+
# 停止服务
112+
docker-compose -f webappSimple-compose.yml stop
113+
114+
# 关闭服务并移除容器
115+
docker-compose -f webappSimple-compose.yml down
99116
```
100117

101118
## 图片预览

elasticsearch.dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM elasticsearch:7.17.0
2+
3+
WORKDIR /usr/share/elasticsearch
4+
COPY resource/elasticsearch-analysis-ik-7.17.0.zip .
5+
RUN unzip elasticsearch-analysis-ik-7.17.0.zip -d /usr/share/elasticsearch/plugins/ik
6+
RUN chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/plugins/ik

launcher.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
docker-compose -f webappSimple-compose.yml up -d --build
4+
sleep 1
5+
6+
echo "Preparing initialize data..."
7+
sleep 2
8+
docker-compose -f webappSimple-compose.yml exec webapp python db/init_db.py
9+
sleep 1
10+
11+
docker-compose -f webappSimple-compose.yml restart all
12+
13+
docker-compose -f webappSimple-compose.yml ps
4.3 MB
Binary file not shown.

webapi/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: UTF-8 -*-
2+
import time
3+
import traceback
24
import asyncio
35
import os
46
from starlette.middleware.cors import CORSMiddleware
@@ -11,6 +13,8 @@
1113

1214

1315
def create_application() -> FastAPI:
16+
# 等待数据库和搜索引擎启动完成
17+
time.sleep(3)
1418
application = FastAPI()
1519
application.include_router(api_router, prefix='/api')
1620
register_middleware(application)
@@ -103,7 +107,10 @@ async def inject_data_to_es():
103107
def register_event(app):
104108
@app.on_event("startup")
105109
async def startup_event():
106-
await register_elasticsearch()
110+
try:
111+
await register_elasticsearch()
112+
except Exception as e:
113+
traceback.print_exc()
107114

108115
@app.on_event("shutdown")
109116
async def shutdown_event():

webapi/db/dals/post_dal.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ async def create(self, obj_in: PostIn):
3535
db_obj.categories.extend(categories)
3636
self.db_session.add(db_obj)
3737
await self.db_session.flush()
38-
await es_create_doc({
39-
'id': db_obj.id,
40-
'title': db_obj.title,
41-
'description': db_obj.description,
42-
'body': db_obj.body,
43-
'timestamp': db_obj.timestamp,
44-
})
38+
if db_obj.is_published:
39+
await es_create_doc({
40+
'id': db_obj.id,
41+
'title': db_obj.title,
42+
'description': db_obj.description,
43+
'body': db_obj.body,
44+
'timestamp': db_obj.timestamp,
45+
})
4546
return db_obj
4647

4748
async def update(self, db_obj: Post, obj_in: PostInUpdate):

webapi/setting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ class Settings(BaseSettings):
1818
ADMIN_EMAIL = '[email protected]'
1919

2020
# 数据库账号密码
21-
DB_HOST = 'fastapivueblog'
21+
DB_HOST = 'mysql'
2222
DB_PORT = 3306
2323
DB_USER = 'root'
24-
DB_PASSWORD = 'blog@fastapi'
24+
DB_PASSWORD = 'fastapivueblog12306'
2525
DB_NAME = 'FastAPIVueBlog'
2626

2727
DATABASE_URI = f'mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}'
2828
SQLALCHEMY_DATABASE_URI: str = f'mysql+aiomysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}'
2929
SECRET_KEY: str = secrets.token_urlsafe(32)
3030

3131
# Elasticsearch
32-
ELASTIC_HOST = 'fastapivueblog'
32+
ELASTIC_HOST = 'elasticsearch'
3333
ELASTIC_PORT = 9200
3434

3535
# 12 hours

webapi/utils/elastic.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,7 @@ def parse_data(data):
6565

6666

6767
async def es_update_doc(data):
68-
if await es.exists(index=INDEX, id=data['id']):
69-
await es.update(
70-
index=INDEX,
71-
id=data['id'],
72-
body=data,
73-
)
74-
else:
75-
await es_create_doc(data)
68+
await es_create_doc(data)
7669

7770

7871
async def es_delete_doc(_id):
@@ -84,6 +77,7 @@ async def es_delete_doc(_id):
8477

8578

8679
async def es_create_doc(data):
80+
await es_delete_doc(data['id'])
8781
await es.create(
8882
index=INDEX,
8983
id=data['id'],

webappSimple-compose.yml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,27 @@ services:
88
- 9527:5000
99
depends_on:
1010
- mysql
11-
networks:
12-
- fastapivueblog
11+
- elasticsearch
1312

1413
mysql:
1514
image: "mariadb:10.5.9-focal"
1615
environment:
1716
- MYSQL_ROOT_HOST=%
18-
- MYSQL_ROOT_PASSWORD=blog@fastapi
17+
- MYSQL_ROOT_PASSWORD=fastapivueblog12306
1918
- MYSQL_DATABASE=FastAPIVueBlog
20-
- MYSQL_USER=root
21-
- MYSQL_PASSWORD=blog@fastapi
19+
# - MYSQL_USER=root
20+
- MYSQL_PASSWORD=fastapivueblog12306
2221
ports:
2322
- 13306:3306
24-
networks:
25-
- fastapivueblog
2623
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
2724

2825
elasticsearch:
29-
image: "elasticsearch:7.17.0"
26+
build:
27+
context: .
28+
dockerfile: elasticsearch.dockerfile
3029
environment:
3130
- discovery.type=single-node
3231
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
3332
ports:
3433
- 19200:9200
3534
- 19300:9300
36-
networks:
37-
- fastapivueblog
38-
39-
40-
networks:
41-
fastapivueblog:
42-
driver: bridge

0 commit comments

Comments
 (0)