• This repository has been archived on 24/Aug/2023
  • Stars
    star
    49
  • Rank 585,874 (Top 12 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GServ: Framework for SPAs and REST based services.

What is gServ?

gServ is a tool for creating and deploying REST based services using Groovy without the hassle of a container (JBoss, Tomcat, etc.) . Using gServ, you can easily define REST resources as Groovy scripts or embed gServ in your application. gServ is perfect for creating lightweight micro services.
        <br/><h3>Features:</h3>
        <ul class="docs-content">
            <li>Container Free
            <li>Serve static files
            <li>Serve Groovy script as REST resources
            <li><a href="https://github.com/javaConductor/gserv/wiki/gServ-HATEOAS"> HATEOAS Support</a>
            <li>Easy Content Negotiation
            <li>Plugin API
            <li><a href="https://github.com/javaConductor/gserv/wiki/gServ-Framework">Embeddable</a>
            <li><a href="https://github.com/javaConductor/gserv/wiki/gServ-Standalone">Standalone Mode</a>
            <li>CORS support
            <li>Compression support
            <li>ETag support
            <li>Basic Authentication
            <li> HTTPS
        </ul>
            <h3>Requirements:</h3>
            <ul><li>Java JDK 1.6+</li>
                <li>Groovy 2.3+ (Framework only)</li>
            </ul>

<div class="docs-content">
    <h3>Basic Concepts</h3>
    <table width="90%" class="docs-content">
        <tr><th>Term</th>             <th>Meaning</th></tr>
        <tr><td>Action</td>      <td>HTTP request handler for a particular path/query/method combination.  </td></tr>
        <tr><td>Resources</td>      <td>Resources define actions for a particular root path (eg. /books) and its sub-resources (eg. /books/bestSellers). </td></tr>
        <tr><td>Server Config</td><td>The config encapsulates any resources, actions, filters, and plugins. </td></tr>
        <tr><td>Server Instance</td><td>This is the actual server instance that will listen to the specified port and handle
            requests based on its configuration.</td></tr>
    </table>
</div>
gServ can be used in two ways
[Framework](https://github.com/javaConductor/gserv/wiki/gServ-Framework)
[Standalone](https://github.com/javaConductor/gserv/wiki/gServ-Standalone) #Simple Examples
Creating REST Resources

/// create a GServ instance def gserv = new GServ()

/// Create a Books REST resource def bkResource = gserv.resource("/books") { // URI: /books/faq get “/faq”, file(“BooksFaq.html”)

// URI: /books/xyz
get “:id”, { id ->
    def book = bookService.get( id )
    writeJson book
}

// responds  to /books/all
get “/all”, {  ->
    def books = bookService.allBooks ()
    header “content-type”, “application/json”
    writeJSON books
}

}

The root path is passed to the GServ.resource() method along with a closure defining the actions for the resource.
Creating a Server Instance

gserv.http { // setup a directory for static files static_root '/public/webapp'

//static FAQ page located at '/public/webapp/App.faq.html'
get '/faq', file("App.faq.html")

}.start(8080);

The http() method creates a GServInstance that can later listen on a port and handle HTTP requests. This server instance defines static roots usually used for templates for single-page apps and a single FAQ page. Then, after the server instance is returned from the http() method, we can immediately call start(8080) on it.
Adding Resources to a Server Instance

def bkResource = gserv.resource("/books") { ... } def userResource = gserv.resource("/users") { ... }

gserv.http { // setup a directory for static files static_root "/public/webapp"

// static FAQ page located at '/public/webapp/App.faq.html'
get '/faq', file('App.faq.html')

/// add Book and User REST resources to our GServ instance
resource bkResource
resource userResource

}.start(8080);

A server instance can be created by simply adding resources. Here we add our 2 resources: bkResources and userResources. Now, all URIs related to both resources are available once the instance is started. This instance also defines a static_root which tells gserv where to find static files such as the FAQ page which should be at /public/webapp/App.faq.html.