Skip to content

Latest commit

 

History

163 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Avaje Sigma

Build Maven Central javadoc License Discord

Provides javalin/jex style request handling for AWS Lambda HTTP requests.

Depending on your infrastructure, you can use ALBHttpEvent, APIGatewayV2HttpEvent, or APIGatewayProxyEvent.

Quick Start

Add dependency:

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-sigma</artifactId>
  <version>${sigma.version}</version>
</dependency>

Create Server:

public class LambdaRequestHandler
    implements RequestHandler<APIGatewayV2HttpEvent, AWSHttpResponse> {

  HttpFunction handler =
      Sigma.create()
          .routing(
              r ->
                  r.get("/lambda", ctx -> ctx.text("Hello World"))
                   .get("/route2/{param}", ctx -> ctx.text(ctx.pathParam("param"))))
          .createHttpFunction();

  @Override
  public AWSHttpResponse handleRequest(APIGatewayV2HttpEvent event, Context context) {

    return handler.apply(event, context);
  }
}

Quick Start with Avaje Http

Avaje-HTTP

Add dependencies

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-sigma</artifactId>
  <version>${sigma.version}</version>
</dependency>

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-http-api</artifactId>
  <version>${avaje.http.version}</version>
</dependency>

<!-- Annotation processor -->
<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-http-sigma-generator</artifactId>
  <version>${avaje.http.version}</version>
  <scope>provided</scope>
  <optional>true</optional>
</dependency>

JDK 23+

In JDK 23+, annotation processors are disabled by default, you will need to add a flag to re-enable.

<properties>
  <maven.compiler.proc>full</maven.compiler.proc>
</properties>

Define a Controller

package org.example.hello;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Get;
import java.util.List;

@Controller("/widgets")
public class WidgetController {
  private final HelloComponent hello;
  public WidgetController(HelloComponent hello) {
    this.hello = hello;
  }

  @Get("/{id}")
  Widget getById(int id) {
    return new Widget(id, "you got it"+ hello.hello());
  }

  @Get()
  List<Widget> getAll() {
    return List.of(new Widget(1, "Rob"), new Widget(2, "Fi"));
  }

  record Widget(int id, String name){};
}

This will generate routing code we can register using any JSR-330 compliant DI:

@Generated("avaje-sigma-generator")
@Singleton
public class WidgetController$Route implements HttpService {

  private final WidgetController controller;

  public WidgetController$Route(WidgetController controller) {
    this.controller = controller;
  }

  @Override
  public void setup(Router router) {

    router.get("/widgets/{id}", ctx -> {
      ctx.status(200);
      var id = asInt(ctx.pathParam("id"));
      var result = controller.getById(id);
      ctx.json(result);
    });

    router.get("/widgets", ctx -> {
      ctx.status(200);
      var result = controller.getAll();
      ctx.json(result);
    });

  }

}

DI Usage

public class LambdaRequestHandler
    implements RequestHandler<APIGatewayV2HttpEvent, AWSHttpResponse> {

  HttpFunction handler;

  public LambdaRequestHandler() {

    List<HttpService> services = // Retrieve HttpServices via DI;
    handler = Sigma.create().routing(services).createHttpFunction();
  }

  @Override
  public AWSHttpResponse handleRequest(APIGatewayV2HttpEvent event, Context context) {

    return handler.apply(event, context);
  }
}

About

http routing for AWS lambda

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages