tlnet: Lightweight high-performance http service framework [中文]
Function Description
- Ease of Use: Similar in style to the built-in
http
, but simpler to use. - Efficiency: Provides extremely high performance thanks to a routing algorithm based on caching and trees.
- Basic Routing: Supports common HTTP methods (GET, POST, PUT, DELETE, PATCH, etc.).
- Interceptors: Supports interceptors, including URL interception based on regex matching.
- WebSocket: Supports WebSocket with the same simplicity as regular HTTP services.
- Static File Service: Allows setting paths to serve static files, with interceptor support for static services.
- RESTful: Supports building RESTful API services, such as
/path/:id/:name/order
. - Wildcards: Supports using wildcards to define routes, such as
/path/*/order
. - Thrift: Supports Thrift HTTP protocol services.
- Lightweight: Quick to start, minimal resource usage, no redundant features.
GitHub Repository
Applicable Scenarios
- If you are looking for a more efficient and lightweight HTTP framework compared to
gin
,echo
, or nativehttp
,tlnet
is a great choice. It provides fundamental HTTP service functionality with good scalability, stability, and high concurrency support. Due totlnet
’s cache and tree-based implementation, routing has an average time complexity ofO(1)
, offering better performance than traditional tree structures. tlnet
is well-suited for building high-performance, lightweight web applications, APIs, or microservices.tlnet
does not include full-stack development features. For scenarios that require a lot of built-in features, complex business logic, and full-stack development support, frameworks likeBeego
orRevel
might offer more comprehensive functionality.
Quick Start
Installation
go get github.com/donnie4w/tlnet
Example 1:
tl := NewTlnet()
tl.GET("/g", getHandle)
tl.HttpStart(":8000")
func getHandle(hc *HttpContext) {
hc.ResponseString("hello tlnet")
}
Example 2:
tl := NewTlnet()
tl.POST("/p", postHandle) // POST request
tl.HandleStatic("/js/", "/html/js", nil) // Set up static resource service
tl.HttpStart(":8000")
func postHandle(hc *HttpContext) {
hc.ResponseString("hello tlnet")
}
Example 3:
tl := NewTlnet()
tl.GET("/user/:id/:name/order", userOrderHandler) // RESTful API
tl.HttpStart(":8000")
func userOrderHandler(hc *HttpContext) {
hc.ResponseString(hc.GetParam("id") + ":" + hc.GetParam("name"))
}
Interceptor Use Case:
tl := NewTlnet()
tl.HandleWithFilter("/", interceptFilter(), func(hc *HttpContext) {hc.ResponseString("uri:"+hc.ReqInfo.Uri)}) // General HTTP handling with an interceptor
tl.HandleStaticWithFilter("/js/", "/html/js", interceptFilter(), nil)
tl.HttpStart(":8000")
func interceptFilter() *Filter {
f := NewFilter()
// Built-in suffix interceptor
f.AddSuffixIntercept([]string{".jpg"}, func(hc *HttpContext) bool {
logging.Debug("suffix path:" + hc.ReqInfo.Path)
return false
})
// Built-in "page not found" interceptor
f.AddPageNotFoundIntercept(func(hc *HttpContext) bool {
hc.Error("page not found:" + hc.ReqInfo.Uri, http.StatusNotFound)
return true
})
// Custom URL regex matching interceptor
f.AddIntercept(".*?", func(hc *HttpContext) bool {
logging.Debug("intercept:", hc.ReqInfo.Uri)
if hc.ReqInfo.Path == "" {
hc.Error("path is empty:" + hc.ReqInfo.Uri, http.StatusForbidden)
return true
}
return false
})
return f
}
Basic WebSocket Usage:
func TestWebsocket(t *testing.T) {
tl := NewTlnet()
tl.HandleWebSocket("/ws", websocketHandler)
tl.HttpStart(":8000")
}
func websocketHandler(hc *HttpContext) {
wsId := hc.WS.Id // Each WebSocket connection generates a unique ID
msg := string(hc.WS.Read()) // Read client request data
hc.WS.Send("Service ID " + fmt.Sprint(wsId) + " received your message: " + msg) // Respond to client
}
Additional tlnet
Functionality Examples:
SetLogger(true) // Enable logging, disabled by default
tl := NewTlnet() // Create a Tlnet instance
tl.SetMaxBytesReader(1 << 20) // Set the maximum HTTP request data limit to 1 MB
tl.HandleWebSocketBindOrigin("/ws", "http://tlnet.top/", websocketFunc) // Define Origin as http://tlnet.top/
tl.HandleWebSocketBindConfig("/ws", websocketFunc, newWebsocketConfig()) // Define config for WebSocket error handling, connection success, etc.
tl.ReadTimeout(10 * time.Second) // Set read timeout
tl.HttpsStart(":8000", certFile, keyFile) // Start HTTPS service with certFile (certificate path) and keyFile (key path)
tl.HttpsStartWithBytes(":8000", certFileBs, keyFileBs) // Start HTTPS service with certFileBs (certificate data) and keyFileBs (key data)
tl.Close() // Close the Tlnet instance service
Tlnet Performance
《Tlnet Performance Benchmark: tlnet vs gin vs echo vs native http》