dotweb属于一个Web框架,但愿经过框架行为,帮助开发人员快速构建Web应用,提高开发效率,减小没必要要的代码臃肿。git
框架地址:https://github.com/devfeel/dotwebgithub
dotweb包含如下几个经常使用对象:web
本章开始,将对这系列对象作经常使用场景的介绍。缓存
App对象,在dotweb中定义为DotWeb,在框架中,主要承担为Web请求处理提供必要的容器类功能,好比启动Web监听、配置初始化、设置工做模式、设置日志、设置缓存等,同时也承担一系列快捷功能入口。服务器
主要属性:app
属性 | 描述 | 默认值 |
HttpServer |
App承载的WebServer对象 | NewHttpServer() |
Config |
全局配置信息,映射dotweb.conf | config.NewConfig() |
Middlewares |
App级别的中间件集合 | make([]Middleware, 0) |
ExceptionHandler |
自定义处理异常方法 | DefaultHTTPErrorHandler() |
NotFoundHandler |
自定义404请求处理方法 | DefaultNotFoundHandler() |
MethodNotAllowedHandler |
自定义405请求处理方法 | DefaultMethodNotAllowedHandler() |
Items |
App级别全局内存kv存储 | NewItemContext() |
OfflineServer |
维护WebServer | NewOfflineServer() |
主要方法:框架
方法 | 描述 |
Start() |
根据配置启动Web端口监听,若发生错误,返回具体error对象 |
ListenAndServe(addr string) |
指定host:port,根据配置启动Web端口监听 |
Use(m ...Middleware) |
注册中间件,全部请求共享 |
SetDevelopmentMode()\SetProductionMode() |
设置开发模式\生产模式 |
Cache() |
提供全局内存缓存入口 |
UseRequestLog() |
启用基础请求日志中间件 |
SetPProfConfig() |
设置pprof监听端口信息 |
SetLogger(log logger.AppLog) |
设置自定义日志插件 |
常规使用代码:分布式
func main() { //初始化App app := dotweb.New() //设置dotserver日志目录 //若是不设置,默认不启用,且默认为当前目录 app.SetEnabledLog(true) //开启development模式 app.SetDevelopmentMode() //设置路由 InitRoute(app.HttpServer) //自定义404输出 app.SetNotFoundHandle(func(ctx dotweb.Context) { ctx.Response().Write(http.StatusNotFound, []byte("is't app's not found!")) }) //启动 监控服务 app.SetPProfConfig(true, 8081) //全局容器 app.Items.Set("app", "dotweb") // 开始服务 port := 8080 fmt.Println("dotweb.StartServer => " + strconv.Itoa(port)) err := app.StartServer(port) fmt.Println("dotweb.StartServer error => ", err) }
更多代码可参考 https://github.com/devfeel/dotweb-examplespa
欢迎各位加入咱们的go语言QQ群:193409346插件