Skip to content

Commit

Permalink
substitute markdown to html with rust for performance and build
Browse files Browse the repository at this point in the history
  • Loading branch information
cashmeresamurai committed Jun 18, 2024
1 parent 8a80ce9 commit 647c6d7
Show file tree
Hide file tree
Showing 23 changed files with 2,705 additions and 155 deletions.
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source_url "https://raw.githubusercontent.com/cachix/devenv/95f329d49a8a5289d31e0982652f7058a189bfca/direnvrc" "sha256-d+8cBpDfDBj41inrADaJt+bDWhOktwslgoP5YiGJ1v0="

use devenv
40 changes: 40 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build and Publish Wheels

on: [push, pull_request]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.7, 3.8, 3.9, 3.10, 3.11]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Install maturin
run: pip install maturin

- name: Build wheels
run: maturin build --release

- name: Show contents of target/wheels
run: ls target/wheels

- name: Upload wheels as artifacts
uses: actions/upload-artifact@v2
with:
name: wheels
path: target/wheels/*.whl
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ users.json
users.json
*.pyc
users.json
# Devenv
.devenv*
devenv.local.nix

# direnv
.direnv

# pre-commit
.pre-commit-config.yaml
2 changes: 1 addition & 1 deletion app/data_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ def get_skills_and_expertise():
skills_user.append(skills_expertise[secrange])
return skills_user

# gen_user(99)
gen_user(1)
32 changes: 32 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
save_json,
save_session,
)

from fastapi.exceptions import RequestValidationError
from fastapi.exception_handlers import request_validation_exception_handler
from fastapi_login import LoginManager

from dotenv import load_dotenv
import os

from app.mkhtml import markdown_to_html

app = FastAPI()
templates = Jinja2Templates(directory="templates")
components = Jinja2Templates(directory="templates/components")
Expand Down Expand Up @@ -240,6 +245,33 @@ async def htmx_get_fn(request: Request):
}
return components.TemplateResponse("result.html", context)

@app.post("/format-markdown", response_class=HTMLResponse)
async def format_markdown(
request: Request,
search: str = Form(...),
):
try:
markdown_formatted = markdown_to_html(search)
print(markdown_formatted)
return HTMLResponse(content=markdown_formatted, status_code=200)
except RequestValidationError as e:
# Handle the validation error and return an empty response
await request_validation_exception_handler(request, e)
return HTMLResponse(content="", status_code=200)


@app.get("/editor", response_class=HTMLResponse)
async def editor(request: Request):
scope = request.scope["htmx"]
# print(scope.target)
# print(scope.current_url)
context = {
"request": request,
# "htmx_name": "this is inhereted from htmx",
# "htmx_value": "this value is inhereted from htmx",
}
return templates.TemplateResponse("editor.html", context)


@app.get("/settings", response_class=HTMLResponse)
async def settings(request: Request, user=Depends(manager)):
Expand Down
5 changes: 5 additions & 0 deletions app/mkhtml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import markdown_fastapi_rs


def markdown_to_html(input: str) -> str:
return markdown_fastapi_rs.md_to_html(input)
169 changes: 169 additions & 0 deletions markdown-fastapi-rs/.github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# This file is autogenerated by maturin v1.6.0
# To update, run
#
# maturin generate-ci github
#
name: CI

on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
linux:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-latest
target: x86_64
- runner: ubuntu-latest
target: x86
- runner: ubuntu-latest
target: aarch64
- runner: ubuntu-latest
target: armv7
- runner: ubuntu-latest
target: s390x
- runner: ubuntu-latest
target: ppc64le
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-linux-${{ matrix.platform.target }}
path: dist

musllinux:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-latest
target: x86_64
- runner: ubuntu-latest
target: x86
- runner: ubuntu-latest
target: aarch64
- runner: ubuntu-latest
target: armv7
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
manylinux: musllinux_1_2
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-musllinux-${{ matrix.platform.target }}
path: dist

windows:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: windows-latest
target: x64
- runner: windows-latest
target: x86
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
architecture: ${{ matrix.platform.target }}
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-windows-${{ matrix.platform.target }}
path: dist

macos:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: macos-12
target: x86_64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-macos-${{ matrix.platform.target }}
path: dist

sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
- name: Upload sdist
uses: actions/upload-artifact@v4
with:
name: wheels-sdist
path: dist

release:
name: Release
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
needs: [linux, musllinux, windows, macos, sdist]
steps:
- uses: actions/download-artifact@v4
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
env:
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
with:
command: upload
args: --non-interactive --skip-existing wheels-*/*
72 changes: 72 additions & 0 deletions markdown-fastapi-rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/target

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
Loading

0 comments on commit 647c6d7

Please sign in to comment.