Posts for: #Mitoyus

Jika Kamu Bingung Kok Ada File init.py tapi gak ada dicontoh, ini solusinya

Berikut adalah contoh isi dari file __init__.py yang perlu dimasukkan dalam setiap folder untuk memastikan folder tersebut diperlakukan sebagai paket Python (module), dan memungkinkan Anda untuk mengimpor modul-modul dalam folder tersebut.

1. my_fastapi_project/app/__init__.py

File ini akan menginisialisasi folder app sebagai paket. Biasanya file ini kosong, tetapi kadang bisa berisi pengaturan atau import global.

# app/__init__.py

# Tidak ada yang perlu dimasukkan di sini jika tidak ada pengaturan tambahan
# Tetapi Anda bisa menambah kode global yang ingin diekspor jika diperlukan

2. my_fastapi_project/app/api/__init__.py

Folder ini berisi modul API. Anda bisa menambah pengaturan impor umum di sini, terutama jika Anda ingin mengelompokkan versi API.

Read more

Recommendation Structure Folder For a Python FastAPI Projects

When structuring a folder for a Python FastAPI project, it’s essential to maintain modularity and scalability, especially as your project grows. A good folder structure helps to separate concerns, making it easier to manage and maintain the codebase.

Here’s a recommended folder structure for a typical FastAPI project:

my_fastapi_project/
│
├── app/                        # Main application code
│   ├── __init__.py
│   ├── main.py                 # Entry point for the FastAPI application
│   ├── api/                    # API routes and business logic
│   │   ├── __init__.py
│   │   ├── v1/                 # Versioned API (optional, but good for future scalability)
│   │   │   ├── __init__.py
│   │   │   ├── endpoints/      # Routes for version 1 API
│   │   │   │   ├── __init__.py
│   │   │   │   ├── user.py     # Example: user-related routes
│   │   │   │   ├── item.py     # Example: item-related routes
│   │   ├── v2/                 # (Optional) Future version of the API
│   │   ├── dependencies.py     # Shared dependencies (e.g., DB session)
│   ├── core/                   # Core components like settings, config, security
│   │   ├── __init__.py
│   │   ├── config.py           # Configuration settings (e.g., environment variables)
│   │   ├── security.py         # Security-related utilities (e.g., JWT)
│   │   ├── settings.py         # Settings management (e.g., loading env variables)
│   ├── models/                 # ORM models or Pydantic schemas
│   │   ├── __init__.py
│   │   ├── user.py             # Example: user model
│   │   ├── item.py             # Example: item model
│   ├── schemas/                # Pydantic schemas for validation
│   │   ├── __init__.py
│   │   ├── user.py             # Example: user schema
│   │   ├── item.py             # Example: item schema
│   ├── services/               # Business logic and service layer
│   │   ├── __init__.py
│   │   ├── user_service.py     # Example: user service
│   │   ├── item_service.py     # Example: item service
│   ├── db/                     # Database-related code (e.g., SQLAlchemy models, migrations)
│   │   ├── __init__.py
│   │   ├── session.py          # Database session management
│   │   ├── init_db.py          # Database initialization code (e.g., creating tables)
│   ├── tests/                  # Unit and integration tests
│   │   ├── __init__.py
│   │   ├── test_user.py        # Example test file for user-related functionality
│   │   ├── test_item.py        # Example test file for item-related functionality
│   ├── utils/                  # Utility functions that are used across the app
│   │   ├── __init__.py
│   │   ├── date_utils.py       # Example utility: date-related functions
│
├── .env                         # Environment variables for sensitive data (e.g., DB URI, API keys)
├── .gitignore                   # Git ignore file to exclude unnecessary files
├── requirements.txt             # Project dependencies (including FastAPI and Uvicorn)
├── alembic.ini                  # Alembic configuration file (if using migrations with SQLAlchemy)
├── Dockerfile                   # Dockerfile for containerizing the app (optional)
├── docker-compose.yml           # Docker Compose file for multi-container setup (optional)
└── README.md                    # Project documentation

Explanation of the Structure:

  1. app/: This is where the majority of your code resides. It contains all your app-specific logic and modules.

Read more

How to completely uninstall NGINX

To completely uninstall NGINX from your system, follow the appropriate instructions based on your operating system:

For Ubuntu/Debian-based systems:

  1. Stop the NGINX service:

    sudo systemctl stop nginx
    
  2. Disable the NGINX service (so it doesn’t start on boot):

    sudo systemctl disable nginx
    
  3. Remove NGINX and its dependencies: Run the following command to remove the NGINX package:

    sudo apt-get purge nginx nginx-common nginx-full nginx-core
    
  4. Remove any unused dependencies: After removing NGINX, you can clean up any unused packages:

Read more

How to deploy rust app to VPS

Deploying a Rust application to a VPS (Virtual Private Server) involves several key steps. Here’s a comprehensive guide on how to do it:

Prerequisites

  1. Rust Toolchain installed on your local machine (using rustup).
  2. VPS Access: SSH access to the VPS (for example, using SSH keys).
  3. VPS OS: Linux-based OS (Ubuntu, Debian, CentOS, etc.) is assumed.
  4. Cargo (Rust’s package manager and build tool) should be installed on the VPS.
  5. A web server (like Nginx or Apache) for serving HTTP applications (optional, only necessary for web apps).

Step-by-Step Guide

1. Build Your Rust Application

First, build your Rust app for release on your local machine:

Read more

Pengalaman VPS menggunakan DEBIAN 12

DEBIAN adalah distro yang sungguh popular, tetapi kenapa banyak hal yang cukup berbeda dengan UBUNTU yang merupakan salah satunya. Berikut adalah beberapa hal yang cukup berbeda di DEBIAN 12

1. Install rust yang tidak lancar

Jika anda ingin menginstall rust, tidak cukup dengan perintah

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Anda juga harus menambahkan beberapa installasi yang harus dilakukan seperti

sudo apt install build-essential pkg-config libudev-dev libpq-dev

Setelah install itu semua, saya masih tidak bisa melakukan build release aplikasi rust saya.

Read more

Create a REST API with Rust and Connect to Telegram Bot

To create a REST API with Rust that allows you to send photos and videos to a Telegram bot, we can use the Actix Web framework for the API and the Teloxide library for interacting with the Telegram bot.

Steps:

  1. Set up Actix Web to create the REST API.
  2. Use Teloxide to interact with the Telegram API for sending media.
  3. Use multipart form data to handle file uploads in the REST API.

Dependencies:

You will need the following dependencies in Cargo.toml:

Read more