# Vert.x groovy http wrap **Repository Path**: key232323/Vert.x-groovy-http-wrap ## Basic Information - **Project Name**: Vert.x groovy http wrap - **Description**: add chain handler/interceptor to vert.x http server - **Primary Language**: Groovy - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2014-02-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Vert.x-groovy-http-wrapper ============ It's a http wrapper which add interceptor, router(copy from RouteMatcher), smarty template support(use smarty4j), json handler etc. There is a watch dog to compile groovy handlers in develop mode. Getting started ------------------ ### download vert.x and install mod-lang-groovy ### run gradle task > gradle -q uploadArchives ### run vert.x > vertx run server.groovy ### visit in browsers eg. http://localhost:8888 ```groovy // file base.groovy package handler import com.alibaba.fastjson.JSON def staticFileUrlPat = /.*\.(js|css|html)/ def staticImageUrlPat = /.*\.(jpg|jpeg|png|gif|ico)/ handler.getRegEx(staticFileUrlPat){req, resp -> resp.sendFile 'webroot' + req.path }.getRegEx(staticImageUrlPat){req, resp -> def mat = req.path =~ staticImageUrlPat String ext = mat ? mat[0][1] : 'jpg' resp.headers.set('Content-Type', 'image/' + ext) vertx.fileSystem.readFile('webroot' + req.path){ar -> if(ar.succeeded){ resp.chunked = true resp << ar.result resp.end() }else{ resp.setStatusCode(404).end() } } }.setNoMatchHandler{req, resp -> resp.sendFile 'webroot/404.html' }.setErrorHandler{req, resp, ex -> resp.sendFile 'webroot/500.html' } // file test.groovy package handler // log/handler(ChainHandler)/vertx handler.get('/1') {req, resp -> resp.end '1' }.get('/2') {req, resp -> resp.end '2' }.get('/3') {req, resp -> resp.render('webroot/tpl/test.html', [val: '中文']) }.get('/4') {req, resp -> def val = [:] for(it in 1..20){ val['key' + it] = 'val' + it } resp.json(val) }.get('/download'){req, resp -> vertx.fileSystem.readFile('D:/tmp/title.rar'){ar -> if(ar.succeeded){ resp.download('test.rar', ar.result.bytes, null) }else{ resp.end 'Failed' } } }.post('/jsonSend'){req, resp -> req.jsonHandler{json -> json.key2 = 'value2' resp.json json } }.post('/upload'){req, resp -> def names = [] req.expectMultiPart = true req.endHandler{ def params = [:] def attrs = req.formAttributes for(attr in attrs){ params[attr.key] = attr.value } log.info params resp.end 'Upload files : ' + names } req.uploadHandler{upload -> // not success maybe names << upload.filename upload.streamToFileSystem 'uploads/test_pre_' + upload.filename } }.post('/uploadSync'){req, resp -> req.uploadSyncHandler{buffer, params -> log.info params // test // throw new RuntimeException('xxx') resp.end 'upload ok : 10 bytes : ' + buffer.getBytes(0, 10) } }.addInterceptor('printDate', handler.PRE, /^\/2$/){req, resp -> log.info new Date() // continue execute return false } ```