
?帶有機翼的Mojo HTTP框架
LightBug是Mojo的簡單甜美的HTTP框架,它基於系統編程的最佳實踐,例如Golang Fasthttp和Rust May_minihttp。
這還沒有準備好。我們的目標是跟上Mojo中的新發展,但是可能需要一些時間才能安全地在現實世界應用程序中使用。
Lightbug目前具有以下功能:
def鍵入(返回到頂部)
lightbug_http的唯一硬依賴性是Mojo。了解如何在模塊化網站上與Mojo一起起床。一旦您在本地設置了Mojo項目,
將mojo-community頻道添加到您的mojoproject.toml ,例如:
[ project ]
channels = [ " conda-forge " , " https://conda.modular.com/max " , " https://repo.prefix.dev/mojo-community " ]添加lightbug_http作為一個依賴項:
[ dependencies ]
lightbug_http = " >=0.1.5 "在項目的根部運行magic install ,其中mojoproject.toml所在
現在,應將燈泡安裝為依賴項。您可以一次導入所有默認導入,例如:
from lightbug_http import *或導入單個結構和功能,例如
from lightbug_http.service import HTTPService
from lightbug_http.http import HTTPRequest, HTTPResponse, OK , NotFound您可以玩一些默認處理程序:
from lightbug_http.service import Printer # prints request details to console
from lightbug_http.service import Welcome # serves an HTML file with an image (currently requires manually adding files to static folder, details below)
from lightbug_http.service import ExampleRouter # serves /, /first, /second, and /echo routes將您的處理程序添加到lightbug.通過傳遞滿足以下特徵的結構:
trait HTTPService :
fn func ( inout self , req : HTTPRequest) raises -> HTTPResponse:
...例如,製作Printer服務以打印有關主機請求的一些詳細信息:
from lightbug_http import *
@value
struct Printer ( HTTPService ):
fn func ( inout self , req : HTTPRequest) raises -> HTTPResponse:
var uri = req.uri
print ( " Request URI: " , to_string(uri.request_uri))
var header = req.headers
print ( " Request protocol: " , req.protocol)
print ( " Request method: " , req.method)
print (
" Request Content-Type: " , to_string(header[HeaderKey. CONTENT_TYPE ])
)
var body = req.body_raw
print ( " Request Body: " , to_string(body))
return OK(body)啟動服務器在端口上使用您的服務聆聽。
from lightbug_http import Welcome, Server
fn main () raises :
var server = Server()
var handler = Welcome()
server.listen_and_serve( " 0.0.0.0:8080 " , handler)隨意更改listen_and_serve()中的設置以在特定主機和端口上服務。
現在發送請求0.0.0.0:8080 。您應該看到有關打印到控制台的請求的一些詳細信息。
恭喜?您正在使用燈泡!
該庫的路由不在範圍內,但是您可以輕鬆地設置路線:
from lightbug_http import *
@value
struct ExampleRouter ( HTTPService ):
fn func ( inout self , req : HTTPRequest) raises -> HTTPResponse:
var body = req.body_raw
var uri = req.uri
if uri.path == " / " :
print ( " I'm on the index path! " )
if uri.path == " /first " :
print ( " I'm on /first! " )
elif uri.path == " /second " :
print ( " I'm on /second! " )
elif uri.path == " /echo " :
print (to_string(body))
return OK(body)我們計劃在名為lightbug_api的將來的庫中添加更高級的路由功能,請參閱路線圖。
(返回到頂部)
默認的歡迎屏幕顯示瞭如何使用LightBug使用圖像或HTML(例如HTML)的示例。 Mojo已open , read和read_bytes方法,您可以用來讀取文件並在路線上使用它們。假設您將HTML文件和圖像從Lightbug Repo複製到倉庫根源的static目錄:
from lightbug_http import *
@value
struct Welcome ( HTTPService ):
fn func ( inout self , req : HTTPRequest) raises -> HTTPResponse:
var uri = req.uri
if uri.path == " / " :
var html : Bytes
with open ( " static/lightbug_welcome.html " , " r " ) as f:
html = f.read_bytes()
return OK(html, " text/html; charset=utf-8 " )
if uri.path == " /logo.png " :
var image : Bytes
with open ( " static/logo.png " , " r " ) as f:
image = f.read_bytes()
return OK(image, " image/png " )
return NotFound(uri.path)使用以下代碼創建一個文件,例如client.mojo 。運行magic run mojo client.mojo以執行給定URL的請求。
from lightbug_http import *
from lightbug_http.client import Client
fn test_request ( inout client : Client) raises -> None :
var uri = URI .parse_raises( " http://httpbin.org/status/404 " )
var headers = Header( " Host " , " httpbin.org " )
var request = HTTPRequest(uri, headers)
var response = client.do(request ^ )
# print status code
print ( " Response: " , response.status_code)
# print parsed headers (only some are parsed for now)
print ( " Content-Type: " , response.headers[ " Content-Type " ])
print ( " Content-Length " , response.headers[ " Content-Length " ])
print ( " Server: " , to_string(response.headers[ " Server " ]))
print (
" Is connection set to connection-close? " , response.connection_close()
)
# print body
print (to_string(response.body_raw))
fn main () -> None :
try :
var client = Client()
test_request(client)
except e:
print (e)默認情況下,基於純Mojo的客戶端可用。該客戶端還用於內部測試服務器。
默認情況下,LightBug使用純Mojo實現進行網絡。要使用Python的socket庫,只需將PythonServer而不是以下行導入Server :
from lightbug_http.python.server import PythonServer然後,您可以以與默認服務器相同的方式使用所有常規服務器命令。注意:截至2024年9月, PythonServer和PythonClient在啟動時會出現彙編錯誤。有一個公開問題可以解決此問題 - 歡迎貢獻!

我們正在努力支持以下(歡迎貢獻者!):
該計劃是要獲得類似於類似於星際旅行的Python框架的功能集,但性能更好。
我們的願景是開發三個庫,以lightbug_http (此存儲庫)為起點:
lightbug_http -HTTP基礎架構和基本API開發lightbug_api (在2024年後期來!)快速製作出色API的工具,並支持OpenAPI規格和域驅動的設計lightbug_web (發布日期tbd)Mojo的全堆棧網絡框架,類似於NextJS或Sveltekit這個想法是要達到一個可以用Mojo編寫的簡單現代Web應用程序的整個代碼庫。
不過,我們沒有做出任何承諾 - 這只是一個願景,無論我們是否到達那裡取決於許多因素,包括社區的支持。
請參閱“開放問題”並提交您的提交,以幫助推動Lightbug的開發。
(返回到頂部)
貢獻是使開源社區成為學習,啟發和創造的驚人場所的原因。您所做的任何貢獻都非常感謝。有關如何貢獻的更多詳細信息,請參見貢獻。
如果您有一個可以使情況變得更好的建議,請分配存儲庫並創建拉動請求。您也可以簡單地使用標籤“增強”打開問題。別忘了給項目一個明星!
git checkout -b feature/AmazingFeature )git commit -m 'Add some AmazingFeature' )git push origin feature/AmazingFeature )(返回到頂部)
根據MIT許可分發。有關更多信息,請參見LICENSE.txt 。
(返回到頂部)
瓦倫丁·伊羅欽(Valentin Erokhin)
項目鏈接:https://github.com/saviorand/mojo-web
(返回到頂部)
我們在以下項目中繪製了很多:
(返回到頂部)
希望您的名字出現在這裡嗎?請參閱貢獻。 md!
用貢獻製成。