Go开源: DotWeb - 简约大方的 Go Web 微型框架

bluestar 7年前
   <h2>DotWeb</h2>    <p>简约大方的go Web微型框架</p>    <h2>安装:</h2>    <pre>  <code class="language-go">go get -u github.com/devfeel/dotweb</code></pre>    <h2>快速开始:</h2>    <pre>  <code class="language-go">func StartServer() error {      //初始化DotServer      dotserver := dotweb.New()      //设置dotserver日志目录      dotserver.SetLogPath("/home/logs/wwwroot/")      //设置路由      dotserver.HttpServer.GET("/index", func(ctx *dotweb.HttpContext) {          ctx.WriteString("welcome to my first web!")      })      //开始服务      err := dotserver.StartServer(80)      return err  }</code></pre>    <h2>特性</h2>    <ul>     <li>支持静态路由、参数路由</li>     <li>路由支持文件/目录服务</li>     <li>中间件支持</li>     <li>支持JSON/JSONP/HTML格式输出</li>     <li>统一的HTTP错误处理</li>     <li>统一的日志处理</li>     <li>支持Hijack与websocket</li>    </ul>    <h2>路由</h2>    <p>常规路由</p>    <p>目前支持GET\POST\HEAD\OPTIONS\PUT\PATCH\DELETE 这几类请求方法 另外也支持HiJack\WebSocket\ServerFile三类特殊应用</p>    <pre>  <code class="language-go">1、HttpServer.GET(path string, handle HttpHandle)  2、HttpServer.POST(path string, handle HttpHandle)  3、HttpServer.HEAD(path string, handle HttpHandle)  4、HttpServer.OPTIONS(path string, handle HttpHandle)  5、HttpServer.PUT(path string, handle HttpHandle)  6、HttpServer.PATCH(path string, handle HttpHandle)  7、HttpServer.DELETE(path string, handle HttpHandle)  8、HttpServer.HiJack(path string, handle HttpHandle)  9、HttpServer.WebSocket(path string, handle HttpHandle)  10、HttpServer.RegisterRoute(routeMethod string, path string, handle HttpHandle)</code></pre>    <p>接受两个参数,一个是URI路径,另一个是 HttpHandle 类型,设定匹配到该路径时执行的方法;</p>    <p>静态路由</p>    <p>静态路由语法就是没有任何参数变量,pattern是一个固定的字符串。</p>    <pre>  <code class="language-go">package main    import (      "github.com/devfeel/dotweb"  )    func main() {      dotserver := dotweb.New()      dotserver.Get("/hello", func(ctx *dotweb.HttpContext) {          ctx.WriteString("hello world!")      })      dotserver.StartServer(80)  }</code></pre>    <p>测试: curl <a href="/misc/goto?guid=4959741094082254791" rel="nofollow,noindex">http://127.0.0.1/hello</a></p>    <p>参数路由</p>    <p>参数路由以冒号 : 后面跟一个字符串作为参数名称,可以通过 HttpContext的 GetRouterName 方法获取路由参数的值。</p>    <pre>  <code class="language-go">package main    import (      "github.com/devfeel/dotweb"  )    func main() {      dotserver := dotweb.New()      dotserver.Get("/hello/:name", func(ctx *dotweb.HttpContext) {          ctx.WriteString("hello " + ctx.GetRouterName("name"))      })      dotserver.Get("/news/:category/:newsid", func(ctx *dotweb.HttpContext) {          category := ctx.GetRouterName("category")      newsid := ctx.GetRouterName("newsid")          ctx.WriteString("news info: category=" + category + " newsid=" + newsid)      })      dotserver.StartServer(80)  }</code></pre>    <p>测试:</p>    <p>curl <a href="/misc/goto?guid=4959741094173093415" rel="nofollow,noindex">http://127.0.0.1/hello/devfeel</a></p>    <p>curl <a href="/misc/goto?guid=4959741094255236807" rel="nofollow,noindex">http://127.0.0.1/hello/category1/1</a></p>    <h2>中间件(拦截器)</h2>    <p>RegisterModule</p>    <ul>     <li>支持OnBeginRequest、OnEndRequest两类中间件</li>     <li>通过实现HttpModule.OnBeginRequest、HttpModule.OnEndRequest接口实现自定义中间件</li>     <li>通过设置HttpContext.End()提前终止请求</li>    </ul>    <h2>异常</h2>    <p>500错误</p>    <ul>     <li>默认设置: 当发生未处理异常时,会根据DebugMode向页面输出默认错误信息或者具体异常信息,并返回 500 错误头</li>     <li>自定义: 通过DotServer.SetExceptionHandle(handler *ExceptionHandle)实现自定义异常处理逻辑</li>    </ul>    <pre>  <code class="language-go">type ExceptionHandle func(*HttpContext, interface{})</code></pre>    <h2>外部依赖</h2>    <p>httprouter - github.com/julienschmidt/httprouter</p>    <p>websocket - golang.org/x/net/websocket</p>    <h2>相关项目</h2>    <p><a href="/misc/goto?guid=4959741094338070928" rel="nofollow,noindex">TokenServer</a></p>    <p>项目简介:token服务,提供token一致性服务以及相关的全局ID生成服务等</p>    <h2> </h2>    <p> </p>