├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── core ├── build.gradle └── src │ ├── main │ └── groovy │ │ └── graffiti │ │ ├── ContextMixin.groovy │ │ ├── DataSource.groovy │ │ ├── Delete.groovy │ │ ├── Get.groovy │ │ ├── Graffiti.groovy │ │ ├── GraffitiServlet.groovy │ │ ├── Post.groovy │ │ ├── Put.groovy │ │ ├── Server.groovy │ │ ├── Session.groovy │ │ └── WebContextHolder.groovy │ └── test │ └── groovy │ └── graffiti │ ├── HTTP.groovy │ └── TestApplication.groovy ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── samples ├── build.gradle └── src │ ├── datasource │ └── datasource.groovy │ ├── helloworld │ └── helloworld.groovy │ └── methods │ └── methods.groovy └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | *.iml 3 | *.ipr 4 | *.iws 5 | 6 | target 7 | .DS_Store 8 | 9 | .gradle/ 10 | build/ 11 | 12 | gradle.properties 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Kerry Wilson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | What is it? 2 | =========== 3 | Graffiti is a lightweight web framework for Groovy inspired by Sinatra 4 | 5 | 6 | Groovy Start 7 | ============= 8 | 9 | import graffiti.* 10 | 11 | // only required once 12 | @Grab('com.goodercode:graffiti:1.0-SNAPSHOT') 13 | @Get('/hello') 14 | def hello() { 15 | 'Hello World!' 16 | } 17 | 18 | // /hello/name?name=You 19 | @Get('/hello/name') 20 | def helloWhomever() { 21 | "Hello '${params['name']}'" 22 | } 23 | 24 | // a sample post 25 | @Post('/save') 26 | def save() { 27 | 'saved it' 28 | } 29 | 30 | // static files served from here 31 | Graffiti.root 'public' 32 | 33 | // we also have to setup what static files to serve 34 | Graffiti.serve '*.css' 35 | 36 | // required to process annotations 37 | Graffiti.serve this 38 | 39 | // starting web server 40 | Graffiti.start() 41 | 42 | 43 | Running It 44 | =========== 45 | 46 | It's super easy! 47 | 48 | groovy $YOUR_FILE_NAME.groovy 49 | 50 | 51 | Implicit Variables 52 | =================== 53 | 54 | * `application` — ServletContext 55 | * `parameters` — map of parameters 56 | * `request` — HttpServletRequest 57 | * `response` — HttpServletResponse 58 | * `session` — HttpSession 59 | 60 | 61 | That's all for now, not sure if or when I will add more features. Feel free to fork it and give it a go! 62 | 63 | Cheers! -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | allprojects { 2 | apply plugin : 'groovy' 3 | apply plugin : 'idea' 4 | 5 | group = 'com.goodercode' 6 | version = '1.0-SNAPSHOT' 7 | 8 | sourceCompatibility = 1.6 9 | targetCompatibility = 1.6 10 | } 11 | 12 | def jetty = 'org.eclipse.jetty' 13 | def jettyVersion = '8.1.2.v20120308' 14 | def orbit = [groupId : "${jetty}.orbit", artifactId : 'javax.servlet'] 15 | 16 | subprojects { 17 | repositories { 18 | mavenCentral () 19 | } 20 | 21 | dependencies { 22 | groovy 'org.codehaus.groovy:groovy-all:1.8.6' 23 | } 24 | } 25 | 26 | project (':core') { 27 | 28 | configurations { 29 | jettyArtifacts 30 | } 31 | 32 | dependencies { 33 | testCompile ('org.spockframework:spock-core:0.6-groovy-1.8') { 34 | exclude module : 'groovy-all' 35 | } 36 | jettyArtifacts ("${jetty}:jetty-server:${jettyVersion}") { 37 | exclude module : orbit.artifactId 38 | exclude module : 'jetty-util' 39 | } 40 | jettyArtifacts ("${jetty}:jetty-webapp:${jettyVersion}") { 41 | exclude module : orbit.artifactId 42 | } 43 | groovy files (project.configurations.jettyArtifacts.asPath.split(':')) 44 | groovy 'javax.servlet:javax.servlet-api:3.0.1' 45 | groovy 'commons-httpclient:commons-httpclient:3.1' 46 | testCompile 'com.google.guava:guava:11.0.2' 47 | testCompile files (project.configurations.jettyArtifacts.asPath.split(':')) 48 | } 49 | 50 | def jettyArtifactDependenciesNodes = { 51 | configurations.jettyArtifacts.dependencies.collect { 52 | def node = new Node(null, 'dependency') 53 | node.appendNode('groupId', it.group) 54 | node.appendNode('artifactId', it.name) 55 | node.appendNode('version', it.version) 56 | 57 | def exclusions = node.appendNode('exclusions') 58 | def exclude = exclusions.appendNode('exclusion') 59 | orbit.each { 60 | exclude.appendNode(it.key, it.value) 61 | } 62 | return node 63 | } 64 | } 65 | } 66 | 67 | project (':samples') { 68 | dependencies { 69 | compile project(':core') 70 | } 71 | } 72 | 73 | idea.project.ipr { 74 | withXml { provider -> 75 | def map = provider.node.component.find { 76 | it.@name == 'VcsDirectoryMappings' 77 | }.mapping 78 | map.@vcs = 'Git' 79 | map.@directory = project.properties.projectDir 80 | } 81 | } 82 | 83 | task wrapper(type: Wrapper) { 84 | gradleVersion = 1.0 85 | } 86 | -------------------------------------------------------------------------------- /core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin : 'signing' 2 | apply plugin : 'maven' 3 | 4 | archivesBaseName = 'graffiti-core' 5 | 6 | artifacts { 7 | archives jar 8 | } 9 | 10 | signing { 11 | sign configurations.archives 12 | } 13 | 14 | uploadArchives { 15 | repositories.mavenDeployer { 16 | beforeDeployment { MavenDeployment deployment -> 17 | signPom(deployment) 18 | } 19 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 20 | authentication(userName: sonatypeUser, password: sonatypePassword) 21 | } 22 | pom.project { 23 | name 'graffiti' 24 | packaging 'jar' 25 | description 'Graffiti is a lightweight web framework for Groovy inspired by Sinatra' 26 | url projectUrl 27 | 28 | scm { 29 | url scmUrl 30 | connection scmUrl 31 | developerConnection scmUrl 32 | } 33 | 34 | licenses { 35 | license { 36 | name 'The Apache Software License, Version 2.0' 37 | url 'http://www.apache.org/license/LICENSE-2.0.txt' 38 | distribution 'repo' 39 | } 40 | } 41 | 42 | developers { 43 | developer { 44 | id 'webdevwilson' 45 | name 'Kerry Wilson' 46 | } 47 | } 48 | } 49 | 50 | pom.withXml { XmlProvider xmlProvider -> 51 | def xml = xmlProvider.asString() 52 | def pomXml = new XmlParser().parse(new ByteArrayInputStream(xml.toString().bytes)) 53 | 54 | pomXml.version[0] + {packaging ('jar')} 55 | Node dependencies = pomXml.get('dependencies') 56 | 57 | jettyArtifactDependenciesNodes().each { 58 | dependencies.append(it) 59 | } 60 | 61 | def writer = new StringWriter() 62 | def printer = new XmlNodePrinter(new PrintWriter(writer)) 63 | 64 | printer.preserveWhitespace = true 65 | printer.print(pomXml) 66 | xml.setLength(0) 67 | xml.append(writer.toString()) 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/ContextMixin.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | public class ContextMixin { 4 | 5 | def getApplication() { 6 | WebContextHolder.instance.application 7 | } 8 | 9 | def getRequest() { 10 | WebContextHolder.instance.request 11 | } 12 | 13 | def getResponse() { 14 | WebContextHolder.instance.response 15 | } 16 | 17 | def getSession() { 18 | new Session( WebContextHolder.instance.request.session ) 19 | } 20 | 21 | def getParams() { 22 | WebContextHolder.instance.params 23 | } 24 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/DataSource.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import java.lang.annotation.Retention 4 | import java.lang.annotation.RetentionPolicy 5 | 6 | @Retention(RetentionPolicy.RUNTIME) 7 | public @interface DataSource { 8 | String value() 9 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Delete.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import java.lang.annotation.Retention 4 | import java.lang.annotation.RetentionPolicy 5 | 6 | @Retention(RetentionPolicy.RUNTIME) 7 | public @interface Delete { 8 | String value() 9 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Get.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import java.lang.annotation.Retention 4 | import java.lang.annotation.RetentionPolicy 5 | 6 | @Retention(RetentionPolicy.RUNTIME) 7 | public @interface Get { 8 | String value() 9 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Graffiti.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import org.eclipse.jetty.servlet.DefaultServlet 4 | 5 | public class Graffiti { 6 | 7 | static server 8 | 9 | static isStarted = false 10 | 11 | public static final welcomeFiles = [] 12 | 13 | public static final classpath = [] 14 | 15 | static config = [ 'port': '8080', 16 | 'root': 'public', 17 | 'datasources': [:], 18 | 'classpath': classpath, 19 | 'servlets': [], 20 | 'mappings': [], 21 | 'welcomeFiles': []] 22 | 23 | public static serve(obj) { 24 | obj.metaClass { mixin ContextMixin } 25 | obj.metaClass.methods.each { method -> 26 | if( method.class == org.codehaus.groovy.reflection.CachedMethod ) { 27 | 28 | def get = method.cachedMethod.getAnnotation(Get) 29 | if( get ) { 30 | Graffiti.get( get.value(), { method.invoke(obj) } ) 31 | } 32 | 33 | def post = method.cachedMethod.getAnnotation(Post) 34 | if( post ) { 35 | Graffiti.post( post.value(), { method.invoke(obj) } ) 36 | } 37 | 38 | def put = method.cachedMethod.getAnnotation(Put) 39 | if( put ) { 40 | Graffiti.put( put.value(), { method.invoke(obj) } ) 41 | } 42 | 43 | def delete = method.cachedMethod.getAnnotation(Delete) 44 | if( delete ) { 45 | Graffiti.delete( delete.value(), { method.invoke(obj) } ) 46 | } 47 | 48 | def dataSource = method.cachedMethod.getAnnotation(DataSource) 49 | if( dataSource ) { 50 | config.datasources[dataSource.value()] = method.invoke(obj) 51 | } 52 | } 53 | } 54 | 55 | } 56 | 57 | public static serve(String path, servlet = DefaultServlet, configBlock = null) { 58 | config.mappings << ['path': path, 'servlet': servlet, 'configBlock': configBlock] 59 | } 60 | 61 | public static root(String path) { 62 | config.root = path 63 | } 64 | 65 | public static get(path, block) { 66 | register('get', path, block) 67 | } 68 | 69 | public static post(path, block) { 70 | register('post', path, block) 71 | } 72 | 73 | public static put(path, block) { 74 | register('put', path, block) 75 | } 76 | 77 | public static delete(path, block) { 78 | register('delete', path, block) 79 | } 80 | 81 | public static start() { 82 | if( !isStarted ) { 83 | // initialize the server 84 | server = new Server(config) 85 | server.start() 86 | } 87 | } 88 | 89 | private static register(method, path, block) { 90 | def mapping = [ 'method': method, 'path': path, 'block': block ] 91 | if(! isStarted ) { 92 | config.mappings << mapping 93 | } else { 94 | server.map(mapping) 95 | } 96 | } 97 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/GraffitiServlet.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import javax.servlet.http.* 4 | 5 | class GraffitiServlet extends HttpServlet { 6 | 7 | def get, post, put, delete 8 | 9 | public void doGet(HttpServletRequest request, HttpServletResponse response) { 10 | execute(request, response, get) 11 | } 12 | 13 | public void doPost(HttpServletRequest request, HttpServletResponse response) { 14 | execute(request, response, post) 15 | } 16 | 17 | protected void doPut(HttpServletRequest request, HttpServletResponse response) { 18 | execute(request, response, put) 19 | } 20 | 21 | protected void doDelete(HttpServletRequest request, HttpServletResponse response) { 22 | execute(request, response, delete) 23 | } 24 | 25 | private void execute(request, response, block) { 26 | if( block ) { 27 | WebContextHolder.instance.setup( request, response ) 28 | def output = block() 29 | if( output instanceof String || output instanceof GString ) { 30 | response.writer.write( output.toString() ) 31 | } 32 | WebContextHolder.instance.cleanup() 33 | } else { 34 | response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED) 35 | } 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Post.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import java.lang.annotation.Retention 4 | import java.lang.annotation.RetentionPolicy 5 | 6 | @Retention(RetentionPolicy.RUNTIME) 7 | public @interface Post { 8 | String value() 9 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Put.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import java.lang.annotation.Retention 4 | import java.lang.annotation.RetentionPolicy 5 | 6 | @Retention(RetentionPolicy.RUNTIME) 7 | public @interface Put { 8 | String value() 9 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Server.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import org.eclipse.jetty.webapp.WebAppContext 4 | import org.eclipse.jetty.server.session.HashSessionManager 5 | import org.eclipse.jetty.server.session.SessionHandler 6 | import org.eclipse.jetty.servlet.ServletHolder 7 | 8 | class Server { 9 | 10 | def jetty 11 | 12 | def webAppContext 13 | 14 | def dynamicServlets = [:] 15 | 16 | Server(config) { 17 | 18 | jetty = new org.eclipse.jetty.server.Server(config.port as Integer) 19 | webAppContext = new WebAppContext() 20 | 21 | // session setup 22 | def sessionManager = new HashSessionManager() 23 | webAppContext.sessionHandler = new SessionHandler(sessionManager) 24 | 25 | jetty.handler = webAppContext 26 | 27 | loadConfig(config) 28 | 29 | start() 30 | } 31 | 32 | void loadConfig(config) { 33 | 34 | webAppContext.extraClasspath = config.classpath.flatten().join(File.pathSeparator) 35 | webAppContext.welcomeFiles = config.welcomeFiles.flatten() 36 | 37 | setRoot(config.root) 38 | 39 | config.mappings.each { map it } 40 | 41 | webAppContext.configurations.each { println it.class } 42 | 43 | // config.datasources.each { name, value -> 44 | //// register the datasource 45 | // def entry = new org.mortbay.jetty.plus.naming.Resource("jdbc/${name}", (javax.sql.DataSource)value) 46 | // 47 | // 48 | // } 49 | } 50 | 51 | void map(mapping) { 52 | 53 | def path = mapping.path 54 | 55 | // handle GET /path { doSomething() } mappings 56 | if( mapping.method && path && mapping.block ) { 57 | def servlet = dynamicServlets[path] 58 | if( !servlet ) { 59 | servlet = new GraffitiServlet() 60 | map(['path': path, 'servlet': servlet]) 61 | dynamicServlets[path] = servlet 62 | } 63 | servlet[mapping.method] = mapping.block 64 | return 65 | } 66 | 67 | // handle /path -> SomeServlet class mappings 68 | if( mapping.path && mapping.servlet ) { 69 | 70 | def servlet = mapping.servlet 71 | if( servlet instanceof javax.servlet.Servlet ) { 72 | servlet = new ServletHolder(servlet) 73 | println mapping 74 | if( mapping.configBlock ) { 75 | mapping.configBlock.delegate = servlet 76 | mapping.configBlock() 77 | } 78 | } 79 | 80 | println 'adding servlet ' + servlet + ' to ' + path 81 | webAppContext.addServlet(servlet, path) 82 | } 83 | 84 | } 85 | 86 | def setRoot(String root) { 87 | webAppContext.resourceBase = root 88 | } 89 | 90 | void start() { 91 | jetty.start() 92 | } 93 | 94 | void stop() { 95 | jetty.stop() 96 | } 97 | } -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/Session.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import javax.servlet.http.HttpSession 4 | 5 | class Session { 6 | 7 | @Delegate 8 | def HttpSession session 9 | 10 | Session(session) { 11 | this.session = session 12 | } 13 | 14 | def getProperty(String name) { 15 | getAttribute(name) 16 | } 17 | 18 | void setProperty(String name, value) { 19 | setAttribute(name, value) 20 | } 21 | 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /core/src/main/groovy/graffiti/WebContextHolder.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | /** 4 | * WebContextHolder is a singleton to allow for dynamic properties (getProperty, setProperty) 5 | */ 6 | class WebContextHolder { 7 | 8 | static application = [:] 9 | 10 | static final instance = new WebContextHolder() 11 | 12 | private static contextHolder = new ThreadLocal() 13 | 14 | def setup(theRequest, theResponse) { 15 | 16 | contextHolder.set([:]) 17 | 18 | request = theRequest 19 | response = theResponse 20 | 21 | // create parameter map 22 | params = [:] 23 | theRequest.parameterMap.each { k, v -> 24 | if( v.length == 1 ) { 25 | params[k] = v[0] 26 | } else { 27 | params[k] = v 28 | } 29 | } 30 | 31 | } 32 | 33 | def cleanup() { 34 | contextHolder.set(null) 35 | } 36 | 37 | def getProperty(String name) { 38 | contextHolder.get()[name] 39 | } 40 | 41 | void setProperty(String name, value) { 42 | contextHolder.get()[name] = value 43 | } 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /core/src/test/groovy/graffiti/HTTP.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | import com.google.common.io.ByteStreams 4 | import java.net.HttpURLConnection 5 | import java.net.URL 6 | 7 | class HTTP { 8 | 9 | static final String BASE = 'http://localhost:8080' 10 | 11 | def get(url) { 12 | return get(url, [:]) 13 | } 14 | 15 | def get(url, params) { 16 | return request('GET', url, params) 17 | } 18 | 19 | def request(method, path, params) { 20 | 21 | def url = new URL(BASE + path) 22 | HttpURLConnection conn = (HttpURLConnection)url.openConnection() 23 | 24 | conn.requestMethod = method 25 | conn.allowUserInteraction = false 26 | 27 | return new String(ByteStreams.toByteArray( conn.inputStream )) 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /core/src/test/groovy/graffiti/TestApplication.groovy: -------------------------------------------------------------------------------- 1 | package graffiti 2 | 3 | class TestApplication { 4 | 5 | @Get('/status') 6 | def status() { 7 | 'OK' 8 | } 9 | 10 | def start() { 11 | Graffiti.serve this 12 | Graffiti.start() 13 | } 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/webdevwilson/graffiti/6c0a6d107a32291a944a62118c63061d1fc3a636/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Aug 15 18:32:31 JST 2012 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.0-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query businessSystem maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /samples/build.gradle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/webdevwilson/graffiti/6c0a6d107a32291a944a62118c63061d1fc3a636/samples/build.gradle -------------------------------------------------------------------------------- /samples/src/datasource/datasource.groovy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env groovy 2 | import graffiti.* 3 | 4 | @Grapes([ 5 | @Grab('com.goodercode:graffiti:1.0-SNAPSHOT'), 6 | @Grab('mysql:mysql-connector-java:5.1.13') 7 | ]) 8 | @DataSource('mydb') 9 | def getDataSource() { 10 | datasource = new com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource() 11 | datasource.user="root" 12 | datasource.password="" 13 | datasource.url = "jdbc:mysql://localhost:3306/test" 14 | return datasource 15 | } 16 | 17 | @Get('/') 18 | def root() { 19 | 'this is the root of the app' 20 | } 21 | 22 | @Get('/form') 23 | def form() { 24 | "this is the form and this is the message ${session.message}" 25 | } 26 | 27 | @Get('/list') 28 | def list() { 29 | // session.message = 'blah' 30 | 31 | javax.naming.InitialContext ic = new javax.naming.InitialContext(); 32 | javax.sql.DataSource mydb = (javax.sql.DataSource)ic.lookup("java:comp/env/jdbc/mydb"); 33 | println mydb 34 | "the a parameter is '${params['value']}'" 35 | } 36 | 37 | @Post('/save') 38 | def save() { 39 | 'saved it' 40 | } 41 | 42 | Graffiti.classpath << 'resin.jar' 43 | 44 | Graffiti.serve('*.php', 'com.caucho.quercus.servlet.QuercusServlet') { 45 | initParameter('database', 'jdbc/mydb') 46 | } 47 | 48 | Graffiti.root 'public' 49 | Graffiti.serve '*.css' 50 | Graffiti.serve this 51 | 52 | Graffiti.start() -------------------------------------------------------------------------------- /samples/src/helloworld/helloworld.groovy: -------------------------------------------------------------------------------- 1 | @Grab('com.goodercode:graffiti:1.0-SNAPSHOT') 2 | @Grab('commons-httpclient:commons-httpclient:3.1') 3 | import graffiti.* 4 | import org.apache.commons.httpclient.* 5 | import org.apache.commons.httpclient.methods.* 6 | 7 | 8 | @Get('/hello') 9 | def get() { 10 | 'hello world' 11 | } 12 | 13 | Graffiti.serve this 14 | Graffiti.start() -------------------------------------------------------------------------------- /samples/src/methods/methods.groovy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env groovy 2 | @Grab('com.goodercode:graffiti:1.0-SNAPSHOT') 3 | @Grab('commons-httpclient:commons-httpclient:3.1') 4 | 5 | import graffiti.* 6 | import org.apache.commons.httpclient.* 7 | import org.apache.commons.httpclient.methods.* 8 | 9 | 10 | @Get('/blah') 11 | def get() { 12 | println 'get' 13 | } 14 | 15 | @Post('/blah') 16 | def post() { 17 | println 'post' 18 | } 19 | 20 | @Put('/blah') 21 | def put() { 22 | println 'put' 23 | } 24 | 25 | @Delete('/blah') 26 | def delete() { 27 | println 'delete' 28 | } 29 | 30 | Graffiti.serve this 31 | Graffiti.start() 32 | 33 | client = new HttpClient(); 34 | 35 | get = new GetMethod("http://localhost:8080/blah") 36 | post = new PostMethod("http://localhost:8080/blah") 37 | put = new PutMethod("http://localhost:8080/blah") 38 | delete = new DeleteMethod("http://localhost:8080/blah") 39 | 40 | client.executeMethod(get) 41 | get.releaseConnection() 42 | 43 | client.executeMethod(post) 44 | post.releaseConnection() 45 | 46 | client.executeMethod(put) 47 | put.releaseConnection() 48 | 49 | client.executeMethod(delete) 50 | delete.releaseConnection() 51 | 52 | Graffiti.server.stop() 53 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'core', 'samples' 2 | --------------------------------------------------------------------------------