io.joshworks:snappy-loader

A tiny and powerful server for Java 8

License

License

GroupId

GroupId

io.joshworks
ArtifactId

ArtifactId

snappy-loader
Last Version

Last Version

0.2
Release Date

Release Date

Type

Type

jar
Description

Description

A tiny and powerful server for Java 8

Download snappy-loader

How to add to project

<!-- https://jarcasting.com/artifacts/io.joshworks/snappy-loader/ -->
<dependency>
    <groupId>io.joshworks</groupId>
    <artifactId>snappy-loader</artifactId>
    <version>0.2</version>
</dependency>
// https://jarcasting.com/artifacts/io.joshworks/snappy-loader/
implementation 'io.joshworks:snappy-loader:0.2'
// https://jarcasting.com/artifacts/io.joshworks/snappy-loader/
implementation ("io.joshworks:snappy-loader:0.2")
'io.joshworks:snappy-loader:jar:0.2'
<dependency org="io.joshworks" name="snappy-loader" rev="0.2">
  <artifact name="snappy-loader" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.joshworks', module='snappy-loader', version='0.2')
)
libraryDependencies += "io.joshworks" % "snappy-loader" % "0.2"
[io.joshworks/snappy-loader "0.2"]

Dependencies

test (8)

Group / Artifact Type Version
org.slf4j : jcl-over-slf4j jar 1.7.21
ch.qos.logback : logback-classic jar 1.2.1
org.bouncycastle : bcprov-jdk16 jar 1.46
junit : junit jar 4.12
org.mockito : mockito-core jar 2.7.13
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.springframework : spring-test jar 4.3.4.RELEASE

Project Modules

There are no modules declared in this project.

Snappy

A tiny and powerful web framework for Java 8

Features:

  • Based on Undertow handlers
  • Rest with content negotiation (media type only)
  • Server sent events
  • Server sent events client
  • Websockets
  • Websocket client
  • Built in simple service discovery
  • Small. ~6mb
  • No magic, plain and simple
  • Small memory footprint
  • Static files
  • Rest client
  • Multipart support
  • Executors and schedulers managed by the server
  • Extensible
  • Maven uber jar plugin using Spring boot

Maven setup

    <dependency>
        <groupId>io.joshworks.snappy</groupId>
        <artifactId>snappy</artifactId>
        <version>0.6.0</version>
    </dependency>
    

Import

import static io.joshworks.snappy.SnappyServer.*;
import static io.joshworks.snappy.http.Response.*;

Hello

public class App {

    public static void main(final String[] args) {
       get("/hello", req -> ok("Hello !", "txt")); //or text/plain
       start(); //9000
    }
}

Path parameter

public class App {

    public static void main(final String[] args) {
       get("/hello/{name}", req -> ok("Hello " + req.pathParameter("name"), "txt"));
       
       start();
    }
}

Receiving JSON

public class App {

    public static void main(final String[] args) {
       post("/users", req -> {
           User user = req.body().asObject(User.class);
           //..
           return ok();
       });
       
       start();
    }
}

Sending JSON

public class App {

    public static void main(final String[] args) {
       get("/users", req -> ok(new User("John Doe")));
       start();
    }
}

Filter

public class App {

    public static void main(final String[] args) {
        
       beforeAll("/users", req -> System.out.println("A")); 
       before("/users", req -> System.out.println("B")); 
       after("/users", (req, res) -> System.out.println("C")); 
       afterAll("/users", (req, res) -> System.out.println("D")); 
       
       get("/users", req -> {
           System.out.println("Users");
            return ok();
       });
               
       start();
        
        // Prints:
        //A
        //B
        //Users
        //C
        //D
    }
}

Error handling

public class App {

    public static void main(final String[] args) {
       
       exception(Exception.class, (ex, req) -> Response.internalServerError(ex));
       
       get("/users", exchange -> {
           throw new RuntimeException("Some error");
       });

       
       start();
    }
}

Serving static files

public class App {

    public static void main(final String[] args) {
        //from src/main/resources/static
        //available on /pages
        staticFiles("/pages");
        
//        staticFiles("/pages", "someFolder"); // src/main/resources/someFolder
    }
}

Uploading file

public class App {

    public static void main(final String[] args) {
        post("/fileUpload", req -> {
            Path theFile = req.multiPartBody("theFile").file().path();
            Files.copy(theFile, someOutputStream);
        });
    }
}

Broadcasting data with SSE

public class ClockServer {

    public static void main(final String[] args) {
            ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

            SseBroadcaster broadcaster =  sse("/clock");
            //curl http://localhost:9000/clock

            onStart(() -> scheduler.scheduleAtFixedRate(() -> broadcaster.broadcast(new Date().toString()), 1, 1, TimeUnit.SECONDS));
    
            start();
        }
}

SSE groups / topics

public class App {

    public static void main(final String[] args) {
        SseBroadcaster broadcaster = sse("/subscribe/{group}", sse -> {
            sse.joinGroup(sse.pathParameter("group"));
        });

        //curl http://localhost:9000/subscribe/group-a
        //curl http://localhost:9000/subscribe/group-b

        post("/publish/{group}", req -> {
            String group = req.pathParameter("group");
            String message = req.body().asString();
            broadcaster.broadcast(group, message);
            return ok();
        });
    }
}

Registering a parser

public class App {

    public static void main(final String[] args) {
       Parsers.register(myXmlParser);
       
       get("/xml", req -> ok("<root></root>"), produces("xml"));
       
    }
}

Resource group

public class App {

     public static void main(String[] args) {
            
        // /groupA/a
        // /groupA/b
        // /groupA/subgroup/c
        group("/groupA", () -> {
            get("/a", req -> {/* ... */});
            put("/b", req -> {/* ... */});

            group("/subgroup", () -> {
                get("/c", req -> {/* ... */});
            });
        });

        // /groupB/d
        group("/groupB", () -> {
            get("/d", req -> {/* ... */});
        });
        
        }
}

Uber jar

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>io.joshworks.snappy</groupId>
            <artifactId>snappy-maven-plugin</artifactId>
            <version>0.5.2</version>
        </plugin>
    </plugins>
</build>

Versions

Version
0.2
0.1