Lightbug API is a framework that allows to quickly write expressive APIs.
This is not production ready yet. We're aiming to keep up with new developments in Mojo, but it might take some time to get to a point when this is safe to use in real-world applications.
Lightbug API currently has the following features:
- Assign handlers on given routes with different methods (GET, POST, ...)
- Logging - @toasty/stump
- CLI and Terminal - @toasty/prism, @toasty/mog
- Date/Time - @mojoto/morrow and @toasty/small-time
Learn how to get up and running with Mojo on the Modular website. Once you have a Mojo project set up locally,
- Add the
mojo-communitychannel to yourmojoproject.toml, e.g:[project] channels = ["conda-forge", "https://conda.modular.com/max", "https://repo.prefix.dev/mojo-community"]
- Add
lightbug_apiandlightbug_httpin dependencies:[dependencies] lightbug_api = ">=0.1.0.dev2024092905" lightbug_http = ">=0.1.4"
- Run
magic installat the root of your project, wheremojoproject.tomlis located - Lightbug API should now be installed. You can import all the default imports at once, e.g:
or import individual structs and functions, e.g.
from lightbug_api import *
from lightbug_api.routing import Router
- Create one or more handlers for your routes that satiisfy the
HTTPServicetrait:For example, to make atrait HTTPService: fn func(self, req: HTTPRequest) raises -> HTTPResponse: ...
Printerservice that prints some details about the request to console:from lightbug_http import HTTPRequest, HTTPResponse, OK @always_inline fn printer(req: HTTPRequest) -> HTTPResponse: print("Got a request on ", req.uri.path, " with method ", req.method) return OK(req.body_raw)
- Assign your handlers to routes and launch your app with
start_server():from lightbug_api import App from lightbug_http import HTTPRequest, HTTPResponse, OK @always_inline fn printer(req: HTTPRequest) -> HTTPResponse: print("Got a request on ", req.uri.path, " with method ", req.method) return OK(req.body_raw) @always_inline fn hello(req: HTTPRequest) -> HTTPResponse: return OK("Hello π₯!") fn main() raises: var app = App() app.get("/", hello, "hello") app.post("/printer", printer, "printer") app.start_server()
- Excellent π. Your app is now listening on the selected port. You've got yourself a pure-Mojo API! π₯
Lightbug serves API docs for your app automatically at /docs by default.
To disable this, add the docs_enabled=False flag when creating a new app instance: App(docs_enabled=False).
To describe your routes, add Mojo docstring annotations like below:
@always_inline
fn printer(req: HTTPRequest) -> HTTPResponse:
"""Prints the request body and returns it.
Args:
req: Any arbitrary HTTP request with a body.
Returns:
HTTPResponse: 200 OK with the request body.
"""
print("Got a request on ", req.uri.path, " with method ", req.method)
return OK(req.body_raw)
@always_inline
fn hello(req: HTTPRequest) -> HTTPResponse:
"""Simple hello world function.
Args:
req: Any arbitrary HTTP request.
Returns:
HTTPResponse: 200 OK with a Hello World message.
Tags:
hello.
"""
return OK("Hello π₯!", "text/plain; charset=utf-8")
fn main() raises:
var app = App()
app.get("/", hello, "hello")
app.post("/printer", printer, "printer")
app.start_server()Want your name to show up here? See CONTRIBUTING.md!
Made with contrib.rocks.