visit
This is where flies in.
is a simple HTTP framework written in pure Mojo, with no external dependencies by default. It's meant to serve as a foundation for more complex projects and allows you to develop Web services like APIs, set up basic routing, or even serve HTML pages with Mojo, while taking advantage of the features of this language, like static typing and great performance.
git clone //github.com/saviorand/lightbug_http.git
cd lightbug_http
mojo lightbug.🔥
🔥🐝 Lightbug is listening on 0.0.0.0:8080
Ready to accept connections...
Now, you can start making requests to your server or try opening localhost:8080
or 0.0.0.0:8080
in the browser — you should see the intro screen. Welcome to the Web, Mojo-style! Now, let's get to some real coding.
Note that since there's no package manager yet, you should simply include lightbug_http
as a subfolder inside your own project. This will work as a and will allow you to import tools like web primitives, servers, and more from Lightbug.
from lightbug_http.sys.server import SysServer
This will import a server implementation in pure Mojo. If you want to use the Python implementation, import the PythonServer
instead. It will work in the same manner.
To make an HTTP service, simply make a struct that satisfies the HTTPService
trait, meaning it has a func
method with the following signature:
trait HTTPService:
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
...
This uses the built-in primitives to take in an HTTPRequest
, execute any custom logic you write in Mojo or Python, and return an HTTPResponse
object with some data back to the API consumer.
Let's make a service that prints all requests sent to 0.0.0.0:8080
to the console. To do this, create a file called my_awesome_service.🔥
, and paste the following:
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)
Run mojo my_awesome_service.🔥
, and send the request to 0.0.0.0:8080
from your favorite API clients, like or . You should see some details about the request printed to the console.
In the example, we initialize a variable called handler
with let
(meaning it cannot be re-assigned) and pass it to listen_and_serve
as a second parameter for clarity.
Adding a @value
decorator is optional: if you're an advanced Mojician, you can add an __init__
instead. It will work the same; @value
just generates this and other useful methods automatically.
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)
@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)
Add this to your my_awesome_service.🔥
, and pass it as a handler to the server:
fn main() raises:
var server = SysServer()
let handler = ExampleRouter()
server.listen_and_serve("0.0.0.0:8080", handler)
You can now open your browser and go to localhost:8080/first
, localhost:8080/second
to see the changes.
This functionality should give you a basis to develop your own apps, libraries, and services that make use of HTTP, while taking advantage of the flexibility and customization options that a lightweight framework/toolkit that is the lightbug_http
can provide.
We plan on making routing, as well as other tasks like authoring and generating APIs from an OpenAPI specification, designing your data model, and building web applications even more enjoyable in the future by building lightbug_api
and lightbug_web
packages. Check out our for details.
That's it for an introduction to ! Hope it was useful.