39

集成了 FastAPI 和 gRPC 的框架 Bali

 2 years ago
source link: https://www.oschina.net/p/bali
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

集成了 FastAPI 和 gRPC 的框架

Bali 基于 FastAPI 和 gRPC,简化了云原生微服务开发。如果你想让你的项目同时支持 HTTP 和 gRPC ,那么 Bali 可以帮助你很轻松的完成。 

Bali 的特性:
* 项目结构简单。
* 融合了 `SQLAlchemy` 并提供了 model 生成的方法。
* 提供了工具类转换 model 成为 Pydantic 模式.
* 支持 GZip 解压缩.
* 🍻 **Resource** 层处理对外服务即支持 HTTP 又支持 gRPC

谁在使用

1. Python 3.8+
2. FastAPI 0.63+
3. grpcio>=1.32.0,<1.42
pip install bali-core

创建应用:

import greeter_server

# Initialized App 
app = Bali()
# Updated settings
app.settings(base_settings={'title': 'Bali App'})

启动 HTTP & RPC:

# lauch RPC 
python main.py --rpc

# lauch HTTP
python main.py --http

数据库对象的使用:

### connect
from bali.core import db

# connect to database when app started
# db is a sqla-wrapper instance
db.connect('DATABASE_URI')  
  


### Declarative mode with sqla-wrapper

class User(db.Model):
    __tablename__ "users"
    id = db.Column(db.Integer, primary_key=True)
    ...

db.create_all()

db.add(User(...))
db.commit()

todos = db.query(User).all()

### Declare models inherit from convenient base models

## BaseModel

# using BaseModel
class User(db.BaseModel):
    __tablename__ "users"
    id = db.Column(db.Integer, primary_key=True)



# BaseModel's source code 

class BaseModel(db.Model):
    __abstract__ = True

    created_time = Column(DateTime(timezone=True), default=datetime.utcnow)
    updated_time = Column(
        DateTime(timezone=True), default=datetime.utcnow, onupdate=datetime.utcnow
    )
    is_active = Column(Boolean(), default=True)


### Transaction

# SQLA-wrapper default model behavior is auto commit, auto commit will be disabled with `db.transaction` context. 
with db.transaction():
    item = Item.create(name='test1')

### Operators

# Operators provided `get_filters_expr` to transform filters (dict) to SQLAlchemy expressions.  

from bali.db.operators import get_filters_expr
from models import User

users = User.query().filter(*get_filters_expr(User, **filters)).all()

Resource

资源(Resouce)的设计借鉴了REST架构风格的几个关键概念。HTTP 和 RPC 使用的是同一套 Resouce 代码,常用的 action 的对应关系如下:

Action Route Method RPC Description get /{id} GET Get{Resource} Get an existing resource matching the given id list / GET List{Resource} Get all the resources create / POST Create{Resource} Create a new resource update /{id} PATCH Update{Resource} Update an existing resource matching the given id delete /{id} DELETE Delete{Resource} Delete an existing resource matching the given id

缓存的使用:

from bali.core import cache

# Usage example (API)

# Read cache 
cache.get(key)

# Set cache 
cache.set(key, value, timeout=10)
展开阅读全文

Recommend

  • 3
    • ana-balica.github.io 2 years ago
    • Cache

    Take me to Bali

    Bali is probably one of those beautiful and exotic places, that are actively advertised at your local travel agency. Paradise on earth, I think, they call it. Not without a reason, of course. It’s this beautiful, haunting place, wher...

  • 10
    • www.galaxytourism.com 2 years ago
    • Cache

    Dubai Singapore & Bali Cruise Tour Packages

    Witness the splendour and charm of different holiday destinations with luxury cruise tour packages and assimilate ecstatic views of the cities to entice your soul and senses. Enjoy delicious meals and groove to foot-tapping numbers while gliding...

  • 3

    Life is full of adventures, and there is nothing more exciting than travelling to new places to experience them. Adventure tour packages from Galaxy Tourism offer the ultimate adventure opportunities to adventurous souls like you. Indulge in the...

  • 5

    Spend the most memorable days of your life amidst one of the most stunning destinations around the world and make your honeymoon an ideal romantic escape. With the perfectly customizable honeymoon tour packages, reaching your dream destinations...

  • 3

    Spending vacation at your dream destination is easier than ever before with Galaxy Tourism offering the best luxury tour packages for you and your loved ones. Enjoy the most exotic ultra-modern infrastructure, breathtaking landscapes, alluring g...

  • 7
    • www.galaxytourism.com 2 years ago
    • Cache

    Top Spellbinding Bali Tourist Attractions

    Relax at the gorgeous beaches and revel in the paradisal destination. Gift your family some fabulous moments to cherish for years. Bali attraction tours offers everything you would have wanted from your perfect vacation. Explore the adventures o...

  • 8
    • www.galaxytourism.com 2 years ago
    • Cache

    Top DMC for Dubai, Singapore, Malaysia & Bali

    Galaxy Tourism has been emerged as the best Destination Management Company (DMC), deals in Dubai, Singapore, Malaysia & Bali. Established in 2006, the best known B2B travel agency which offers quality services by keeping in mind the requirem...

  • 3

    CEO InsiderBali and the “Wanna Be” Digital Nomad

  • 4

    Are you feeling exhausted and bored with day to day life? Do you want to quench your wanderlust? Contact Anjna Global! We are the leading one-stop shop for all your traveling needs. We offer the best hotel accommodation, sightseeing city tours,...

  • 5
    • ceoworld.biz 1 year ago
    • Cache

    How To Plan Long Term Stays In Bali

    How To Plan Long Term Stays In Bali

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK