https://github.com/icha024/spur
Sinatra inspired library for building REST microservices in Java 8
https://github.com/icha024/spur
Last synced: 6 months ago
JSON representation
Sinatra inspired library for building REST microservices in Java 8
- Host: GitHub
- URL: https://github.com/icha024/spur
- Owner: icha024
- License: apache-2.0
- Created: 2017-04-14T17:15:44.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-16T16:29:50.000Z (over 7 years ago)
- Last Synced: 2025-07-22T12:44:56.008Z (11 months ago)
- Language: Java
- Homepage:
- Size: 92.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spur Framework
[](http://search.maven.org/#search%7Cga%7C1%7Cspur-framework)
[](https://travis-ci.org/icha024/spur)
A Sinatra inspired library for building REST microservices in Java 8. Spur for the cowboy in us!
Lightweight, at 4.6MB in size.
## Simple
```java
package com.example;
import com.clianz.spur.SpurServer;
public class BasicExample {
public static void main(final String[] args) {
new SpurServer()
.get("/", (req, res) -> res.send("Hello World"))
.start();
}
}
```
## Non-Blocking by Default
The server is build around single-threaded event loop by default (reactive pattern).
*IMPORTANT:* Do not block the thread!
- JDBC queries are blocking by default, look into NoSQL with an async clients instead.
- Watchout for synchronous HTTP client, pick an async one instead.
If blocking code is necessary, it can still be enabled through options.
```java
server.start(new SpurOptions().enableBlockableHandlers(false)
```
## Request Validator Built-in
Declare the model with Bean Validator 1.1 tags.
```java
public class Car {
@Min(1)
private int doors;
private String name;
...
}
```
Any HTTP request body of this type will trigger the validation automatically.
### Request/Response JSON Marshaling/Unmarshaling
Server will automatically parse to/from JSON:
```java
new SpurServer()
.post("/test-drive", Car.class, (req, res) -> {
Car car = req.body();
log.info("Validated with Bean Validator 1.1: " + car.getName());
log.info("Sending request body back as response.");
res.send(car);
})
.start();
```
## Simple Scheduler
```java
server.schedule(60, () -> LOGGER.info("This is a runnable task that triggers every 60 seconds"));
```
## Server Side Event (SSE) Support
```java
server.sse("/sse");
server.broadcastSse("/sse", "A Server-Sent-Event (SSE) to everyone listening for events on the endpoint.");
server.schedule(5,
() -> server.broadcastSse("/sse", sseConn -> sseConn.send("Constant spam, by SSE")));
```
## Web-Socket Support
```java
server.websocket("/myapp", res -> {
LOGGER.info("[OnConnectEvent] A user has connected");
res.send("Welcome!");
}, (msg, res) -> {
LOGGER.info("[OnMessageEvent] User message received: " + msg);
res.send("I heard you say: " + msg);
});
server.broadcastWebsockets("/myapp", "Everyone connected to the websocket path /myapp will see this");
server.broadcastWebsockets("/myapp",
"This message will broadcast to websocket users on the path /myapp only if the predicate operator on the key's value is true",
"attrKey", attrVal -> attrVal != null);
```
## HTTP Request Filter/Validator
Since this library does not support sessions by design, JWT should be used for auth.
Filter/pre-request validator may be used for this:
```java
server.preFilterRequests(req -> !req.header("deny")
.isPresent(), res -> res.status(StatusCodes.FORBIDDEN)
.send());
```
Note: This is not applicable web-sockets or SSE.
## Other Server Options
Some other options that can be configured via `server.start(new SpurOptions()...)`
- forceHttps
- enableCorsHeaders
- enableBlockableHandlers
- enableGzip
- gzipMaxSize
- enableBasicAuth
- host
- port
- enableHttps
- sslContext (required when using https)
- httpsPort
- enableHttp2 (only with https)
- requestParseTimeOut
- maxEntitySize
## Examples
See: [Examples code](/example/src/main/java/com/example)
Complete sample project: https://github.com/icha024/spur-example