
Framework Web Micro Framework ง่ายและง่าย
สำคัญ: ตอนนี้ต้องการการสนับสนุนเวอร์ชัน 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 )
}| Dotweb | 1.9.2 | 16Core16G | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ซีพียู | หน่วยความจำ | ตัวอย่าง | เฉลี่ย | ค่ามัธยฐาน | บรรทัด 90% | บรรทัด 95% | บรรทัด 99% | นาที | สูงสุด | ข้อผิดพลาด% | ปริมาณงาน | ได้รับ kb/วินาที | ส่ง kb/วินาที |
| 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 | |||||||||||
| ซีพียู | หน่วยความจำ | ตัวอย่าง | เฉลี่ย | ค่ามัธยฐาน | บรรทัด 90% | บรรทัด 95% | บรรทัด 99% | นาที | สูงสุด | ข้อผิดพลาด% | ปริมาณงาน | ได้รับ kb/วินาที | ส่ง kb/วินาที |
| 38% | 35m | 15307586 | 19 | 4 | 44 | 76 | 181 | 0 | พ.ศ. 2351 | 0.00% | 48951.22 | 6166.71 | 9034.94 |
| 36% | 35m | 15239058 | 19 | 4 | 45 | 76 | 178 | 0 | 2546 | 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 | |||||||||||
| ซีพียู | หน่วยความจำ | ตัวอย่าง | เฉลี่ย | ค่ามัธยฐาน | บรรทัด 90% | บรรทัด 95% | บรรทัด 99% | นาที | สูงสุด | ข้อผิดพลาด% | ปริมาณงาน | ได้รับ kb/วินาที | ส่ง kb/วินาที |
| 36% | 36 เมตร | 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 การตั้งค่าวิธีการเพื่อดำเนินการเมื่อจับคู่เส้นทาง;
ไวยากรณ์การกำหนดเส้นทางแบบคงที่หมายความว่าไม่มีตัวแปรพารามิเตอร์รูปแบบเป็นสตริงคงที่
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 )
}ทดสอบ: Curl http://127.0.0.1/hello
การกำหนดเส้นทางพารามิเตอร์ตามด้วยสตริงเป็นชื่อพารามิเตอร์ คุณสามารถรับค่าของพารามิเตอร์การกำหนดเส้นทางผ่านวิธี getRoutername ของ httpContext
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 )
} ทดสอบ:
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 ) ทดสอบ:
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
ตั้งค่าว่าจะเปิดใช้งานการสนับสนุนเซสชันหรือไม่ ปัจจุบันรองรับโหมดรันไทม์และ REDIS และไม่ได้เปิดใช้งานตามค่าเริ่มต้น
httpserver.enabledgzip
ตั้งค่าว่าจะเปิดใช้งานการสนับสนุน GZIP จะไม่สามารถเปิดใช้งานได้โดยค่าเริ่มต้น
httpserver.enabledlistdir
ตั้งค่าว่าจะเปิดใช้งานการเรียกดูไดเรกทอรีมันใช้ได้เฉพาะสำหรับเราเตอร์ ServerFile หากคุณตั้งค่ารายการนี้คุณสามารถเรียกดูไฟล์ไดเรกทอรีและไม่ได้เปิดใช้งานตามค่าเริ่มต้น
httpserver.enabledautohead
ตั้งค่าว่าจะเปิดใช้งานการกำหนดเส้นทางหัวโดยอัตโนมัติหรือไม่ หากรายการนี้ถูกตั้งค่าเส้นทางหัวจะถูกเพิ่มโดยค่าเริ่มต้นสำหรับวิธีการกำหนดเส้นทางทั้งหมดยกเว้น WebSocket Head โหมดการไม่พัฒนาจะไม่สามารถเปิดใช้งานได้โดยค่าเริ่มต้น
httpserver.enabledautooptions
ตั้งค่าว่าจะเปิดใช้งานการกำหนดเส้นทางตัวเลือกโดยอัตโนมัติหรือไม่ หากรายการนี้ถูกตั้งค่าการกำหนดเส้นทางตัวเลือกจะถูกเพิ่มโดยค่าเริ่มต้นสำหรับวิธีการกำหนดเส้นทางทั้งหมดยกเว้น WebSocket Head โหมดการไม่พัฒนาจะไม่สามารถเปิดใช้งานได้โดยค่าเริ่มต้น
httpserver.enabledignorefavicon
ตั้งค่าว่าจะเพิกเฉยต่อคำขอของ Favicon โดยทั่วไปใช้ในโครงการอินเทอร์เฟซ
httpserver.enabledDetailRequestData
ตั้งค่าว่าจะเปิดใช้งานสถิติข้อมูลการร้องขอโดยละเอียดค่าเริ่มต้นเป็นเท็จหรือไม่ หากรายการนี้ถูกตั้งค่าสถิติของ DetailRequesturldata ใน ServerStateInfo จะถูกเปิดใช้งาน
httpserver.enabledtls
ตั้งค่าว่าจะเปิดใช้งานการประมวลผลการเข้ารหัส TLS
httpserver.enabledignorefavicon
ตั้งค่าว่าจะละเว้นการตอบสนอง Favicon ค่าเริ่มต้นเป็นเท็จ หากรายการนี้ถูกตั้งค่าไว้จะมีการลงทะเบียนโดยค่าเริ่มต้นและดำเนินการก่อนที่เส้นทางจะมีผล
httpserver.enabledbindusejontag
ตั้งค่าว่าจะเปิดใช้งานแท็ก JSON ให้มีประสิทธิภาพในอินเตอร์เฟส BIND ค่าเริ่มต้นเป็นเท็จหรือไม่ หากรายการนี้ถูกตั้งค่าแท็ก JSON จะถูกตรวจสอบเมื่อมีการดำเนินการ BIND
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
บทนำโครงการ: โปรแกรมบล็อก A GO ตาม DOTWEB และ MAPPER
บทนำโครงการ: เซิร์ฟเวอร์บล็อก (บล็อก) ตาม DotWeb
บทนำโครงการ: บริการโทเค็นให้บริการความสอดคล้องโทเค็นและบริการสร้าง ID ทั่วโลกที่เกี่ยวข้อง ฯลฯ
การแนะนำโครงการ: เซิร์ฟเวอร์การควบคุมโทเค็นกลางการเข้าถึง WeChat ใช้เพื่อจัดการ Access_Tokens ของบัญชีสาธารณะที่หลากหลายอย่างสม่ำเสมอและให้อินเทอร์เฟซแบบครบวงจรสำหรับการได้รับและรีเฟรชโทเค็นการเข้าถึงโดยอัตโนมัติ
การแนะนำโครงการ: เทมเพลตโครงการที่ครอบคลุมตาม DotWeb, Dotlog, Mapper, Dottask, Cache และฐานข้อมูล