# full-stack-fastapi-postgresql **Repository Path**: ly3566/full-stack-fastapi-postgresql ## Basic Information - **Project Name**: full-stack-fastapi-postgresql - **Description**: full-stack-fastapi-postgresql 全栈fastapi项目的使用说明 - **Primary Language**: Python - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 0 - **Created**: 2022-04-29 - **Last Updated**: 2024-01-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python ## README 在练习 [GitHub - tiangolo/full-stack-fastapi-postgresql](https://github.com/tiangolo/full-stack-fastapi-postgresql) 项目的时候发现有很多坑,这里做一下记录。 参考文章:[full-stack-fastapi-postgresql-从安装docker开始 - Coodyzのblog - 博客园 (cnblogs.com)](https://www.cnblogs.com/coodyz/p/14287021.html) ### 预览 ![前端](img/1.png) ![api](img/2.png) ![traefik](img/3.png) ### 准备工作 - **创建项目** 下载`full-stack-fastapi-postgresql-master.zip`放到你需要生成项目的位置,执行命令: ```shell pip install cookiecutter cookiecutter full-stack-fastapi-postgresql-master.zip ``` 执行命令后会要求输入一些基本配置,推荐只设置`project_name`项目名称,比如我的项目名叫`ly`,其他使用默认值就行,全部设置之后会在当前目录下生成一个项目 这是作者推荐的使用方法,但是我没法生成项目,使用本地安装就正常 ```shell pip install cookiecutter # 生成项目的时候会要求输入一些配置 cookiecutter https://github.com/tiangolo/full-stack-fastapi-postgresql ``` - **安装docker** 我用的是win11系统,安装过程比较顺利,启动的时候如果提示更新wsl的话,可以按提示下载新版[wsl2](https://docs.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package)更新。打开软件正常启动没有报错就行 ### 修改配置文件 > 如果直接用作者原来的配置,启动的时候各种报错... > > 可以用我修改的文件直接替换,也可以自己打开文件修改 **先使用cookiecutter创建项目才会有这些文件,在使用docker启动项目前修改它们。** **docker-compose.yml** 文件在当前项目下,需要修改flower的版本。lssues上有关于这个的说明[#403](https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/403)、[#420](https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/420) ```dockerfile # 115行 flower: # image: mher/flower # 使用这个版本不会报错 image: mher/flower:0.9.7 ``` **backend.dockerfile 和 celeryworker.dockerfile** 这两个文件在`backend`目录下,**两个都需要修改**,使用pip替换poetry来安装python包 ```dockerfile # 使用pip来安装puthon包 COPY ./app/requirments.txt /app/ RUN pip config set global.index-url http://mirrors.aliyun.com/pypi/simple && \ pip config set install.trusted-host mirrors.aliyun.com && \ pip install -U pip && \ pip install -r requirments.txt # 把poetry相关的代码注释掉 # # Install Poetry # RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \ # cd /usr/local/bin && \ # ln -s /opt/poetry/bin/poetry && \ # poetry config virtualenvs.create false # # Allow installing dev dependencies to run tests # ARG INSTALL_DEV=false # RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi" ``` **requirments.txt** 这个文件需要手动创建,用pip安装依赖的时候需要用到它,文件放在`backend\app`目录下;原来的依赖文件在`pyproject.toml` ``` uvicorn==0.11.3 fastapi==0.54.1 python-multipart==0.0.5 email-validator==1.0.5 requests==2.23.0 celery==4.4.2 passlib==1.7.2 tenacity==6.1.0 pydantic==1.4 emails==0.5.15 raven==6.10.0 gunicorn==20.0.4 jinja2==2.11.2 psycopg2-binary==2.8.5 alembic==1.4.2 sqlalchemy==1.3.16 pytest==5.4.1 python-jose==3.1.0 mypy==0.770 black==19.10b0 isort==4.3.21 autoflake==1.3.1 flake8==3.7.9 pytest==5.4.1 sqlalchemy-stubs==0.3 pytest-cov==2.8.1 bcrypt==3.2.0 ``` **Dockerfile** 文件在`frontend`目录下,这里把npm下载源替换成国内 ``` # RUN npm install RUN npm --registry https://registry.npm.taobao.org info underscore && \ npm install --registry=https://registry.npm.taobao.org ``` **daemon.json** 文件在`C:\Users\ly\.docker`,其他系统的自己查一下,在文件中设置镜像下载地址,如果不设置的话,下载镜像会很慢很可能超时报错 ```json { // 追加代码,原来的配置不要删掉 "registry-mirrors": [ "https://1nj0zren.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn", "http://f1361db2.m.daocloud.io", "https://dockerhub.azk8s.cn" ] } ``` 如果你是windows系统,也可以在`Docker`软件里面设置 ![输入图片说明](img/4.png) ### 启动项目 启动`docker`服务,双击docker程序就能启动了,然后在项目目录下执行: ```shell docker-compose up -d ``` 如果你是第一次启动会下载镜像,我这里下了20几分钟。 启动完成,没有报错的话就可以在浏览器中访问了:http://localhost 我的镜像已经安装好了,这里直接开启服务 ![cmd](img/5.png) ![输入图片说明](img/6.png) 如果报错的话,看看是不是下载镜像超时,还是安装依赖的时候报错,下载超时看看`daemon.json`有没有设置好,不确定的话就重新`cookiecutter`生成新项目,把上面的文件全部替换掉。 ### 其他 访问页面的时候需要**登录**,密码保存在项目下`.env`文件里了,不同的页面需要不同的密码,这里写一些我的密码,账号默认会根据项目名称来生成 - http://localhost ``` FIRST_SUPERUSER=admin@ly.com FIRST_SUPERUSER_PASSWORD=changethis ``` - http://localhost:5050 ``` PGADMIN_DEFAULT_EMAIL=admin@ly.com PGADMIN_DEFAULT_PASSWORD=changethis ``` - http://localhost:5555 ``` FLOWER_BASIC_AUTH=admin:changethis ``` - 更多url在项目的`README.md`里面有写