gorest 一个简单的用go实现的restful 框架 gorest

ejunjsh • 2709 次点击    
这是一个分享于 的项目,其中的信息可能已经有所发展或是发生改变。
# gorest [![Build Status](https://travis-ci.org/ejunjsh/gorest.svg?branch=master)](https://travis-ci.org/ejunjsh/gorest) [![baby-gopher](https://raw.githubusercontent.com/drnic/babygopher-site/gh-pages/images/babygopher-badge.png)](http://www.babygopher.org) a restful go framework ## install ````bash go get github.com/ejunjsh/gorest ```` ## usage ### import ````go import "github.com/ejunjsh/gorest" ```` ### create a app and run a server ````go app:=gorest.NewApp() app.[Get/Post/Delete/Put/Error] app.Run(":8081") ```` ### supports 4 methods of http request ````go app.Get("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...}) app.Post("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...}) app.Delete("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...}) app.Put("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error {...}) ```` ### supports parameters from url path ````go app.Get("/:abc/:cba", func(r *gorest.HttpRequest, w gorest.HttpResponse) error { fmt.Println(w.PathParams["abc"],w.PathParams["cba"]) return nil }) ```` ### supports string,json,xml,file,template as result of return ````go app.Get("/", func(r *gorest.HttpRequest, w gorest.HttpResponse) error { return w.WriteJson(jsonObj) //w.WriteString(str) //w.WriteXml(xmlObj) //w.WriteFile("/Users/zhouff/file.txt") //w.WriteTemplates(data,"/Users/zhouff/index.html","/Users/zhouff/header.html") }) ```` ### supports dealing with errors ````go app.Error(func(err error, r *gorest.HttpRequest, w gorest.HttpResponse){ if e,ok:=err.(gorest.NoFoundError);ok { w.Write([]byte(e.Error())) } if e,ok:=err.(gorest.InternalError);ok { w.Write([]byte(e.Error())) } }) ```` ### see [example](https://github.com/ejunjsh/gorest/blob/master/main/main.go)
授权协议:
开发语言:
go 查看源码»
2709 次点击  
加入收藏 微博
1 回复  |  直到
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传