在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:dotweb开源软件地址:https://gitee.com/devfeel/dotweb开源软件介绍:DotWebSimple and easy go web micro framework Important: Now need go1.9+ version support, and support go mod. Document: https://www.kancloud.cn/devfeel/dotweb/346608 Guide: https://github.com/devfeel/dotweb/blob/master/docs/GUIDE.md 1. Installgo get github.com/devfeel/dotweb 2. Getting Startedpackage mainimport ( "fmt" "github.com/devfeel/dotweb")func main() { //init DotApp app := dotweb.New() //set log path app.SetLogPath("/home/logs/wwwroot/") //set route app.HttpServer.GET("/index", func(ctx dotweb.Context) error{ return ctx.WriteString("welcome to my first web!") }) //begin server fmt.Println("dotweb.StartServer begin") err := app.StartServer(80) fmt.Println("dotweb.StartServer error => ", err)} examples: https://github.com/devfeel/dotweb-example3. Features
Config Example4. Performance
5. Router1) 常规路由
1、Router.GET(path string, handle HttpHandle)2、Router.POST(path string, handle HttpHandle)3、Router.HEAD(path string, handle HttpHandle)4、Router.OPTIONS(path string, handle HttpHandle)5、Router.PUT(path string, handle HttpHandle)6、Router.PATCH(path string, handle HttpHandle)7、Router.DELETE(path string, handle HttpHandle)8、Router.HiJack(path string, handle HttpHandle)9、Router.WebSocket(path string, handle HttpHandle)10、Router.Any(path string, handle HttpHandle)11、Router.RegisterRoute(routeMethod string, path string, handle HttpHandle)12、Router.RegisterHandler(name string, handler HttpHandle)13、Router.GetHandler(name string) (HttpHandle, bool)14、Router.MatchPath(ctx Context, routePath string) bool 接受两个参数,一个是URI路径,另一个是 HttpHandle 类型,设定匹配到该路径时执行的方法; 2) static router静态路由语法就是没有任何参数变量,pattern是一个固定的字符串。 package mainimport ( "github.com/devfeel/dotweb")func main() { dotapp := dotweb.New() dotapp.HttpServer.GET("/hello", func(ctx dotweb.Context) error{ return ctx.WriteString("hello world!") }) dotapp.StartServer(80)} test:curl http://127.0.0.1/hello 3) parameter router参数路由以冒号 : 后面跟一个字符串作为参数名称,可以通过 HttpContext的 GetRouterName 方法获取路由参数的值。 package mainimport ( "github.com/devfeel/dotweb")func main() { dotapp := dotweb.New() dotapp.HttpServer.GET("/hello/:name", func(ctx dotweb.Context) error{ return ctx.WriteString("hello " + ctx.GetRouterName("name")) }) dotapp.HttpServer.GET("/news/:category/:newsid", func(ctx dotweb.Context) error{ category := ctx.GetRouterName("category") newsid := ctx.GetRouterName("newsid") return ctx.WriteString("news info: category=" + category + " newsid=" + newsid) }) dotapp.StartServer(80)} test: 4) group router g := server.Group("/user") g.GET("/", Index) g.GET("/profile", Profile) test: 6. Binder
type UserInfo struct { UserName string `form:"user"` Sex int `form:"sex"`}func TestBind(ctx dotweb.HttpContext) error{ user := new(UserInfo) if err := ctx.Bind(user); err != nil { return ctx.WriteString("err => " + err.Error()) }else{ return ctx.WriteString("TestBind " + fmt.Sprint(user)) }} 7. MiddlewareMiddleware
app.Use(NewAccessFmtLog("app"))func InitRoute(server *dotweb.HttpServer) { server.GET("/", Index) server.GET("/use", Index).Use(NewAccessFmtLog("Router-use")) g := server.Group("/group").Use(NewAccessFmtLog("group")) g.GET("/", Index) g.GET("/use", Index).Use(NewAccessFmtLog("group-use"))}type AccessFmtLog struct { dotweb.BaseMiddlware Index string}func (m *AccessFmtLog) Handle(ctx dotweb.Context) error { fmt.Println(time.Now(), "[AccessFmtLog ", m.Index, "] begin request -> ", ctx.Request.RequestURI) err := m.Next(ctx) fmt.Println(time.Now(), "[AccessFmtLog ", m.Index, "] finish request ", err, " -> ", ctx.Request.RequestURI) return err}func NewAccessFmtLog(index string) *AccessFmtLog { return &AccessFmtLog{Index: index}} 8. Server ConfigHttpServer:
Run Mode
9. Exception500 error
type ExceptionHandle func(Context, error) 404 error
type NotFoundHandle func(http.ResponseWriter, *http.Request) Dependencywebsocket - golang.org/x/net/websocket dependency now managed by go mod. 相关项目LongWeb项目简介:http长连接网关服务,提供Websocket及长轮询服务 yulibaozi.com项目简介:基于dotweb与mapper的一款go的博客程序 Golang-Blog-Server项目简介:基于dotweb的一款go的Blog(博客)服务端 TokenServer项目简介:token服务,提供token一致性服务以及相关的全局ID生成服务等 Wechat-token项目简介:微信Access Token中控服务器,用来统一管理各个公众号的access_token,提供统一的接口进行获取和自动刷新Access Token。 dotweb-start项目简介:基于dotweb、dotlog、mapper、dottask、cache、database的综合项目模板。 Contact UsQQ-Group:193409346 -Gitter: |
请发表评论