简约大方的 go Web 微型框架
- go get -u github.com/devfeel/dotweb
- 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
- }
目前支持 GET\POST\HEAD\OPTIONS\PUT\PATCH\DELETE 这几类请求方法 另外也支持 HiJack\WebSocket\ServerFile 三类特殊应用
- 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)
接受两个参数,一个是 URI 路径,另一个是 HttpHandle 类型,设定匹配到该路径时执行的方法;
静态路由语法就是没有任何参数变量,pattern 是一个固定的字符串。
- 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)
- }
测试: curl
参数路由以冒号 : 后面跟一个字符串作为参数名称,可以通过 HttpContext 的 GetRouterName 方法获取路由参数的值。
- 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)
- }
测试:
curl
curl
- type ExceptionHandle func(*HttpContext, interface{})
httprouter - github.com/julienschmidt/httprouter
websocket - golang.org/x/net/websocket
项目简介:token 服务,提供 token 一致性服务以及相关的全局 ID 生成服务等
QQ 群:193409346
来源: http://www.tuicool.com/articles/nIvemqM