Skip to content

vaccovecrana/murmux

Repository files navigation

Murmux

Murmux

Zero-dependencies HTTP Framework based on ExpressJS

Getting Started

public class Example {
  public static void main(String[] args) {
    new Murmux()
      .rootHandler(xc -> xc.commitText("Hello world"))
      .listen(8080); // Will listen on port 8080
  }
}

Installation

Available in Maven Central.

implementation("io.vacco.murmux:murmux:<LATEST_VERSION>")

Routing

Direct

You can add routes (And middlewares) directly to handle requests. Direct also supports methods like POST PATCH DELETE and PUT, others need to be created manually:

DirectRouting

With Router

However, it's better to split your code. You can create routes and add them at runtime:

DeferredRouting

URL Basics

You can create handlers for all request-methods and contexts. Some examples:

HttpMethods

URL Parameters

Sometimes you want to create dynamic URLs where some parts are not static. With the : operator you can create variables in the URL which will be saved later in a HashMap.

Example request: GET /posts/john/all:

UrlParameters

URL Query

If you make a request which contains queries, you can access the queries over req.getQuery(NAME).

Example request: GET /posts?page=12&from=john:

UrlQuery

Cookies

With req.getCookie(NAME) you can get a cookie by his name, and with res.setCookie(NAME, VALUE) you can easily set a cookie.

Example request: GET /setcookie and GET /showcookie:

Cookies

Form data

Use req.getFormQuery(NAME) to receive values from input elements of an HTML-Form.

FormData

Warning: Currently, File-inputs don't work, if there is a File-input the data won't get parsed!

Middleware

Middleware allows you to handle a request before it reaches any other request handler.

Middlewares work exactly as request handlers.

You can also filter by request-methods and contexts:

Existing Middlewares

Core middlewares are included in MxMiddleware.

CORS

To realize a CORS api you can use the CORS middleware.

Cors

Static Content

Use MxStatic to serve static content.

Example:

StaticContent

Cookie Session

A simple in-memory cookie-session middleware is provided:

CookieSession

Examples

File download

FileDownload

Development

Requires Gradle 7.1 or later.

Create a file with the following content at ~/.gsOrgConfig.json:

{
  "orgId": "vacco-oss",
  "orgConfigUrl": "https://vacco-oss.s3.us-east-2.amazonaws.com/vacco-oss.json"
}

Then run:

gradle clean build