Coder Social home page Coder Social logo

barista's Introduction

barista

An opinionated library to simplify making Java services and helping developers focus on business logic instead of server configuration and setup. By default, Barista listens on port 8443 and enables TLS, using var/security/key.pem as the private key and var/security/trust.pem as the trust store and CA certificates.

Depend on barista via Maven Central at coordinates com.markelliot.barista:barista:<version>.

Endpoints

Barista serves HTTP endpoints that:

  • use an HTTP method that's one of GET, PUT, POST, DELETE
  • serve requests on a path-like HTTP route
  • support path, querystring, header, and cookie parameters
  • support enforced user authentication

Generally, users of Barista use a code generator to make endpoint implementation, and Barista comes with a built-in code generator. The built-in system works using Java's annotation processors.

To configure the annotation processor in Gradle:

dependencies {
    implementation "com.markelliot.barista:barista-annotations"
    annotationProcessor "com.markelliot.barista:barista-annotations"
    annotationProcessor "com.markelliot.barista:barista-processor"
}

Then, use one of @Http.{Get, Put, Post, Delete} to label any method in any class. Labeled methods from the same class will produce a class in the same package with the name <OriginalClassName>Endpoints that implements com.markelliot.barista.endpoints.Endpoints and collects labeled methods from OriginalClassName. <OriginalClassName>Endpoints comes with a single, public constructor that accepts an implementation of OriginalClassName.

To register these endpoints with Barista, construct <OriginalClassName>Endpoints, pass it an instance of OriginalClassName, and then call Server.builder()...endpoints(<instanceOfEndpoints>).

Customize endpoint function by:

  • HTTP method: select the matching annotation

  • Require authentication: include a method argument with type VerifiedAuthToken

  • Use a path parameter: specify path parameters in the route using {placeholder}, name arguments to the method with names that correspond to placeholder.

  • Use a querystring, header or cookie parameter: specify additional arguments to the method and annotate the arguments with @Query("queryParamName") String param, @Header or @Cookie. Use Optional<String> to allow these parameters to be optional and String to require the presence of the parameters.

    (Note: today, querystring, header and cookie parameters may only be of type String)

  • Use up to one unannotated non-path parameter as the body; Barista's code-generator will attempt to deserialize incoming requests to the type of this parameter.

    (Generics are not presently supported. InputStream is not presently supported.)

  • The return type of the method dictates behavior of the endpoint:

    • HttpRedirect: redirect according to the logic encoded by an instance of HttpRedirect
    • void: send an empty response with HTTP status 201 if the method does not error when executed
    • any concrete T: serialize the response with status code 200

    Future contemplated return types include:

    • HttpResponse: a richer return format that includes a body, headers and cookies
    • InputStream: a producer of raw bytes to write into the response

Some endpoint examples may be found in the processor tests.

An example that returns the JSON string "Hello, world!":

final class GreeterResource {
    /** To greet by name, call GET /greet?name=Your+Name */
    @Http.Get("/greet")
    String greet(@Query Optional<String> name) {
        return "Hello, " + name.orElse("world") + "!";
    }
}

Integrating with Conjure

Barista is compatible with Conjure, and conjure-undertow generated endpoint classes. To use Conjure endpoints, take an additional dependency on barista-conjure:

dependencies {
    implementation "com.markelliot.barista:barista-conjure"
}

And use the ConjureAdapter class to go from conjure-undertow generated classes to Barista-compatible endpoints:

UndertowRuntime conjureRuntime = ConjureUndertowRuntime.builder().build();
Endpoints endpoints = ConjureAdapter.adapt(
        SampleServiceEndpoints.of(new DefaultSampleService()), conjureRuntime);
Server.builder()
        .endpoints(endpoints)
        ...

Creating and Starting the Server

Then, to create and start the server, one can use the Server builder to register the endpoint, correctly set allowed CORS origins and then ultimately start the server:

Authz authz = Authz.denyAll(); // not using authz, so this is a no-op implementation
Server.builder()
    .authz(authz)
    .endpoints(new GreeterResourceEndpoints(new GreeterResource()))
    .allowOrigin("https://example.com")
    .allowOrigin("http://localhost:8080") // for development
    .start();

barista's People

Contributors

bavardage avatar markelliot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.