Git Product home page Git Product logo

stubbornjava / stubbornjava Goto Github PK

View Code? Open in Web Editor NEW
232.0 19.0 125.0 1.13 MB

Unconventional Java code for building web servers / services without a framework. Think dropwizard but as a seed project instead of a framework. If this project had a theme it would be break the rules but be mindful of your decisions.

Home Page: https://www.stubbornjava.com

License: MIT License

Java 52.13% Shell 0.35% JavaScript 1.50% HCL 1.53% Dockerfile 0.11% Handlebars 43.09% SCSS 1.23% Jinja 0.06%
undertow jackson jooq slf4j logback jool hikaricp java gradle okhttp3

stubbornjava's Introduction

Release Follow @StubbornJava

StubbornJava

https://www.stubbornjava.com/

This is very much a work in progress and in the early stages. No code will be pushed to maven central or supported in any way currently. Feel free to clone and install locally. The website is built using all the methods described on the site and in the examples. There is no backing blog framework.

Potentially moving to GitLab

Quick Example (full example Simple REST server in Undertow)

public static void createUser(HttpServerExchange exchange) {
    User userInput = userRequests.user(exchange);
    User user = userDao.create(userInput.getEmail(), userInput.getRoles());
    if (null == user) {
        ApiHandlers.badRequest(exchange, String.format("User %s already exists.", userInput.getEmail()));
        return;
    }
    exchange.setStatusCode(StatusCodes.CREATED);
    Exchange.body().sendJson(exchange, user);
}

public static void getUser(HttpServerExchange exchange) {
    String email = userRequests.email(exchange);
    User user = userDao.get(email);
    if (null == user) {
        ApiHandlers.notFound(exchange, String.format("User %s not found.", email));
        return;
    }
    Exchange.body().sendJson(exchange, user);
}
    
public static final RoutingHandler ROUTES = new RoutingHandler()
    .get("/users/{email}", timed("getUser", UserRoutes::getUser))
    .post("/users", timed("createUser", UserRoutes::createUser))
    .get("/metrics", timed("metrics", CustomHandlers::metrics))
    .get("/health", timed("health", CustomHandlers::health))
    .setFallbackHandler(timed("notFound", RoutingHandlers::notFoundHandler));

public static final HttpHandler ROOT = CustomHandlers.exception(EXCEPTION_THROWER)
    .addExceptionHandler(ApiException.class, ApiHandlers::handleApiException)
    .addExceptionHandler(Throwable.class, ApiHandlers::serverError);

public static void main(String[] args) {
    SimpleServer server = SimpleServer.simpleServer(Middleware.common(ROOT));
    server.start();
}

Suggest a Topic

Check out issues to suggest topics, bug fixes, errors, or vote on issues. Reactions / comments may influence the order topics are added but no guarantees. Several topics will not be accepted here such as larger frameworks (Spring, Play, Jersey ...) as well as dependency injection. We will be more focused on rolling things yourself and solving practical problems. The coding style here may not fit with the norm but it should be very easy to convert any of the classes to be DI friendly.

Getting Started

A guide for building your own minimal embedded Java web server. A simple in order intro to a lot of uses cases for simple web development.

Dev Tools

HTML / CSS Themes and Templates for rapid prototyping

Libraries

SLF4J is fairly standard and we chose to use Logback as our underlying implementation.

Typesafe config is a clean lightweight immutable configuration library. It offers several formats such as .properties, .yml, .json as well as a human friendly json super set. It handles configuration inheritance, includes, data types (string, boolean, int, long, double, durations, arrays, ...), variabe substitution and many more features. Typesafe Config examples

Jackson for JSON

Undertow is a very fast low level non blocking web server written in Java. It is very lightweight and has a very clean API that should be relatively easy for anyone who knows HTTP to pick up. Most custom code will be in the form of an HttpHandler which is a simple interface that can be used in a variety of ways.

Metrics (Dropwizard Metrics, Grafana, Graphite)

OkHttp for HTTP Client

HikariCP is a very fast lightweight Java connection pool. The API and overall codebase is relatively small (A good thing) and highly optimized. It also does not cut corners for performance like many other Java connection pool implementations.

Uility Classes / Extras

Sites Built With This Method

stubbornjava's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stubbornjava's Issues

Implement search

The site doesn't have the most user friendly navigation implement search.

Post about Jitpack

Talk about using Jitpack as a zero headache nexus alternative for smaller teams who don't want to deal with managing nexus. (Hosted nexus alternative)

CompletableFutures improvement (CompletionStage with sticky Executor)

It can be very frustrating that CompletableFuture always defaults to the global ForkJoinPool.commonPool(). This pool is not meant to be used for blocking operations and can be often misused. Let's use a CompletionStage implementation that remembers its last used Executor and defaults to that.

Immutable POJOs with Lombok

I'm not the biggest fan of lombok but its immutable pojo / builder code generation can be useful. Just don't get carried away and use it for everything.

Bcrypt Java example

Password hashing in Java with BCrypt. It should also support auto updating the hash when iterations are increased.

How to create an Integration Test with this project?

Hi I would like to test and API created with this project but I don´t find a way to stop the server after every test.

SimpleServer server = SimpleServer.simpleServer(Middleware.common(ROOT));
server.start();

Can you help me?

Juan Antonio

Serving static files from resources folder

Wondering if you may help how to use undertow for serving static files (html, css, js, png) along with APIs. I am trying to serve swagger-ui assets from resources along with APIs. Thanks in advance.

Bootstrapping an app

Sometimes you need to ensure resources are loaded before a server starts or ensure resources are closed after some execution.

Turn off -server flag

Tiered compiling, an option introduced in Java 7, uses both the client and server compilers in tandem to provide faster startup time than the server compiler, but similar or better peak performance.[7] Starting in Java 8, tiered compilation is the default for the server VM.[8]

https://en.wikipedia.org/wiki/HotSpot#Features

Lower thread stack size

Our call stacks are not very deep so we can lower the thread stack size from 1MB to something like 256k or 512k

Distributed Session Using Redis

I came across your website and it is extremely helpful. I have been building microservices using Spring Boot for long time and when I came across your website discovered that Spring is so bloated and one could same functionality by just using Undertow. Thanks for taking the effort and being an unofficial evangelist :)

I'd like to request for an example of how to implement distributed session using Redis if someone plans to use Undertow for building stateful services (more like website)

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.