Skip to content

saviorand/lightbug_api

Repository files navigation


Logo

Lightbug API

🐝 Author APIs in Pure Mojo 🔥

Written in Mojo MIT License Contributors Welcome Join our Discord

Overview

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, ...)

Check Out These Mojo Libraries:

(back to top)

Getting Started

Learn how to get up and running with Mojo on the Modular website. Once you have a Mojo project set up locally,

  1. Add the mojo-community channel to your mojoproject.toml, e.g:
    [project]
    channels = ["conda-forge", "https://conda.modular.com/max", "https://repo.prefix.dev/mojo-community"]
  2. Add lightbug_api and lightbug_http in dependencies:
    [dependencies]
    lightbug_api = ">=0.1.0.dev2024092905"
    lightbug_http = ">=0.1.4"
  3. Run magic install at the root of your project, where mojoproject.toml is located
  4. Lightbug API should now be installed. You can import all the default imports at once, e.g:
    from lightbug_api import *
    or import individual structs and functions, e.g.
    from lightbug_api.routing import Router
  5. Create one or more handlers for your routes that satiisfy the HTTPService trait:
    trait HTTPService:
     fn func(self, req: HTTPRequest) raises -> HTTPResponse:
         ...
    For example, to make a Printer service 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)
  6. 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)
         app.post("/", printer)
    
         app.start_server()
  7. Excellent 😈. Your app is now listening on the selected port. You've got yourself a pure-Mojo API! 🔥

Contributors

Want your name to show up here? See CONTRIBUTING.md!

Made with contrib.rocks.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published