
Simple 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
go get github.com/devfeel/dotweb
package main
import (
"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 )
}| DotWeb | 1.9.2 | 16core16G | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| cpu | 內存 | Samples | Average | Median | 90%Line | 95%Line | 99%Line | Min | Max | Error% | Throughput | Received KB/Sec | Send KB/Sec |
| 40% | 39M | 15228356 | 19 | 4 | 43 | 72 | 204 | 0 | 2070 | 0.00% | 48703.47 | 7514.79 | 8656.28 |
| 40% | 42M | 15485189 | 18 | 4 | 41 | 63 | 230 | 0 | 3250 | 0.00% | 49512.99 | 7639.7 | 8800.16 |
| 40% | 44M | 15700385 | 18 | 3 | 41 | 64 | 233 | 0 | 2083 | 0.00% | 50203.32 | 7746.22 | 8922.86 |
| ECHO | 1.9.2 | 16core16G | |||||||||||
| cpu | 內存 | Samples | Average | Median | 90%Line | 95%Line | 99%Line | Min | Max | Error% | Throughput | Received KB/Sec | Send KB/Sec |
| 38% | 35M | 15307586 | 19 | 4 | 44 | 76 | 181 | 0 | 1808 | 0.00% | 48951.22 | 6166.71 | 9034.94 |
| 36% | 35M | 15239058 | 19 | 4 | 45 | 76 | 178 | 0 | 2003 | 0.00% | 48734.26 | 6139.37 | 8994.9 |
| 37% | 37M | 15800585 | 18 | 3 | 41 | 66 | 229 | 0 | 2355 | 0.00% | 50356.09 | 6343.68 | 9294.24 |
| Gin | 1.9.2 | 16core16G | |||||||||||
| cpu | 內存 | Samples | Average | Median | 90%Line | 95%Line | 99%Line | Min | Max | Error% | Throughput | Received KB/Sec | Send KB/Sec |
| 36% | 36M | 15109143 | 19 | 6 | 44 | 71 | 175 | 0 | 3250 | 0.00% | 48151.87 | 5877.91 | 8746.33 |
| 36% | 40M | 15255749 | 19 | 5 | 43 | 70 | 189 | 0 | 3079 | 0.00% | 48762.53 | 5952.45 | 8857.25 |
| 36% | 40M | 15385401 | 18 | 4 | 42 | 66 | 227 | 0 | 2312 | 0.00% | 49181.03 | 6003.54 | 8933.27 |
Router . GET ( path string , handle HttpHandle )
Router . POST ( path string , handle HttpHandle )
Router . HEAD ( path string , handle HttpHandle )
Router . OPTIONS ( path string , handle HttpHandle )
Router . PUT ( path string , handle HttpHandle )
Router . PATCH ( path string , handle HttpHandle )
Router . DELETE ( path string , handle HttpHandle )
Router . HiJack ( path string , handle HttpHandle )
Router . WebSocket ( path string , handle HttpHandle )
Router . Any ( path string , handle HttpHandle )
Router . RegisterRoute ( routeMethod string , path string , handle HttpHandle )
Router . RegisterHandler ( name string , handler HttpHandle )
Router. GetHandler ( name string ) ( HttpHandle , bool )
Router . MatchPath ( ctx Context , routePath string ) bool接受兩個參數,一個是URI路徑,另一個是HttpHandle 類型,設定匹配到該路徑時執行的方法;
靜態路由語法就是沒有任何參數變量,pattern是一個固定的字符串。
package main
import (
"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
參數路由以冒號: 後面跟一個字符串作為參數名稱,可以通過HttpContext的GetRouterName 方法獲取路由參數的值。
package main
import (
"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:
curl http://127.0.0.1/hello/devfeel
curl http://127.0.0.1/hello/category1/1
g := server . Group ( "/user" )
g . GET ( "/" , Index )
g . GET ( "/profile" , Profile ) test:
curl http://127.0.0.1/user
curl http://127.0.0.1/user/profile
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 ))
}
} 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 }
}HttpServer.EnabledSession
設置是否開啟Session支持,目前支持runtime、redis兩種模式,默認不開啟
HttpServer.EnabledGzip
設置是否開啟Gzip支持,默認不開啟
HttpServer.EnabledListDir
設置是否啟用目錄瀏覽,僅對Router.ServerFile有效,若設置該項,則可以瀏覽目錄文件,默認不開啟
HttpServer.EnabledAutoHEAD
設置是否自動啟用Head路由,若設置該項,則會為除WebsocketHEAD外所有路由方式默認添加HEAD路由,非開發模式默認不開啟
HttpServer.EnabledAutoOPTIONS
設置是否自動啟用Options路由,若設置該項,則會為除WebsocketHEAD外所有路由方式默認添加OPTIONS路由,非開發模式默認不開啟
HttpServer.EnabledIgnoreFavicon
設置是否忽略Favicon的請求,一般用於接口項目
HttpServer.EnabledDetailRequestData
設置是否啟用詳細請求數據統計,默認為false,若設置該項,將啟用ServerStateInfo中DetailRequestUrlData的統計
HttpServer.EnabledTLS
設置是否啟用TLS加密處理
HttpServer.EnabledIgnoreFavicon
設置是否忽略favicon響應,默認為false,若設置該項,將會默認註冊內集成的IgnoreFaviconModule,在路由生效前執行
HttpServer.EnabledBindUseJsonTag
設置是否啟用json tag生效於Bind接口,默認為false,若設置該項,將會在Bind執行時檢查json tag
type ExceptionHandle func ( Context , error ) type NotFoundHandle func (http. ResponseWriter , * http. Request ) websocket - golang.org/x/net/websocket
redis - github.com/garyburd/redigo
yaml - gopkg.in/yaml.v2
dependency now managed by go mod.
項目簡介:http長連接網關服務,提供Websocket及長輪詢服務
項目簡介:基於dotweb與mapper的一款go的博客程序
項目簡介:基於dotweb的一款go的Blog(博客)服務端
項目簡介:token服務,提供token一致性服務以及相關的全局ID生成服務等
項目簡介:微信Access Token中控服務器,用來統一管理各個公眾號的access_token,提供統一的接口進行獲取和自動刷新Access Token。
項目簡介:基於dotweb、dotlog、mapper、dottask、cache、database的綜合項目模板。