θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content

Lightbug-HQ/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, "hello")
         app.post("/printer", printer, "printer")
    
         app.start_server()
  7. Excellent 😈. Your app is now listening on the selected port. You've got yourself a pure-Mojo API! πŸ”₯

API Docs

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()

Contributors

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

Made with contrib.rocks.

About

Author APIs in pure Mojo! πŸ”₯🐝

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published