Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,45 @@ ENV SQLALCHEMY_DATABASE_URI="sqlite:////app/tmp/dev.db"

WORKDIR /app

RUN apk update && apk add make
RUN python -m venv /app/venv

RUN apk update && apk add --no-cache nodejs npm make uv

COPY . .
RUN /app/venv/bin/python3 -m pip install -r requirements.txt
RUN /app/venv/bin/python3 -m pip install -r dev.txt


RUN source /app/venv/bin/activate && make check
RUN uv sync && make check && make test

FROM python:3.10-alpine AS build-image
FROM python:3.10-alpine

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV FLASK_APP="blog"
ENV SQLALCHEMY_DATABASE_URI="sqlite:////app/tmp/dev.db"

WORKDIR /app


RUN python -m venv /app/venv

COPY requirements.txt .
RUN /app/venv/bin/python3 -m pip install -r requirements.txt



FROM python:3.10-alpine
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN apk update && apk add --no-cache uv

WORKDIR /app

ARG UID=10001
ARG UID=10000
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--home "/app" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
# --no-create-home \
# appuser

USER appuser
COPY --from=build-image /app/venv /app/venv

# Copy the source code into the container.
COPY . .

RUN uv sync --no-dev -n
# Expose the port that the application listens on.
EXPOSE 5000

ENV PATH=/app/venv/bin:$PATH

#USER appuser

# Run the application.
ENTRYPOINT [ "./entrypoint.sh" ]
CMD [ "gunicorn -c gunicorn.py 'app:create_app()'" ]
#CMD [ "uv", "run gunicorn -c gunicorn.py "]

10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 0.0.6
VERSION = 0.0.7


all: check
Expand All @@ -16,6 +16,10 @@ lint-types:
uv run pyright .


test:
FLASK_ENV=testing FLASK_APP=blog uv run pytest


