Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit debe0a3

Browse files
authored
Merge pull request #16 from Desunovu/dev
new features
2 parents aa7cbc4 + 3ebfd95 commit debe0a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2674
-649
lines changed

.env.development

Lines changed: 0 additions & 8 deletions
This file was deleted.

.env.development.example

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### FASTAPI BACKEND
2+
3+
# blogapp secret key
4+
FASTAPI_SECRET_KEY=someSecretKey
5+
# chatgpt api key, could be empty if using neuroapi or alternative
6+
FASTAPI_CHATGPT_API_KEY=someApiKey
7+
# chatgpt alternative base url, should be empty if base should be original
8+
FASTAPI_CHATGPT_ALTERNATIVE_BASE=https://neuroapi.host/v1
9+
# blogapp mode, efforts on how .env file is loading and etc
10+
FASTAPI_APP_MODE=DEV
11+
# enables creation of test users (Admin, Author, Reader)
12+
FASTAPI_CREATE_TEST_USERS=True
13+
# set logging level
14+
FASTAPI_LOGGING_LEVEL=DEBUG
15+
16+
FASTAPI_MONGODB_URL=mongodb://localhost:27017
17+
FASTAPI_MONGODB_DATABASE=blogdatabase
18+
19+
### VITE VUE FRONTEND
20+
VITE_API_URL="http://localhost:8000"

.env.production

Lines changed: 0 additions & 12 deletions
This file was deleted.

.env.production.example

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# FASTAPI BACKEND
2+
3+
# blogapp secret key
4+
FASTAPI_SECRET_KEY=someSecretKey
5+
# chatgpt api key, could be empty if using neuroapi or alternative
6+
FASTAPI_CHATGPT_API_KEY=someApiKey
7+
# chatgpt alternative base url, should be empty if base should be original
8+
FASTAPI_CHATGPT_ALTERNATIVE_BASE=https://neuroapi.host/v1
9+
# blogapp mode, efforts on how .env file is loading and etc
10+
FASTAPI_APP_MODE=DEV
11+
# enables creation of test users (Admin, Author, Reader)
12+
FASTAPI_CREATE_TEST_USERS=True
13+
# set logging level
14+
FASTAPI_LOGGING_LEVEL=DEBUG
15+
16+
FASTAPI_MONGODB_URL=mongodb://mongodb:27017
17+
FASTAPI_MONGODB_DATABASE=blogdatabase
18+
19+
### VITE VUE FRONTEND
20+
VITE_API_URL="/api"
21+
22+
## MONGO
23+
#MONGO_INITDB_ROOT_USERNAME:
24+
#MONGO_INITDB_ROOT_PASSWORD:

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ venv/
44
*.pyc
55

66
# env
7-
*.env
7+
*.env.*
8+
!.env.development.example
9+
!.env.production.example
810

911
# Logs
1012
logs
@@ -34,3 +36,4 @@ coverage
3436
*.njsproj
3537
*.sln
3638
*.sw?
39+
test.py

