2 Star 1 Fork 0

丁永 / Vert.x groovy http wrap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
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

// 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
}

空文件

简介

add chain handler/interceptor to vert.x http server 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Groovy
1
https://gitee.com/key232323/Vert.x-groovy-http-wrap.git
git@gitee.com:key232323/Vert.x-groovy-http-wrap.git
key232323
Vert.x-groovy-http-wrap
Vert.x groovy http wrap
master

搜索帮助