test-coverage:
( \
. venv/bin/activate;\
Expand All @@ -25,8 +29,8 @@ test-coverage:
check: lint test

run:
flask db upgrade
flask run --host="0.0.0.0" --debug
uv run flask db upgrade
uv run flask run --host="0.0.0.0" --debug

docker-build:
docker build . --tag="gunlinux:$(VERSION)"
Expand Down
7 changes: 4 additions & 3 deletions blog/category/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""SqlAlchemy models."""

from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING

from sqlalchemy.orm import Mapped, mapped_column, relationship

Expand All @@ -17,7 +17,8 @@ class Category(db.Model):
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(default="")
alias: Mapped[str] = mapped_column(unique=True)
posts: Mapped[List["Post"]] = relationship()
posts: Mapped[list["Post"]] = relationship()
template: Mapped[str] = mapped_column(nullable=True)

def __str__(self):
return f"Category(id={self.id}, title={self.title})"
return f"Category(id={self.id}, title={self.title}, template={self.template})"
8 changes: 6 additions & 2 deletions blog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Config(object):
SECRET_KEY = environ.get("SECRET_KEY") or "hard to guess string"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = False
PAGE_CATEGORY = int(environ.get("PAGE_CATEGORY", 0))
PAGE_CATEGORY: list[int] = [
int(c) for c in environ.get("PAGE_CATEGORY", "0").split(",")
]


class DevelopmentConfig(Config):
Expand All @@ -23,7 +25,9 @@ class DevelopmentConfig(Config):
class TestingConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
PAGE_CATEGORY = 1
PAGE_CATEGORY: list[int] = [
1,
]


class ProductionConfig(Config):
Expand Down
13 changes: 10 additions & 3 deletions blog/post/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from blog import cache, db
from blog.post.models import Post
from blog.category.models import Category

post = Blueprint("postb", __name__)

Expand All @@ -23,7 +24,7 @@ def pages_gen(f):
@wraps(f)
def decorated_function(*args, **kwargs):
page_category = current_app.config["PAGE_CATEGORY"]
pages_query = sa.select(Post).where(Post.category_id == page_category)
pages_query = sa.select(Post).where(Post.category_id.in_(page_category))
pages = db.session.scalars(pages_query).all()
return f(pages=pages, *args, **kwargs)

Expand All @@ -46,12 +47,18 @@ def index(**kwargs):
@cache.cached(timeout=50)
@pages_gen
def view(alias=None, **kwargs):
page_category = current_app.config["PAGE_CATEGORY"]
page_categories = current_app.config["PAGE_CATEGORY"] # (1,3,)
post_query = sa.select(Post).where(
or_(Post.publishedon.isnot(None), Post.category_id == page_category),
or_(Post.publishedon.isnot(None), Post.category_id.in_(page_categories)),
Post.alias == alias,
)
post = db.first_or_404(post_query)

page_category_obj = db.session.scalars(
sa.select(Category).where(Category.id == post.category_id)
).first()
if page_category_obj:
return render_template(page_category_obj.template, post=post, **kwargs)
return render_template("post.html", post=post, **kwargs)


Expand Down
34 changes: 34 additions & 0 deletions blog/static/src/components/refs.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.refs {
padding: 0.5rem 0;
}

.refs__links {

}

.refs__links_desc {

}

.refs__link_itemlink {
transition: all 0.2s ease-in-out;
color: #333;
text-align: center;
text-decoration: none;
border-bottom: 1px solid #ccc;
font-size: 1.3em;
}

.refs__link_itemlink:hover {
border-color: #000;
color: #000;
}



}

.refs__link_itemlink {

}

1 change: 1 addition & 0 deletions blog/static/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
@import 'components/nav.css';
@import 'components/works.css';
@import 'components/work.css';
@import 'components/refs.css';
1 change: 1 addition & 0 deletions blog/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<link rel="stylesheet" href="/static/src/components/post.css">
<link rel="stylesheet" href="/static/src/components/nav.css">
<link rel="stylesheet" href="/static/src/components/tags.css">
<link rel="stylesheet" href="/static/src/components/refs.css">
{% block styles %}
{% endblock %}
</head>
Expand Down
7 changes: 5 additions & 2 deletions blog/templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<h1 class="post__title">{{post.pagetitle}}</h1>
{% if post.category_id != config['PAGE_CATEGORY'] %}
<div class="post__date">
{{post.publishedon.strftime("%B %d, %Y")}}
{% if post.publishedon %}
{{post.publishedon.strftime("%B %d, %Y")}}
{% endif %}
</div>
{% endif %}

Expand All @@ -30,4 +32,5 @@ <h1 class="post__title">{{post.pagetitle}}</h1>
{% block scripts %}
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<script>
hljs.initHighlightingOnLoad(); </script> {% endblock %}
hljs.initHighlightingOnLoad(); </script>
{% endblock %}
47 changes: 47 additions & 0 deletions blog/templates/refs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends "layout.html" %}

{% block styles %}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
{% endblock %}

{% block content %}
<article class="page__content post">
<div class="refs">
<div class="refs__desc">
{{ post.markdown|safe }}
</div>

<ul class="refs__links">

<li class="refs__links_item">
<a target="_blank" href="https://ru.hexlet.io/programs/python-basics-free"
class="refs__link_itemlink">🐍 Бесплатный курс по Python, уважаем hexlet.io</a>
</li>
<li class="refs__links_item">
<a target="_blank" href="https://sql-academy.org/ru/guide"
class="refs__link_itemlink">🗃️ Бесплатные курс по sql у меня и сертификатик есть</a>
</li>
<li class="refs__links_item">
<a target="_blank" href="https://skyeng.ru/referral/?source_type=referral&inviterHash=4d6a497a4d6a51784f54593d" class="refs__link_itemlink">🇬🇧 Учим язык чтобы жить и работать не в этой стране</a>
</li>
<li class="refs__links_item refslink">
<a target="_blank" href="https://www.tbank.ru/baf/3vfvMSwmZ8s" class="refs__link_itemlink">💸 Тинькофф — пользуюсь годами, там же инвестирую (пока не просадил всё)</a>
</li>
<li class="refs__links_item refslink">
<a target="_blank" href="https://yasno.live/r/n9YHv8Pn" class="refs__link_itemlink">🧠 Следим за кукухой — потому что психическое здоровье дороже курса биткоина.
</a>
</li>



<li target="_blank" class="refs__links_item refslink">
<a target="_blank" class="refs__link_itemlink" href="https://donationalerts.com/u/gunlinux">🫂 Базированный донат — если вдруг рефки не ваш стиль, но поддержать охота.
</a>
</li>
</ul>
</div>
</article>
{% endblock %}

{% block scripts %}
{% endblock %}
8 changes: 4 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/sh

echo "wait until database started"
while ! nc -z $DB_HOST $DB_PORT; do sleep 1; done
#while ! nc -z $DB_HOST $DB_PORT; do sleep 1; done

source /app/venv/bin/activate
#source /app/venv/bin/activate
echo "run migrates"
FLASK_APP=blog /app/venv/bin/flask db upgrade
FLASK_APP=blog uv run flask db upgrade

exec "$@"
exec uv run gunicorn -c gunicorn.py # "$@"
36 changes: 36 additions & 0 deletions migrations/versions/49df8e610d33_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""empty message
Revision ID: 49df8e610d33
Revises: 81d804b1c18c
Create Date: 2025-06-21 23:24:05.447521

"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "49df8e610d33"
down_revision = "81d804b1c18c"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("categories", schema=None) as batch_op:
batch_op.add_column(
sa.Column(
"template", sa.String(), server_default="post.html", nullable=True
)
)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("categories", schema=None) as batch_op:
batch_op.drop_column("template")

# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ dependencies = [
"gevent>=25.5.1",
"gunicorn>=23.0.0",
"markdown>=3.7",
"psycopg2>=2.9.10",
"psycopg2-binary>=2.9.10",
"python-dotenv>=1.0.1",
"wtforms==2.3.3",
]

[project.scripts]
Expand Down
Loading