Giter VIP home page Giter VIP logo

undertow-vh's Introduction

undertow-vh

This library is serving as a wrapper for Java's most performant webserver undertow to easily manage and deploy multiple virtual hosts.

Gradle

implementation 'com.displee:undertow-vh:4.3-SNAPSHOT'

Usage

Create a virtual host

package com.displee.web.localhost

import com.displee.undertow.host.VirtualHost
import com.displee.undertow.host.route.send
import com.google.gson.JsonObject

class LocalHost : VirtualHost("localhost") {

    override fun routes() {
        super.routes() //for class based routes
        get("/", publicHtml().resolve("index.html"))
        get("/test") {
            val json = JsonObject()
            json.addProperty("message", "Hello")
            it.send(json)
        }
        get("/pebble", privateHtml().resolve("test.peb"), mapOf(Pair("message", "Undertow is awesome!")))
    }

}

Start the webserver

val server = UndertowVH("0.0.0.0", 80, 443)
server.register(LocalHost())
server.start()

Class based route

Class based routes must be placed in a package named 'route'. This 'route' package should be in the same package as the virtual host class.

package com.displee.web.localhost.route

import com.displee.undertow.host.route.*
import com.displee.undertow.host.route.impl.TemplateRouteHandler
import com.google.gson.JsonObject
import io.undertow.server.HttpServerExchange
import io.undertow.util.Methods

@RouteManifest("/register", Methods.GET_STRING)
class SampleRoute : TemplateRouteHandler() {

    override fun handleRequest(exchange: HttpServerExchange) {
        //check if we've received the required query params
        if (!exchange.checkQueryParameters("username", "first_name", "last_name")) {
            exchange.responseSender.send("Error received not enough parameters.")
            return
        }
        val queryParameters = exchange.getQueryParametersAsMap()
        val username = queryParameters["username"]
        val firstName = queryParameters["first_name"]
        val lastName = queryParameters["last_name"]
        //we can do any logic here, for example put the data above in a database
        val json = JsonObject()
        json.addProperty("error", -1)
        json.addProperty("message", "Successfully registered!")
        exchange.send(json)
    }

}

Pebble/Twig templating

You can easily put Java objects in the front-end using Pebble templating. Declaring Pebble variables is not necessary but it helps your IDE recognize variable types.

When transferring Java objects to the front-end you can use them like json.

{# @pebvariable name="session" type="io.undertow.server.session.Session" #}
{# @pebvariable name="request_uri" type="java.lang.String" #}
{# @pebvariable name="message" type="java.lang.String" #}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
    You're currently on route: "{{ request_uri }}"
    <br/>
    Your session id is: {{ session.id }}
    <br/>
    {{ message }}
</body>
</html>

Contribute

Feel free to fork and submit pull requests :)

Credits

undertow-vh's People

Contributors

displee avatar yassincoffeeit avatar

Watchers

 avatar  avatar

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.