Skip to content

avaje/avaje-sigma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Avaje Sigma

Build Maven Central javadoc License Discord

Provides javalin/jex style request handling for AWS lambda http requests.

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);
  }
}

Use with 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>2.8</version>
</dependency>

<!-- Annotation processor -->
<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-http-sigma-generator</artifactId>
  <version>2.8</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);
  }
}

Releases

No releases published

Contributors 4

  •  
  •  
  •  
  •  

Languages