Mojo ã¯ãPython ã®èªã¿ããããš C++ ã®é床ãçµã¿åãããèšèªã§ãã
ããã¯ãããŒããŠã§ã¢ã«è¿ãäœã¬ãã«ã®ã³ãŒããããããã¯ãšã³ã API èšèšãä»ããŠãããã³ããšã³ãã Web ã®äžçã«è³ããŸã§ãããŸããŸãªçšéã«åœ¹ç«ã¡ãŸãã Mojo ã¯ãææ°ã®ã¹ã¿ãã¯å šäœã«ããã£ãŠæ¡åŒµã§ããã»ã©åŒ·åã§ãããã®èšèªã¯ AI ãšæ©æ¢°åŠç¿ã念é ã«çœ®ããŠèšèšãããŠãããããAI éçºè ãããŒã¿ ãµã€ãšã³ãã£ã¹ãã«ãšã£ãŠç¹ã«åœ¹ç«ã¡ãŸãã
ã¢ãžã§ã¯ãŸã è¥ãã§ãããšã³ã·ã¹ãã ã«ã¯ããããã¯ãŒãã³ã°ãåºæ¬ç㪠HTTP æäœãªã©ãæ¥åžžçãªãœãããŠã§ã¢éçºã®ããã®ããŒã«ãæ¬ ããŠããŸãã
ããã§Lightbug ð¥ðãç»å ŽããŸãã
Lightbug ð¥ð ã¯çŽç²ãª Mojo ã§æžãããã·ã³ãã«ãª HTTP ãã¬ãŒã ã¯ãŒã¯ã§ãããããã©ã«ãã§ã¯å€éšäŸåé¢ä¿ããããŸãããããã¯ãããè€éãªãããžã§ã¯ãã®åºç€ãšããŠæ©èœããããšãç®çãšããŠãããéçåä»ããåªããããã©ãŒãã³ã¹ãªã©ã®ãã®èšèªã®æ©èœã掻çšããªãããAPI ãªã©ã® Web ãµãŒãã¹ã®éçºãåºæ¬çãªã«ãŒãã£ã³ã°ã®èšå®ãããã«ã¯ Mojo 㧠HTML ããŒãžã®æäŸãè¡ãããšãã§ããŸãã ã
å§ããã«ã¯ãåã«MojoãšGitãã€ã³ã¹ããŒã«ããLightbug Github ãªããžããªã®ã¯ããŒã³ãäœæããŸãã
git clone https://github.com/saviorand/lightbug_http.git
ã¯ããŒã³ãäœæãããããã³ãã³ã ã©ã€ã³ã§ãã£ã¬ã¯ããªã«åãæ¿ããŸãã
cd lightbug_http
次ã«ããµãŒããŒãå®è¡ããŸã (ã¯ãããã㯠Mojo ãã¡ã€ã«æ¡åŒµåã§ã! ð¥):
mojo lightbug.ð¥
次ã®è¡ãã³ã³ãœãŒã«ã«åºåãããã¯ãã§ãã
ð¥ð Lightbug is listening on 0.0.0.0:8080 Ready to accept connections...
ããã§ããµãŒããŒãžã®ãªã¯ãšã¹ããéå§ãããããã©ãŠã¶ã§localhost:8080
ãŸãã¯0.0.0.0:8080
ãéããŠã¿ãŠãã ãããã€ã³ããç»é¢ã衚瀺ãããã¯ãã§ãã Mojo ã¹ã¿ã€ã«ã® Web ãžãããã!ããã§ã¯ãå®éã®ã³ãŒãã£ã³ã°ã«ç§»ããŸãããã
Lightbug ã¯ãŸã è¥ãã§ãããçŸä»£ã® Web åãã«éçºã§ãããšäººã ãæåŸ ããŠããã³ã¢æ©èœã¯ãã§ã«ååšããŠããŸãã
ãŸã ããã±ãŒãž ãããŒãžã£ãŒããªããããç¬èªã®ãããžã§ã¯ãå
ã®ãµããã©ã«ããŒãšããŠlightbug_http
ãå«ããå¿
èŠãããããšã«æ³šæããŠãã ãããããã¯Mojo ããã±ãŒãžãšããŠæ©èœããWeb ããªããã£ãããµãŒããŒãªã©ã®ããŒã«ã Lightbug ããã€ã³ããŒãã§ããããã«ãªããŸãã
ããšãã°ããµãŒããŒãã€ã³ããŒãããã«ã¯ãããããã¡ã€ã«ã®å é ã«è¿œå ããŸãã
from lightbug_http.sys.server import SysServer
ããã«ãããçŽç²ãª Mojo ã«ãµãŒããŒå®è£
ãã€ã³ããŒããããŸãã Python å®è£
ã䜿çšããå Žåã¯ã代ããã«PythonServer
ãã€ã³ããŒãããŸããåãããã«åäœããŸãã
HTTP ãµãŒãã¹ãäœæããã«ã¯ã HTTPService
traitãæºããæ§é äœãäœæããã ãã§ããã€ãŸãã次ã®ã·ã°ããã£ãæã€func
ã¡ãœãããå«ãŸããŠããŸãã
trait HTTPService: fn func(self, req: HTTPRequest) raises -> HTTPResponse: ...
ããã¯ãçµã¿èŸŒã¿ããªããã£ãã䜿çšããŠHTTPRequest
ãåãåããMojo ãŸã㯠Python ã§äœæããã«ã¹ã¿ã ããžãã¯ãå®è¡ããããŒã¿ãå«ãHTTPResponse
ãªããžã§ã¯ãã API ã³ã³ã·ã¥ãŒããŒã«è¿ããŸãã
0.0.0.0:8080
ã«éä¿¡ããããã¹ãŠã®ãªã¯ãšã¹ããã³ã³ãœãŒã«ã«åºåãããµãŒãã¹ãäœæããŠã¿ãŸãããããããè¡ãã«ã¯ã my_awesome_service.ð¥
ãšãããã¡ã€ã«ãäœæãã次ã®å
容ã貌ãä»ããŸãã
from lightbug_http import * @value struct Printer(HTTPService): fn func(self, req: HTTPRequest) raises -> HTTPResponse: let body = req.body_raw print(String(body)) return OK(body) fn main() raises: var server = SysServer() let handler = Printer() server.listen_and_serve("0.0.0.0:8080", handler)
mojo my_awesome_service.ð¥
ãå®è¡ãã InsomniaãBrunoãªã©ã®ãæ°ã«å
¥ãã® API ã¯ã©ã€ã¢ã³ããã0.0.0.0:8080
ã«ãªã¯ãšã¹ããéä¿¡ããŸãããªã¯ãšã¹ãã«é¢ãã詳现ãã³ã³ãœãŒã«ã«åºåãããã¯ãã§ãã
ããã§ãšãããããŸãïŒããã§ããªãã¯æ£åŒã« Mojo Web éçºè ã§ã ð¥ã
ãã®äŸã§ã¯ãããããããããããã«ã handler
ãšããå€æ°ãlet
ã§åæåã (åå²ãåœãŠã§ããªãããšãæå³ããŸã)ãããã 2 çªç®ã®ãã©ã¡ãŒã¿ãšããŠlisten_and_serve
ã«æž¡ããŸãã
@value
ãã³ã¬ãŒã¿ãŒã®è¿œå ã¯ãªãã·ã§ã³ã§ããäžçŽ Mojician ã§ããã°ã代ããã«__init__
ã³ã³ã¹ãã©ã¯ã¿ãŒ ã¡ãœãããè¿œå ã§ããŸããåãããã«æ©èœããŸãã @value
ããã®ã¡ãœãããšä»ã®äŸ¿å©ãªã¡ãœãããèªåçã«çæããã ãã§ãã
struct Printer(HTTPService): fn __init__(inout self): print("Printer initialized!") fn func(self, req: HTTPRequest) raises -> HTTPResponse: let body = req.body_raw print(String(body)) return OK(body)
ãšèšãããããç¥ããŸããããããã¯äžã€ã®ã«ãŒãã«éããŸããïŒææ°ã® API ã«ã¯ãã以äžã®ãã®ãå¿ èŠã§ãã
Lightbug ã¯ããã€ãã®åºæ¬çãªã«ãŒãã£ã³ã°ãè¡ãããšãã§ããŸãã
@value struct ExampleRouter(HTTPService): fn func(self, req: HTTPRequest) raises -> HTTPResponse: let body = req.body_raw let 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!") return OK(body)
ãããmy_awesome_service.ð¥
ã«è¿œå ãããã³ãã©ãŒãšããŠãµãŒããŒã«æž¡ããŸãã
fn main() raises: var server = SysServer() let handler = ExampleRouter() server.listen_and_serve("0.0.0.0:8080", handler)
ããã§ããã©ãŠã¶ãéããŠlocalhost:8080/first
ã localhost:8080/second
ã«ç§»åããŠå€æŽã確èªã§ããŸãã
ãã®æ©èœã¯ã lightbug_http
ã§ãã軜éãã¬ãŒã ã¯ãŒã¯/ããŒã«ããããæäŸã§ããæè»æ§ãšã«ã¹ã¿ãã€ãº ãªãã·ã§ã³ã掻çšããªãããHTTP ãå©çšããç¬èªã®ã¢ããªãã©ã€ãã©ãªãããã³ãµãŒãã¹ãéçºããããã®åºç€ãæäŸããŸãã
ç§ãã¡ã¯ã lightbug_api
ããã³lightbug_web
ããã±ãŒãžãæ§ç¯ããããšã§ãã«ãŒãã£ã³ã°ã ãã§ãªããOpenAPI ä»æ§ããã® API ã®äœæãšçæãããŒã¿ ã¢ãã«ã®èšèšãWeb ã¢ããªã±ãŒã·ã§ã³ã®æ§ç¯ãªã©ã®ä»ã®ã¿ã¹ã¯ãå°æ¥çã«ããã«æ¥œããããäºå®ã§ãã詳现ã«ã€ããŠã¯ãããŒãããããã芧ãã ããã
Lightbugã®çŽ¹ä»ã¯ä»¥äžã§ãð¥ð !ã圹ã«ç«ãŠã°å¹žãã§ãã
ããã¯ãªãŒãã³ãœãŒã¹ã®éå¶å©ã³ãã¥ãã㣠ãããžã§ã¯ãã§ãã
ç§ãã¡ã®Github ãªããžããªã«ã¹ã¿ãŒãä»ããŠãã ãã â Discordã«åå ããŠãããªãã®ã³ãŒãã«è²¢ç®ããæ¹æ³ããã§ãã¯ããŠãã ãããããããã°ãä»ã® Mojicians ã«ãšã£ãŠããã«è¯ããã®ã«ããããšãã§ããŸãã
次åãŸã§ïŒ