celrenheit / lion Goto Github PK
View Code? Open in Web Editor NEWLion is a fast HTTP router for building modern scalable modular REST APIs in Go
License: MIT License
Lion is a fast HTTP router for building modern scalable modular REST APIs in Go
License: MIT License
I was trying your framework.
Could you provide examples of you access the body for a Post or Put request?
As well getting URL params from a Get request?
Thanks.
Hello, I just released that you don't have full benchmark results, I have created a repository, as new gopher which I ran the benchmark and compare all frameworks here https://github.com/goc0der/go-http-routing-benchmark, can you provide the link to your README or should I PR ?
func main() {
l := lion.New(middleware.Classic())
sub := lion.New()
//normal return
sub.GET("/", func(ctx lion.Context) {
ctx.Write([]byte("/"))
})
//404 page not found ?
sub.GET("/a", func(ctx lion.Context) {
ctx.Write([]byte("a"))
})
//normal return
sub.GET("/b", func(ctx lion.Context) {
ctx.Write([]byte("b"))
})
//404 page not found ?
sub.GET("/c", func(ctx lion.Context) {
ctx.Write([]byte("c"))
})
//normal return
sub.GET("/d", func(ctx lion.Context) {
ctx.Write([]byte("d"))
})
l.Mount("/api", sub)
l.Run()
}
Currently there is only one form of resources (RESTful).
This is a small proposal to extend resource to multiple type.
router.Resource("/users", myResource)
HTTP method | Path | Struct's method name |
---|---|---|
GET | /users | Get |
Head | /users | Head |
POST | /users | Post |
PUT | /users | Put |
PATCH | /users | Patch |
DELETE | /users | Delete |
CONNECT | /users | Connect |
router.Resource("/users", myResource)
HTTP method | Path | Struct's method name |
---|---|---|
GET | /users | Index |
POST | /users | Create |
GET | /users/:id | Show |
PUT or PATCH | /users/:id | Update |
DELETE | /users/:id | Destroy |
Hopefully this could be customized.
I currently have not decided the best way to specify differents types.
The resource could implement an interface or by embedding a type.
Feedback are welcome
Just an FYI,
if you have defined the routes:
GET /hello/contact/named => handler1
GET /hello/contact/:dest => handler2
and then do a request..
GET /hello/contact/nameddd
it would be expected that the route would match /hello/contact/:dest with the param nameddd
, but instead, it returns a 404. This problem grows as you try to compose subrouters.
btw, this does work in the current version of http://github.com/pressly/chi but unfortunately uses recursion which means it uses a few more stack frames for finding the route. I am in the process of updating the tree findNode()
method in chi to use a trampoline function and removing the recursive calls.
compare your method https://github.com/celrenheit/lion/blob/master/tree.go#L138-L209 to https://github.com/pressly/chi/blob/master/tree.go#L181-L253
@celrenheit What would be the simplest form to serve static files with Lion? I'm attempting to get the following folders "css", "js", "img", and "templates" all within a root folder named "public".
for _, dir := range []string{"js", "css", "img", "templates"} { lion.ServeFiles(fmt.Sprintf("/%s/", dir)) }
In following code, I'm found some problens on parameters:
package main
import (
"fmt"
"net/http"
"github.com/celrenheit/lion"
"golang.org/x/net/context"
)
func Handler(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "DNS")
fmt.Fprintf(w, "DNS: "+dns)
}
func Handler2(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "Anything")
dnsDiscography := lion.Param(c, "DNSDiscography")
fmt.Fprintf(w, "DNS: "+dns+" DNSDiscography: "+dnsDiscography)
}
func Handler3(c context.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "I'm here!")
}
func main() {
l := lion.Classic()
l.GetFunc("/artistas/:Anything/discografia/:DNSDiscography/", Handler2)
l.GetFunc("/artistas/:DNS/", Handler)
l.GetFunc("/artistas/", Handler3)
l.Run()
}
Results:
$ curl http://localhost:3000/artistas/test/
DNS:
$ curl http://localhost:3000/artistas/test/discografia/testing/
DNS: test DNSDiscography: testing
If I change Handler to get "Anything" param without change the route, it's get "DNS" from route. Following code and resutls:
package main
import (
"fmt"
"net/http"
"github.com/celrenheit/lion"
"golang.org/x/net/context"
)
func Handler(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "Anything")
fmt.Fprintf(w, "DNS: "+dns)
}
func Handler2(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "Anything")
dnsDiscography := lion.Param(c, "DNSDiscography")
fmt.Fprintf(w, "DNS: "+dns+" DNSDiscography: "+dnsDiscography)
}
func Handler3(c context.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "I'm here!")
}
func main() {
l := lion.Classic()
l.GetFunc("/artistas/:Anything/discografia/:DNSDiscography/", Handler2)
l.GetFunc("/artistas/:DNS/", Handler)
l.GetFunc("/artistas/", Handler3)
l.Run()
}
Results:
$ curl http://localhost:3000/artistas/test/
DNS: test
$ curl http://localhost:3000/artistas/test/discografia/testing/
DNS: test DNSDiscography: testing
I not sure about it, but I thing which /artistas/:Anything/... is match before /artistas/:DNS/ and :Anything overwrite :DNS param.
If I switch positions of url's, I found this results:
package main
import (
"fmt"
"net/http"
"github.com/celrenheit/lion"
"golang.org/x/net/context"
)
func Handler(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "DNS")
fmt.Fprintf(w, "DNS: "+dns)
}
func Handler2(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "Anything")
dnsDiscography := lion.Param(c, "DNSDiscography")
fmt.Fprintf(w, "DNS: "+dns+" DNSDiscography: "+dnsDiscography)
}
func Handler3(c context.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "I'm here!")
}
func main() {
l := lion.Classic()
l.GetFunc("/artistas/:DNS/", Handler)
l.GetFunc("/artistas/:Anything/discografia/:DNSDiscography/", Handler2)
l.GetFunc("/artistas/", Handler3)
l.Run()
}
Results:
$ curl http://localhost:3000/artistas/test/
DNS: test
$ curl http://localhost:3000/artistas/test/discografia/testing/
DNS: DNSDiscography: testing
In this case, if I try to get DNS on Handler2 it's get "Anything" from url. Bellow code and results:
package main
import (
"fmt"
"net/http"
"github.com/celrenheit/lion"
"golang.org/x/net/context"
)
func Handler(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "DNS")
fmt.Fprintf(w, "DNS: "+dns)
}
func Handler2(c context.Context, w http.ResponseWriter, r *http.Request) {
dns := lion.Param(c, "DNS")
dnsDiscography := lion.Param(c, "DNSDiscography")
fmt.Fprintf(w, "DNS: "+dns+" DNSDiscography: "+dnsDiscography)
}
func Handler3(c context.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "I'm here!")
}
func main() {
l := lion.Classic()
l.GetFunc("/artistas/:DNS/", Handler)
l.GetFunc("/artistas/:Anything/discografia/:DNSDiscography/", Handler2)
l.GetFunc("/artistas/", Handler3)
l.Run()
}
$ curl http://localhost:3000/artistas/test/
DNS: test
$ curl http://localhost:3000/artistas/test/discografia/testing/
DNS: test DNSDiscography: testing
go get github.com/celrenheit/lion
# github.com/celrenheit/lion
../../celrenheit/lion/middlewares.go:20: undefined: color.FgHiMagenta
../../celrenheit/lion/middlewares.go:24: undefined: color.FgHiBlue
../../celrenheit/lion/middlewares.go:25: undefined: color.FgHiGreen
might want to vendor, or add to the docs
Implementation of a contextual renderer:
I would like your feedback on what is your preferred way of handling responses and status codes.
Currently, it works like this (1):
func render(c Context) {
c.WithStatus(http.StatusOK).
JSON(data)
}
But I might consider the following way (2):
func render(c Context) {
c.JSON(http.StatusOK, data)
}
Please, add a comment to this issue with "1" for the first or "2" for the second.
func User(w http.ResponseWriter, r *http.Request, params denco.Params) {
fmt.Fprintf(w, "Hello %s!\n", params.Get("name"))
}
mux.GET("/user/:name", User),
FastHTTP is a much faster HTTP library for Go.
Echo v2 already supports it.
It'd be nice to have it in other frameworks as well.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.