From f2bc4b34d02c8d905c7d95faa877e2c55ceaeada Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Wed, 8 Jul 2015 13:56:05 -0500 Subject: [PATCH] Reworked GroovyDirectoryServer into a proper class. --- .../net/GroovyDirectoryServer.groovy | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/main/groovy/com/jdbernard/net/GroovyDirectoryServer.groovy b/src/main/groovy/com/jdbernard/net/GroovyDirectoryServer.groovy index 85bec11..fd6c055 100755 --- a/src/main/groovy/com/jdbernard/net/GroovyDirectoryServer.groovy +++ b/src/main/groovy/com/jdbernard/net/GroovyDirectoryServer.groovy @@ -1,33 +1,42 @@ -#!/usr/bin/env groovy - package com.jdbernard.net import org.eclipse.jetty.server.Server import org.eclipse.jetty.servlet.* import groovy.servlet.* + public class GroovyDirectoryServer { -def startJetty(int port) { - def server = new Server(port) - - def handler = new ServletContextHandler(ServletContextHandler.SESSIONS) - handler.contextPath = '/' - handler.resourceBase = '.' + public static void runJetty(int port) { + def server = new Server(port) + + def handler = new ServletContextHandler(ServletContextHandler.SESSIONS) + handler.contextPath = '/' + handler.resourceBase = '.' - // Groovy Scripts - handler.addServlet(GroovyServlet, '*.groovy') + // Groovy Scripts + handler.addServlet(GroovyServlet, '*.groovy') - // Files - def filesHolder = handler.addServlet(DefaultServlet, '/') - filesHolder.setInitParameter('resourceBase', '.') - - server.handler = handler - server.start() + // Files + def filesHolder = handler.addServlet(DefaultServlet, '/') + filesHolder.setInitParameter('resourceBase', '.') + + server.handler = handler + server.start() + } + + public static void main(String[] args) { + def port = 9002 + + if (args.length < 1) { + port = 9002 + println "Defaulting to port 9002" } + + else try { port = args[0] as int } + catch(Exception e) { + println "Usage: GroovyDirectoryServer.grooy " + System.exit(1) } + + println "Starting Jetty on port $port, press Ctrl-C to stop." + runJetty(port) + } } - - if (args.length < 1) { - println "Usage: webServer.groovy " - System.exit(1) } - -println "Starting Jetty, press Ctrl-C to stop." -startJetty(Integer.parseInt(args[0]))