กรอบบริการเว็บที่มีน้ำหนักเบาและมีประสิทธิภาพสูงสำหรับการเดินทาง
การจัดทำเอกสาร
// package gear
import "github.com/teambition/gear" https://github.com/teambition/gear/tree/master/example/hello
app := gear . New ()
// Add logging middleware
app . UseHandler ( logging . Default ( true ))
// Add router middleware
router := gear . NewRouter ()
// try: http://127.0.0.1:3000/hello
router . Get ( "/hello" , func ( ctx * gear. Context ) error {
return ctx . HTML ( 200 , "<h1>Hello, Gear!</h1>" )
})
// try: http://127.0.0.1:3000/test?query=hello
router . Otherwise ( func ( ctx * gear. Context ) error {
return ctx . JSON ( 200 , map [ string ] any {
"Host" : ctx . Host ,
"Method" : ctx . Method ,
"Path" : ctx . Path ,
"URI" : ctx . Req . RequestURI ,
"Headers" : ctx . Req . Header ,
})
})
app . UseHandler ( router )
app . Error ( app . Listen ( ":3000" ))https://github.com/teambition/gear/tree/master/example/http2
package main
import (
"net/http"
"github.com/teambition/gear"
"github.com/teambition/gear/logging"
"github.com/teambition/gear/middleware/favicon"
)
// go run example/http2/app.go
// Visit: https://127.0.0.1:3000/
func main () {
const htmlBody = `
<!DOCTYPE html>
<html>
<head>
<link href="/hello.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Hello, Gear!</h1>
</body>
</html>`
const pushBody = `
h1 {
color: red;
}
`
app := gear . New ()
app . UseHandler ( logging . Default ( true ))
app . Use ( favicon . New ( "./testdata/favicon.ico" ))
router := gear . NewRouter ()
router . Get ( "/" , func ( ctx * gear. Context ) error {
ctx . Res . Push ( "/hello.css" , & http. PushOptions { Method : "GET" })
return ctx . HTML ( 200 , htmlBody )
})
router . Get ( "/hello.css" , func ( ctx * gear. Context ) error {
ctx . Type ( "text/css" )
return ctx . End ( 200 , [] byte ( pushBody ))
})
app . UseHandler ( router )
app . Error ( app . ListenTLS ( ":3000" , "./testdata/out/test.crt" , "./testdata/out/test.key" ))
}https://github.com/teambition/gear/tree/master/example/staticgo
ติดตั้งด้วย GO:
go install github.com/teambition/gear/example/staticgo@latest เป็นเครื่องมือ CMD ที่มีประโยชน์ที่ให้บริการไฟล์ในเครื่องของคุณเป็นเว็บเซิร์ฟเวอร์ (รองรับ TLS) คุณสามารถสร้าง osx , linux , เวอร์ชัน windows ด้วย make build
package main
import (
"flag"
"github.com/teambition/gear"
"github.com/teambition/gear/logging"
"github.com/teambition/gear/middleware/cors"
"github.com/teambition/gear/middleware/static"
)
var (
address = flag . String ( "addr" , "127.0.0.1:3000" , `address to listen on.` )
path = flag . String ( "path" , "./" , `static files path to serve.` )
certFile = flag . String ( "certFile" , "" , `certFile path, used to create TLS static server.` )
keyFile = flag . String ( "keyFile" , "" , `keyFile path, used to create TLS static server.` )
)
func main () {
flag . Parse ()
app := gear . New ()
app . UseHandler ( logging . Default ( true ))
app . Use ( cors . New ())
app . Use ( static . New (static. Options { Root : * path }))
logging . Println ( "staticgo v1.2.0, created by https://github.com/teambition/gear" )
logging . Printf ( "listen: %s, serve: %s n " , * address , * path )
if * certFile != "" && * keyFile != "" {
app . Error ( app . ListenTLS ( * address , * certFile , * keyFile ))
} else {
app . Error ( app . Listen ( * address ))
}
}https://github.com/teambition/gear/tree/master/example/Gearproxy
ติดตั้งด้วย GO:
go install github.com/teambition/gear/example/gearproxy@latest
gearproxy -helphttps://github.com/teambition/gear/tree/master/example/grpc_server
https://github.com/teambition/gear/tree/master/example/grpc_client
Gear.router เป็นตัวจัดการคำขอ HTTP ฐาน Trie คุณสมบัติ:
405 Method Not AllowedOPTIONS การจัดการอัตโนมัติเส้นทางที่ลงทะเบียนซึ่งเราเตอร์ตรงกับคำขอที่เข้ามาสามารถมีพารามิเตอร์หกประเภท:
| ไวยากรณ์ | คำอธิบาย |
|---|---|
:name | พารามิเตอร์ชื่อ |
:name(regexp) | ตั้งชื่อด้วยพารามิเตอร์ regexp |
:name+suffix | ชื่อพารามิเตอร์ที่มีการจับคู่คำต่อท้าย |
:name(regexp)+suffix | ตั้งชื่อด้วยพารามิเตอร์ regexp และการจับคู่คำต่อท้าย |
:name* | ตั้งชื่อด้วยพารามิเตอร์ catch-all |
::name | ไม่มีชื่อพารามิเตอร์มันเป็นตัวอักษร :name |
พารามิเตอร์ที่มีชื่อคือเซ็กเมนต์เส้นทางแบบไดนามิก พวกเขาจับคู่อะไรจนกว่าจะถึง '/' หรือเส้นทางต่อไป:
กำหนด: /api/:type/:ID
/api/user/123 matched: type="user", ID="123"
/api/user no match
/api/user/123/comments no matchตั้งชื่อด้วยพารามิเตอร์ regexp จับคู่อะไรโดยใช้ regexp จนกว่าจะถึง '/' ถัดไปหรือปลายเส้นทาง:
กำหนด: /api/:type/:ID(^d+$)
/api/user/123 matched: type="user", ID="123"
/api/user no match
/api/user/abc no match
/api/user/123/comments no matchชื่อพารามิเตอร์ที่มีคำต่อท้ายเช่น Google API Design:
กำหนด: /api/:resource/:ID+:undelete
/api/file/123 no match
/api/file/123 : undelete matched: resource="file", ID="123"
/api/file/123 : undelete /comments no matchตั้งชื่อด้วยพารามิเตอร์ regexp และคำต่อท้าย:
กำหนด: /api/:resource/:ID(^d+$)+:cancel
/api/task/123 no match
/api/task/123 : cancel matched: resource="task", ID="123"
/api/task/abc : cancel no matchตั้งชื่อด้วยพารามิเตอร์ catch-all จับคู่อะไรจนกว่าจะสิ้นสุดเส้นทางรวมถึงดัชนีไดเรกทอรี ('/' ก่อนที่จะจับทั้งหมด) เนื่องจากพวกเขาจับคู่อะไรจนกว่าจะสิ้นสุดพารามิเตอร์ catch-all ต้องเป็นองค์ประกอบเส้นทางสุดท้ายเสมอ
กำหนด: /files/:filepath*
/files no match
/files/LICENSE matched: filepath="LICENSE"
/files/templates/article.html matched: filepath="templates/article.html"
ค่าของพารามิเตอร์จะถูกบันทึกไว้ใน params Matched.Params ดึงค่าของพารามิเตอร์ตามชื่อ:
type := matched . Params ( "type" )
id := matched . Params ( "ID" )เกียร์ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT ลิขสิทธิ์© 2016-2023 Teambition