README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ This project is an educational project for developing a blog using Fast API, Vue
1010
- **Backend Web Framework**: [FastAPI](https://fastapi.tiangolo.com/)
1111
- **Database**: MongoDB (used with [Beanie ODM](https://beanie-odm.dev/))
1212
- **Frontend Web Framework**: Vue.js (with [Quasar](https://quasar.dev/), [Pinia](https://pinia.vuejs.org/), [OpenAPI Typescript Codegen](https://github.com/ferdikoomen/openapi-typescript-codegen))
13-
- **Honorable mention**: OAuth 2.0 JWT Bearer Flow, Nginx
13+
- **Honorable mention**: OAuth 2.0 JWT Bearer Flow, Nginx, Chat completions API
1414

1515
## Project Functionality
1616
1. **Registration and Authentication**: Users can register and authenticate using OAuth 2.0 JWT.
1717
2. **Profile Management**: Users can manage their profiles, including changing passwords and updating information. Administrators can also manage users.
18-
3. **Blog Management**: Users can create, edit, and delete blog articles.
18+
3. **Blog Management**: Authors can create, edit, and delete blog articles.
1919
4. **Viewing Articles**: Articles can be viewed as a list and in detail. Pagination and sorting are supported for displaying a large number of articles.
2020
5. **Tags and Filters**: Articles can be filtered by tags, and users can add tags to articles.
2121
6. **Search**: Search for articles using keywords.
2222
7. **Comments**: Users can add comments and reply to them.
23+
8. **Article Generation**: Admins can generate articles using the OpenAI chat completion API.
2324

2425

2526
## Installation and Local Setup
@@ -31,19 +32,39 @@ To run this project locally, you can use Docker Compose. Make sure you have Dock
3132
git clone https://github.com/Desunovu/fastapi-vue-blog.git
3233
cd fastapi-vue-blog
3334
```
34-
2. Open the `.env.production` file located in the project's root directory using a text editor. Set the `SECRET_KEY` variable in this file to a secure secret key for your application. For example:
35+
36+
37+
2. Rename the example environment files located in the project's root directory:
38+
- Rename `.env.production.example` to `.env.production`
39+
- Rename `.env.development.example` to `.env.development`
40+
41+
42+
3. Open the newly created .env.production and .env.development files using a text editor. In these files, you can customize the variables according to your preferences. For example:
3543

3644
```dotenv
37-
SECRET_KEY=mysecretkey
45+
FASTAPI_SECRET_KEY=mysecretkey
46+
FASTAPI_CHATGPT_ALTERNATIVE_BASE=https://neuroapi.host/v1
47+
FASTAPI_CHATGPT_API_KEY=someapikey
48+
FASTAPI_CREATE_TEST_USERS=true
49+
FASTAPI_LOGGING_LEVEL=debug
3850
```
39-
Ensure that the SECRET_KEY value is kept secret and not shared publicly.
4051

4152

42-
3. Build and start the application containers using Docker Compose:
53+
4. Build and start the application containers using Docker Compose. You can specify profiles using the --profile argument as follows:
54+
- To deploy the full application stack, use:
55+
```bash
56+
docker-compose --profile full-deploy up --build
57+
```
58+
- To run only the FastAPI and MongoDB containers, use:
59+
```bash
60+
docker-compose --profile backend-only up --build
61+
```
62+
- To run only the MongoDB container, use:
4363
```bash
44-
docker-compose up --build
64+
docker-compose --profile mongodb-only up --build
4565
```
46-
This command will download the necessary Docker images, build the application, and start the containers.
66+
67+
This command will download the necessary Docker images, build the application, and start the containers.
4768

4869
## Accessing the Application
4970
- Vue App: http://localhost:8080

backend/blogapp/core/config.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import logging
12
import os
23
import time
34
from pathlib import Path
45

56
from dotenv import load_dotenv
67

7-
mode = os.getenv("APP_MODE", "development")
8-
if mode == "development":
9-
# Загрузка переменных окружения из файла ".env.development в /backend"
8+
log = logging.getLogger("blogapp")
9+
10+
mode = os.getenv("FASTAPI_APP_MODE", "DEV")
11+
if mode == "DEV":
12+
# Загрузка переменных окружения из файла ".env.development.example в /backend"
1013
# Не перезаписывает уже заданные переменные.
1114
# Переменные для prod устанавливаются в docker-compose.yml
1215
env_path = os.path.join(
@@ -15,16 +18,21 @@
1518
),
1619
".env.development",
1720
)
18-
print(f"[DEV MODE] Путь к файлу .env: {env_path}")
21+
log.debug(f"[DEV MODE] Путь к файлу .env: {env_path}")
1922
load_dotenv_result = load_dotenv(dotenv_path=env_path)
20-
elif mode == "production":
21-
print(f"[PROD MODE]")
23+
elif mode == "PROD":
24+
log.debug(f"[PROD MODE]")
25+
26+
# Получение переменных окружения приложения
27+
FASTAPI_SECRET_KEY = os.getenv("FASTAPI_SECRET_KEY", "")
28+
29+
FASTAPI_CHATGPT_ALTERNATIVE_BASE = os.getenv("FASTAPI_CHATGPT_ALTERNATIVE_BASE", "")
30+
FASTAPI_CHATGPT_API_KEY = os.getenv("FASTAPI_CHATGPT_API_KEY", "")
2231

23-
# Определение констант из переменных окружения
24-
SECRET_KEY = os.getenv("SECRET_KEY")
25-
LOGGING_LEVEL = os.getenv("LOGGING_LEVEL")
26-
MONGODB_URL = os.getenv("MONGODB_URL", "")
27-
MONGODB_DATABASE = os.getenv("MONGODB_DATABASE")
32+
FASTAPI_CREATE_TEST_USERS = os.getenv("FASTAPI_CREATE_TEST_USERS", "DEV")
33+
FASTAPI_LOGGING_LEVEL = os.getenv("FASTAPI_LOGGING_LEVEL", "DEBUG")
34+
FASTAPI_MONGODB_URL = os.getenv("FASTAPI_MONGODB_URL", "")
35+
FASTAPI_MONGODB_DATABASE = os.getenv("FASTAPI_MONGODB_DATABASE", "")
2836

2937
# Прочие константы
3038
ALGORITHM = "HS256"
@@ -38,3 +46,10 @@
3846
LOG_FORMAT: str = "%(levelprefix)s | %(asctime)s | %(message)s"
3947
LOGS_DIR_PATH: Path = BACKEND_DIR_PATH / "logs"
4048
LOG_FILE_PATH: Path = LOGS_DIR_PATH / f"""{time.strftime("%Y%m%d-%H%M%S")}.log"""
49+
50+
# Аватары пользователей
51+
DEFAULT_AVATAR_URL: str = "https://cdn-icons-png.flaticon.com/512/149/149071.png"
52+
AVATAR_PROVIDER_URL: str = "https://cdn-icons-png.flaticon.com"
53+
54+
# Параметры статей
55+
ARTICLE_MAX_LENGTH: int = 10000

backend/blogapp/core/database/mongodb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
from beanie import init_beanie
22
from motor.motor_asyncio import AsyncIOMotorClient
33

4-
from ..config import MONGODB_URL, MONGODB_DATABASE
4+
from ..config import FASTAPI_MONGODB_URL, FASTAPI_MONGODB_DATABASE
55
from ...modules.articles.models import ArticleDocument
66
from ...modules.comments.models import CommentDocument
77
from ...modules.users.models import UserDocument
88

99

1010
# Init beanie ODM
1111
async def init_odm():
12-
mongo_client = AsyncIOMotorClient(MONGODB_URL)
12+
mongo_client = AsyncIOMotorClient(FASTAPI_MONGODB_URL)
1313
await init_beanie(
14-
database=mongo_client[MONGODB_DATABASE],
14+
database=mongo_client[FASTAPI_MONGODB_DATABASE],
1515
document_models=[
1616
ArticleDocument,
1717
UserDocument,

backend/blogapp/core/logging.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
from pydantic import BaseModel
44

5-
from .config import LOGS_DIR_PATH, LOG_FILE_PATH, LOGGING_LEVEL, LOG_FORMAT, LOGGER_NAME
5+
from .config import (
6+
LOGS_DIR_PATH,
7+
LOG_FILE_PATH,
8+
FASTAPI_LOGGING_LEVEL,
9+
LOG_FORMAT,
10+
LOGGER_NAME,
11+
)
612

713

814
# From https://stackoverflow.com/questions/63510041/adding-python-logging-to-fastapi-endpoints-hosted-on-docker-doesnt-display-api
@@ -25,9 +31,14 @@ class LogConfig(BaseModel):
2531
"formatter": "default",
2632
"filename": LOG_FILE_PATH,
2733
},
34+
"console": {
35+
"class": "logging.StreamHandler",
36+
"formatter": "default",
37+
"stream": "ext://sys.stdout",
38+
},
2839
}
2940
loggers: dict = {
30-
LOGGER_NAME: {"handlers": ["file"], "level": LOGGING_LEVEL},
41+
LOGGER_NAME: {"handlers": ["file", "console"], "level": FASTAPI_LOGGING_LEVEL},
3142
}
3243

3344

backend/blogapp/core/security/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class UserBase(ABC, BaseModel):
1616
created_at: datetime | None
1717
updated_at: datetime | None
1818
password_hash: str | None
19+
avatar_url: str | None
1920

2021

2122
class TokenResponseBody(BaseModel):

0 commit comments

Comments
 (0)