Git Product home page Git Product logo

vue3-fastapi's Introduction

Vue3+ FastAPI Demo

1.项目目录

-- backend										# 后端
	-- api									    # 接口文件夹
  -- common                   # 公共文件夹
  -- core                     # 核心文件夹
    -- config.py              # 配置文件夹
  -- crud                     # 数据库增删改查文件夹
  -- models                   
    -- database               # mysql 表模型
    -- redis                  # redis 表模型
  -- register                 # 注册中心
  -- schemas                  # 模型文件夹 (Java中的实体类或者VO视图类)
  -- static                   # 静态文件夹
  -- utils                    # 工具文件夹
  -- Dockerfile               # 后端服务部署文件
  -- main.py                  # 项目启动文件
  -- requirements.txt         # 所需的包
-- frontend
  -- src
    -- apis                   # 接口文件夹
    -- assets                 # 静态资源文件夹
      -- js
        -- global.js          # 全局配置文件
    -- components             # 封装的组件
    -- request                # 封装的axios
    -- router                 # 路由
    -- stores                 # 状态管理
    -- utils                  # 工具类
    -- views                  # 页面
      -- pages                # 布局页面中的 内容
      -- Login.vue            # 布局页面
    -- App.vue
    -- main.ts                
  -- index.html               # 项目入口
-- demo.sql                   # 数据库 (backend中config.py)
-- docker-compose.yml         # 所有项目部署配置 (nginx, mysql, redis, backend)

2.项目启动

  • 后端

    # 安装包 (进入到 backend 文件夹)
    pip install -r ./requirements.txt
    
    # 找到 main.py 中的 主函数, 右键启动
  • 前端

    # 安装包 (进入到 frontend 文件夹)
    npm install
    
    # 启动项目
    npm run start

部署相关

1.项目目录

-- 服务器
	-- ...
	-- root
	-- opt
		-- containerd 
		-- docker 								# 存放docker容器配置
			-- mysql							# mysql 配置
			-- nginx							# nginx 配置
				-- conf.d						
					-- default.conf				 # nginx 配置 【重要】
				-- html							# 存放打包后的文件 【重要】
				-- ...
			-- redis							# redis
			-- demo								# 项目配置
				-- frontend						# 前端
				-- backend						# 后端
					-- Dockerfile				# 构建镜像【重要】 
				-- docker-compose.yml			 # docker-compose 【重要】
				-- ...
			-- ...

2. Dcoker浅学

3. 前端部署

  • frontend/src/assets/js/global.ts 文件中 API_URL_PRODUCTION 字段改为服务器 IP

  • 修改 frontend/src/request/https.ts 文件中的 isDev 字段为 false

    // 是否是开发环境
    // const isDev: boolean = true;
    const isDev: boolean = false;
  • 运行 npm run build 命令打包文件

  • frontend/dist 中的文件上传到服务器 /opt/docker/nginx/html 文件夹下

4. 后端部署

  • 修改 backend/core/config.py 中的 IS_DEV 字段为 false

    # IS_DEV = True  # 是否开发环境
    IS_DEV = False  # 是否开发环境
  • 修改 backend/core/config.py 中的部分字段

  • 使用 Docker 创建网络桥段

    # docker network ls 查看网络桥段
    # docker network create 桥段名 
    
    # app 为 docker-compose.yml 中定义的桥段名
    docker network create app
  • 运行 /opt/docker/demo 文件下的 docker-compose.yml

    docoker-compose -f /opt/docker/demo/docker-compose.yml up -d
  • 创建名为 demo 的数据库, 并导入 demo.sql

5. Nginx 配置 ( /nginx/conf.d/default.conf )

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html; # 防止页面刷新404
    }
    
     location /static/avatar/ {
        proxy_pass http://fastapi:8000/static/avatar/;
    }

    location /api {
        client_max_body_size 5m;
        proxy_pass http://fastapi:8000; # 这里 localhost -> 对应的容器名
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    # 可选
    location /api/docs { # docs 文档地址
        proxy_pass http://fastapi:8000/docs;
    }

    location /api/redoc { # redoc 文档地址
        proxy_pass http://fastapi:8000/redoc;
    }

    location /openapi.json { # openapi 地址 (如果代理上述文档地址, 请务必添加 openapi 的代理)
        proxy_pass http://fastapi:8000/openapi.json;
    }

    ...
}

vue3-fastapi's People

Contributors

zxiaosi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue3-fastapi's Issues

登录时报错

PS D:\ASIC\vue3-fastapi\backend> d:; cd 'd:\ASIC\vue3-fastapi\backend'; & 'c:\Python311\python.exe' 'c:\Users\yahui.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '52015' '--' 'D:\ASIC\vue3-fastapi\backend\main.py'
2024-02-17 09:05:31.803 | INFO | core.init_db:drop_table:41 - 删除表成功!!!
2024-02-17 09:05:32.538 | INFO | core.init_db:init_table:32 - 创建表成功!!!
2024-02-17 09:05:32.603 | INFO | main::28 - 项目启动成功!!!
INFO: Started server process [31220]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:52874 - "OPTIONS /api/user/login HTTP/1.1" 200 OK
2024-02-17 09:18:55.013 | ERROR | register.exception:http_exception_handler:103 - 全局异常: URL:http://127.0.0.1:8000/api/user/login
Headers:Headers({'host': '127.0.0.1:8000', 'connection': 'keep-alive', 'content-length': '202', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"', 'accept': 'application/json, text/plain, /', 'content-type': 'application/json', 'sec-ch-ua-mobile': '?0', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'sec-ch-ua-platform': '"Windows"', 'origin': 'http://127.0.0.1:5173', 'sec-fetch-site': 'same-site', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', 'referer': 'http://127.0.0.1:5173/', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'})
Traceback (most recent call last):
File "C:\Users\yahui\AppData\Roaming\Python\Python311\site-packages\starlette\middleware\errors.py", line 162, in call
await self.app(scope, receive, _send)
File "C:\Users\yahui\AppData\Roaming\Python\Python311\site-packages\starlette\middleware\cors.py", line 92, in call
await self.simple_response(scope, receive, send, request_headers=headers)
File "C:\Users\yahui\AppData\Roaming\Python\Python311\site-packages\starlette\middleware\cors.py", line 147, in simple_response
await self.app(scope, receive, send)
File "C:\Users\yahui\AppData\Roaming\Python\Python311\site-packages\starlette\middleware\exceptions.py", line 79, in call
raise exc
File "C:\Users\yahui\AppData\Roaming\Python\Python311\site-packages\starlette\middleware\exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "c:\Python311\Lib\contextlib.py", line 222, in aexit
await self.gen.athrow(typ, value, traceback)
File "C:\Users\yahui\AppData\Roaming\Python\Python311\site-packages\fastapi\concurrency.py", line 36, in contextmanager_in_threadpool
raise e
TypeError: 'NoneType' object is not subscriptable

你好请教一下,我现在点击登录时后台报错,根据断点定位到报错是/utils/handle_cookie.py里的def set_cookie方法里创建LocalUser(**_user, pk=session)报错,请问可能是什么原因

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.