Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 删除closing模块 #209

Merged
merged 1 commit into from
Dec 29, 2023
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
72 changes: 15 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,27 @@ pip3 install pywss
### 2、搭建 web 应用
首先创建 `main.py` 文件,并写入以下代码:
```python
import time
import pywss

def log_handler(ctx: pywss.Context):
start_time = time.time()
ctx.next()
print(
f"Route: {ctx.route}, "
f"Method: {ctx.method}, "
f"Status: {ctx.response_status_code}, "
f"Time: {time.time() - start_time:.3f}s"
)

def handler(ctx: pywss.Context):
ctx.write("hello~")

def main(port = 8080):
def main():
app = pywss.App()
app.get("/hi", lambda ctx: ctx.write("hi~")) # curl localhost:8080/hi
app.post("/hello", handler) # curl -X POST localhost:8080/hello
app.run(port=port)
app.get("/hello", handler) # curl localhost:8080/hello
app.any("*", log_handler, handler) # curl -X POST localhost:8080/hello
app.run()

if __name__ == '__main__':
main()
Expand All @@ -66,56 +77,3 @@ python3 main.py
至此,一个简单的 web 应用服务就完成了。

更多功能见[在线文档](https://czasg.github.io/pywss/)。

<br/>

## 特性速览

### 轻巧的中间件机制
```python
import time
import pywss

# 请求日志中间件
def logHandler(ctx: pywss.Context):
startTime = time.time()
ctx.next()
cost = time.time() - startTime
print(f"{ctx.method} - {ctx.route} - cost: {cost: .2f}")

app = pywss.App()
app.use(logHandler) # 注册全局日志中间件
app.run()
```

### 原生的依赖注入体验
```python
import pywss

class Repo:
def get(self):
return "repo"

class Service:

def __init__(self, repo: Repo): # Service 依赖 Repo
self.repo = repo

def get(self):
return "power by " + self.repo.get()

class UserView:

def __init__(self, service: Service): # UserView 依赖 Service
self.srv = service

def http_get(self, ctx):
ctx.write(self.srv.get())

app = pywss.App()
app.view("/user", UserView) # 注册视图路由->自动注入依赖
app.run()
```

### 强大的文件路由机制
见 [文件路由](https://czasg.github.io/pywss/advance/file-route)
8 changes: 5 additions & 3 deletions pywss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import gzip
import queue
import signal
import loggus
import socket
import inspect
Expand All @@ -21,7 +22,6 @@
from pywss.handler import *
from pywss.websocket import WebSocketUpgrade
from pywss.testing import HttpTestRequest, HttpTestResponse
from pywss.closing import Closing
from pywss.routing import Route
from pywss.openapi import openapi_ui_template
from pywss.utils import split_method_route, merge_dict
Expand Down Expand Up @@ -760,12 +760,14 @@ def run(
thread_pool_idle_time: int = int(os.environ.get("PYWSS_THREAD_POOL_IDLE_TIME", 300)),
watch: bool = os.environ.get("PYWSS_WATCHDOG_ENABLE", "false").lower() == "true",
) -> None:
# global closing manager
Closing.add_close(self.close)
# build app with [route:handler]
self.build()
# watchdog
watch and threading.Thread(target=self.watchdog, daemon=True).start()
# rigister signal closing
for sig in (signal.SIGTERM, signal.SIGINT, signal.SIGILL):
signal.signal(sig, lambda *args: self.close())
# socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind((host, port))
sock.listen(select_size)
Expand Down
20 changes: 0 additions & 20 deletions pywss/closing.py

This file was deleted.