Skip to content

soklet/soklet-guice

Repository files navigation

⚠️ Project Retired

This repository will no longer be maintained except for critical security fixes and should not be used for new development.

It is superseded by Soklet 2.0.0, which no longer provides formal support for Guice (it is now simple to bring-your-own DI).

  • No new features or bug fixes will be made except for critical security fixes
  • Issues and pull requests are not monitored
  • The repository is kept for historical reference only

Replacement: https://github.com/soklet/soklet
Final release: 1.0.6 (January 26, 2019)

Soklet Guice

What Is It?

Guice integration for Soklet, a minimalist infrastructure for Java webapps and microservices.

License

Apache 2.0

Maven Installation

<dependency>
  <groupId>com.soklet</groupId>
  <artifactId>soklet-guice</artifactId>
  <version>1.0.6</version>
</dependency>

Direct Download

If you don't use Maven, you can drop soklet-guice-1.0.6.jar directly into your project. You'll also need Guice 4.0 as a dependency.

Example Code

// SokletModule provides default implementations for Soklet interfaces like ResponseHandler.
// You can override them in your own module as needed
public static void main(String[] args) throws Exception {
  Injector injector = createInjector(Modules.override(new SokletModule()).with(new AppModule()));
  Server server = injector.getInstance(Server.class);
  server.start();
  System.in.read(); // Wait for keypress
  server.stop();
}

class AppModule extends AbstractModule {
  // SokletMatchers.httpMethodMatcher() is a Guice Matcher which matches any resource method.
  // For example, methods annotated with @GET("/test") and @DELETE("/users/{id}") would be matched.
  // This is useful for applying method interceptors to handle security, database transactions, and more
  @Override
  protected void configure() {
    bindInterceptor(Matchers.annotatedWith(Resource.class),
      SokletMatchers.httpMethodMatcher(), new MyCustomInterceptor());
  }

  // Use Guice as you would normally
  @Provides
  @Singleton
  public Server provideServer(InstanceProvider instanceProvider) {
    return JettyServer.forInstanceProvider(instanceProvider).port(8080).build();
  }  

  // Override Soklet's default implementations as needed
  @Provides
  @Singleton
  public PageResponseWriter providePageResponseWriter() {
    // My app should use a custom Mustache.java-backed page response writer
    return new MyMustachePageResponseWriter();
  }
}

About

Soklet Guice was created by Mark Allen and sponsored by Transmogrify, LLC.

About

This repository has been retired as of Soklet 2.0.0's release.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages