Compare commits

..

1 Commits
fzoz ... mpabi

Author SHA1 Message Date
u1 4e1b40521b u 2024-11-06 09:26:14 +00:00
11 changed files with 117 additions and 216 deletions

View File

@ -1,7 +1,7 @@
# Choose a base image. Debian is a good choice due to pyenv # Wybierz bazowy obraz. Debian jest dobrym wyborem ze względu na pyenv
FROM debian:latest FROM debian:latest
# Update the package list and install necessary dependencies # Aktualizacja listy pakietów i instalacja niezbędnych zależności
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
aptitude \ aptitude \
tmux vim-nox nano mc git curl termshark procps \ tmux vim-nox nano mc git curl termshark procps \
@ -13,71 +13,67 @@ RUN apt-get update && apt-get install -y \
python3-pip \ python3-pip \
npm npm
# Set password for root user (optional, for configuration) # Ustawienie hasła dla użytkownika root (opcjonalne, do konfiguracji)
RUN echo 'root:rootpass' | chpasswd RUN echo 'root:rootpass' | chpasswd
# Add a new user `user` with password `pass` and prepare the environment # Dodanie nowego użytkownika `user` z hasłem `pass` i przygotowanie środowiska
RUN useradd -m user && echo 'user:pass' | chpasswd && adduser user sudo RUN useradd -m user && echo 'user:pass' | chpasswd && adduser user sudo
# Switch to user `user` # Przełączenie na użytkownika `user`
USER user USER user
WORKDIR /home/user WORKDIR /home/user
# Install pyenv # Ustawienie zmiennych środowiskowych
RUN git clone https://github.com/pyenv/pyenv .pyenv
# Install pyenv-virtualenv
RUN git clone https://github.com/pyenv/pyenv-virtualenv .pyenv/plugins/pyenv-virtualenv
# Set environment variables
ENV HOME /home/user ENV HOME /home/user
ENV PYENV_ROOT $HOME/.pyenv ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$HOME/.local/bin:$PATH
ENV PYENV_VERSION p3.12
# Add pyenv and pyenv-virtualenv configuration to .bashrc # Instalacja pyenv
RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> .bashrc RUN git clone https://github.com/pyenv/pyenv $PYENV_ROOT
RUN echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> .bashrc
RUN echo 'eval "$(pyenv init -)"' >> .bashrc
RUN echo 'eval "$(pyenv virtualenv-init -)"' >> .bashrc
# Install Python 3.12 with --enable-shared # Instalacja pyenv-virtualenv
RUN bash -i -c "source ~/.bashrc && env PYTHON_CONFIGURE_OPTS='--enable-shared' pyenv install 3.12" RUN git clone https://github.com/pyenv/pyenv-virtualenv $PYENV_ROOT/plugins/pyenv-virtualenv
# Create a virtual environment p3.12 # Dodanie konfiguracji pyenv i pyenv-virtualenv do .bashrc
RUN bash -i -c "source ~/.bashrc && pyenv virtualenv 3.12 p3.12" RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc \
&& echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc \
&& echo 'eval "$(pyenv init --path)"' >> ~/.bashrc \
&& echo 'eval "$(pyenv init -)"' >> ~/.bashrc \
&& echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
# Set p3.12 as the default virtual environment # Instalacja Pythona 3.12 z --enable-shared
RUN bash -i -c "source ~/.bashrc && pyenv local p3.12" RUN env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.12.0
# Install ipython in the p3.12 environment # Utworzenie środowiska wirtualnego p3.12
RUN bash -i -c "source ~/.bashrc && pyenv activate p3.12 && pip install ipython" RUN pyenv virtualenv 3.12.0 p3.12
# Install Vim-Plug for Vim plugin management # Ustawienie p3.12 jako globalnego środowiska
RUN pyenv global p3.12
# Instalacja ipython w środowisku p3.12
RUN pip install ipython
# Instalacja Vim-Plug do zarządzania wtyczkami Vim
RUN curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ RUN curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
# Copy the vim configuration file for plugins # Kopiowanie plików konfiguracyjnych vim i tmux
COPY ./_confs/.vimrc /home/user/.vimrc COPY --chown=user:user ./_confs/.vimrc /home/user/.vimrc
COPY --chown=user:user ./_confs/.tmux.conf /home/user/.tmux.conf
# Install Vim plugins via Vim-Plug # Instalacja wtyczek Vim za pomocą Vim-Plug
RUN vim +'PlugInstall --sync' +qall RUN vim +'PlugInstall --sync' +qall
# Install Poetry # Instalacja Poetry w środowisku p3.12
RUN curl -sSL https://install.python-poetry.org | python3 - RUN curl -sSL https://install.python-poetry.org | python3 -
# Copy the project into the image # Kopiowanie projektu do obrazu
COPY --chown=user:user . /home/user/fapi COPY --chown=user:user . /home/user/fapi
COPY ./_confs/* ./
# Set the working directory for installing dependencies # Instalacja zależności za pomocą Poetry z głównego katalogu
WORKDIR /home/user/fapi RUN cd /home/user/fapi && poetry config virtualenvs.create false && poetry install
# Add Poetry path to PATH directly in the Dockerfile # Uruchomienie aplikacji
ENV PATH="$HOME/.local/bin:$PATH" CMD ["python", "/home/user/fapi/entrypoint.py"]
# Install dependencies using Poetry
RUN bash -i -c "source ~/.bashrc && poetry config virtualenvs.create false && poetry install"
# Run the application
CMD ["/bin/bash", "-c", "python entrypoint.py"]

5
__n1
View File

@ -1,8 +1,7 @@
sudo docker run --rm \ sudo docker run --rm \
-dit \ -dit \
--privileged \ --privileged \
-p 9999:9999 \
-p 8888:8888 \ -p 8888:8888 \
-v "$(pwd):/home/user/fapi" \ -v /home/user/work/fapi/fapi/src:/home/user/fapi \
--name b1 \ --name f1 \
fapi bash fapi bash

2
__run2
View File

@ -1 +1 @@
docker run --rm -it -p 9999:9999 --name cfapi fapi bash docker run --rm -it -p 8888:8888 --name cfapi fapi bash

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,7 +4,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
#SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # Dla SQLite #SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # Dla SQLite
SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.18.0.2:3306/test" SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.19.0.4:3306/test"
engine = create_engine( engine = create_engine(
SQLALCHEMY_DATABASE_URL, SQLALCHEMY_DATABASE_URL,

View File

@ -1,15 +1,25 @@
from fastapi import FastAPI, Depends, HTTPException from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from . import models, schemas from fastapi.middleware.cors import CORSMiddleware
from .database import SessionLocal, engine from app.database import SessionLocal, engine
from datetime import datetime import app.models as models
from app.schemas import Person, PersonCreate
app = FastAPI() app = FastAPI()
# Tworzenie tabel w bazie danych origins = [
models.Base.metadata.create_all(bind=engine) "http://localhost:8080",
"http://mpabi.pl:8888",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Dependency do sesji z bazą danych
def get_db(): def get_db():
db = SessionLocal() db = SessionLocal()
try: try:
@ -17,42 +27,31 @@ def get_db():
finally: finally:
db.close() db.close()
# Endpoint do pobierania ról # Endpoint do tworzenia nowej osoby (POST)
@app.get("/roles/", response_model=list[schemas.Role]) @app.post("/persons/", response_model=Person)
def read_roles(db: Session = Depends(get_db)): def create_person(person: PersonCreate, db: Session = Depends(get_db)):
return db.query(models.Role).all() db_person = models.Person(
last_name=person.last_name,
# Endpoint do dodania roli first_name=person.first_name,
@app.post("/roles/", response_model=schemas.Role) address=person.address,
def create_role(role: schemas.RoleCreate, db: Session = Depends(get_db)): city=person.city
db_role = models.Role(name=role.name)
db.add(db_role)
db.commit()
db.refresh(db_role)
return db_role
# Endpoint do pobierania zdarzeń
@app.get("/events/", response_model=list[schemas.Event])
def read_events(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
return db.query(models.Event).offset(skip).limit(limit).all()
# Endpoint do dodania zdarzenia
@app.post("/events/", response_model=schemas.Event)
def create_event(event: schemas.EventCreate, db: Session = Depends(get_db)):
db_event = models.Event(
role_id=event.role_id,
student_id=event.student_id,
criteria_id=event.criteria_id,
description=event.description,
event_date=event.event_date or datetime.now()
) )
db.add(db_event) db.add(db_person)
db.commit() db.commit()
db.refresh(db_event) db.refresh(db_person)
return db_event return db_person
# Endpoint do pobierania informacji o zdarzeniach dla konkretnego ucznia # Endpoint do odczytu listy osób (GET)
@app.get("/events/student/{student_id}", response_model=list[schemas.Event]) @app.get("/persons/", response_model=list[Person])
def read_student_events(student_id: int, db: Session = Depends(get_db)): def read_persons(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
return db.query(models.Event).filter(models.Event.student_id == student_id).all() persons = db.query(models.Person).offset(skip).limit(limit).all()
return persons
# Endpoint do odczytu osoby po ID (GET)
@app.get("/persons/{person_id}", response_model=Person)
def read_person(person_id: int, db: Session = Depends(get_db)):
person = db.query(models.Person).filter(models.Person.person_id == person_id).first()
if person is None:
raise HTTPException(status_code=404, detail="Person not found")
return person

View File

@ -1,54 +1,25 @@
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base
from .database import Base
class Criteria(Base): Base = declarative_base()
__tablename__ = "criteria"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(255))
class Role(Base): class Person(Base):
__tablename__ = "roles" __tablename__ = "Persons"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(255))
class Student(Base): person_id = Column("PersonID", Integer, primary_key=True, index=True, autoincrement=True)
__tablename__ = "students" last_name = Column("LastName", String(255), nullable=False)
id = Column(Integer, primary_key=True, index=True) first_name = Column("FirstName", String(255), nullable=True)
first_name = Column(String(50)) address = Column("Address", String(255), nullable=True)
last_name = Column(String(50)) city = Column("City", String(255), nullable=True)
grade = Column(String(50), default="Brak ocen")
points = Column(Integer, nullable=True)
status = Column(Integer)
class ClassInfo(Base): """
__tablename__ = "class_info" SQL Insert statements:
id = Column(Integer, primary_key=True, index=True) INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
school = Column(String(255)) VALUES
city = Column(String(255)) (1, 'Smith', 'John', '123 Maple St', 'New York'),
year = Column(String(50)) (2, 'Johnson', 'Emily', '456 Oak Ave', 'Los Angeles'),
semester = Column(String(50)) (3, 'Williams', 'Michael', '789 Pine Dr', 'Chicago'),
subject = Column(String(255)) (4, 'Brown', 'Sarah', '101 Birch Ln', 'Houston'),
level = Column(String(50)) (5, 'Jones', 'David', '202 Cedar Rd', 'Phoenix');
publisher = Column(String(255)) """
class_name = Column(String(50))
group = Column(String(50))
profile = Column(String(255))
max_points = Column(Integer)
teacher = Column(String(255))
file_path = Column(String(255))
student_count = Column(Integer)
class Event(Base):
__tablename__ = "events"
id = Column(Integer, primary_key=True, index=True)
role_id = Column(Integer, ForeignKey("roles.id"), nullable=False)
student_id = Column(Integer, ForeignKey("students.id"), nullable=False)
criteria_id = Column(Integer, ForeignKey("criteria.id"), nullable=False)
description = Column(String(255))
event_date = Column(DateTime)
role = relationship("Role")
student = relationship("Student")
criteria = relationship("Criteria")

View File

@ -1,83 +1,19 @@
from pydantic import BaseModel from pydantic import BaseModel, Field
from datetime import datetime
class CriteriaBase(BaseModel): # Schemat do tworzenia nowej osoby (POST)
name: str class PersonCreate(BaseModel):
last_name: str = Field(..., description="Nazwisko osoby", example="Kowalski")
first_name: str = Field(..., description="Imię osoby", example="Jan")
address: str | None = None
city: str | None = None
class CriteriaCreate(CriteriaBase): # Schemat reprezentujący osobę (GET)
pass class Person(BaseModel):
person_id: int
class Criteria(CriteriaBase):
id: int
class Config:
orm_mode = True
class RoleBase(BaseModel):
name: str
class RoleCreate(RoleBase):
pass
class Role(RoleBase):
id: int
class Config:
orm_mode = True
class StudentBase(BaseModel):
first_name: str
last_name: str last_name: str
grade: str first_name: str
points: int address: str | None = None
status: int city: str | None = None
class StudentCreate(StudentBase):
pass
class Student(StudentBase):
id: int
class Config:
orm_mode = True
class ClassInfoBase(BaseModel):
school: str
city: str
year: str
semester: str
subject: str
level: str
publisher: str
class_name: str
group: str
profile: str
max_points: int
teacher: str
file_path: str
student_count: int
class ClassInfoCreate(ClassInfoBase):
pass
class ClassInfo(ClassInfoBase):
id: int
class Config:
orm_mode = True
class EventBase(BaseModel):
role_id: int
student_id: int
criteria_id: int
description: str
event_date: datetime
class EventCreate(EventBase):
pass
class Event(EventBase):
id: int
class Config: class Config:
orm_mode = True orm_mode = True