
シンプルで簡単なWebマイクロフレームワーク
重要:Go1.9+バージョンのサポートが必要になり、サポートGo Modが必要です。
ドキュメント:https://www.kancloud.cn/devfeel/dotweb/346608
ガイド: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 )
}| ドットウェブ | 1.9.2 | 16core16g | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CPU | メモリ | サンプル | 平均 | 中央値 | 90%ライン | 95%ライン | 99%ライン | 分 | マックス | エラー% | スループット | KB/Secを受け取りました | 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 |
| エコー | 1.9.2 | 16core16g | |||||||||||
| CPU | メモリ | サンプル | 平均 | 中央値 | 90%ライン | 95%ライン | 99%ライン | 分 | マックス | エラー% | スループット | KB/Secを受け取りました | 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 |
| ジン | 1.9.2 | 16core16g | |||||||||||
| CPU | メモリ | サンプル | 平均 | 中央値 | 90%ライン | 95%ライン | 99%ライン | 分 | マックス | エラー% | スループット | KB/Secを受け取りました | 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 ) bool2つのパラメーターを受け入れます。1つはURIパス、もう1つはhttphandleタイプで、パスを一致させるときに実行する方法を設定します。
静的ルーティング構文は、パラメーター変数がなく、パターンは固定文字列であることを意味します。
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 )
}テスト:カール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 )
}テスト:
カールhttp://127.0.0.1/hello/devfeel
カールhttp://127.0.0.1/hello/category1/1
g := server . Group ( "/user" )
g . GET ( "/" , Index )
g . GET ( "/profile" , Profile )テスト:
カール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
セッションサポートを有効にするかどうかを設定します。現在、ランタイムモードとRedisモードをサポートしており、デフォルトでは有効になっていません。
httpserver.enabledgzip
GZIPサポートを有効にするかどうかを設定すると、デフォルトでは有効になりません
httpserver.enabledListdir
ディレクトリブラウジングを有効にするかどうかを設定すると、router.serverfileでのみ有効です。このアイテムを設定した場合、ディレクトリファイルを閲覧できますが、デフォルトでは有効になりません。
httpserver.enabledautohead
ヘッドルーティングを自動的に有効にするかどうかを設定します。このアイテムが設定されている場合、WebSocket Headを除くすべてのルーティング方法に対してデフォルトでヘッドルートが追加されます。非開発モードはデフォルトでは有効になりません。
httpserver.enabledAutooptions
オプションルーティングを自動的に有効にするかどうかを設定します。このアイテムが設定されている場合、WebSocket Headを除くすべてのルーティング方法に対してデフォルトでオプションルーティングが追加されます。非開発モードはデフォルトでは有効になりません。
httpserver.enabledignorefavicon
一般的にインターフェイスプロジェクトで使用されるFaviconの要求を無視するかどうかを設定します
httpserver.EnabledDetailRequestData
詳細な要求データ統計を有効にするかどうかを設定します。デフォルトはFALSEです。このアイテムが設定されている場合、ServerStateInfoの詳細Requesturldataの統計が有効になります。
httpserver.enabledtls
TLS暗号化処理を有効にするかどうかを設定します
httpserver.enabledignorefavicon
Favicon応答を無視するかどうかを設定します。デフォルトはFALSEです。このアイテムが設定されている場合、統合されたIngroreFavicOnmoduleはデフォルトで登録され、ルートが有効になる前に実行されます。
httpserver.enabledBindusejsontag
JSONタグをBINDインターフェイスで有効にするかどうかを設定します。デフォルトはFALSEです。このアイテムが設定されている場合、Bindが実行されるとJSONタグがチェックされます。
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
Go modによって管理される依存関係。
プロジェクトの紹介:HTTP Long Connection Gateway Service、WebSocketおよびLong Polling Servicesの提供
プロジェクトの紹介:dotwebとmapperに基づくGOブログプログラム
プロジェクトの紹介:dotwebに基づくGOブログ(ブログ)サーバー
プロジェクトの紹介:トークンサービス、トークンの一貫性サービスおよび関連するグローバルID生成サービスなどを提供する。
プロジェクトの紹介:Wechat Access Token Central Control Serverは、さまざまなパブリックアカウントのAccess_Tokensを均一に管理し、アクセストークンを取得して自動的に更新するための統一インターフェイスを提供するために使用されます。
プロジェクトの紹介:dotweb、dotlog、mapper、dottask、キャッシュ、およびデータベースに基づく包括的なプロジェクトテンプレート。