Giter Site home page Giter Site logo

clevergo / clevergo Goto Github PK

View Code? Open in Web Editor NEW
249.0 28.0 46.0 309 KB

:tongue: CleverGo is a lightweight, feature rich and high performance HTTP router for Go.

Home Page: https://clevergo.tech

License: MIT License

Go 100.00%
httprouter restful-api golang router web-framework api high-performance middleware fast rest-api restful routing

clevergo's Introduction

CleverGo

Build Status Coverage Status Go Report Card Go.Dev reference Release Downloads Chat Community

CleverGo is a lightweight, feature rich and trie based high performance HTTP request router.

go get -u clevergo.tech/clevergo

Benchmark

Features

  • Full features of HTTP router.
  • High Performance: extremely fast, see Benchmark.
  • Gradual learning curve: you can learn the entire usages by going through the documentation in half an hour.
  • Reverse Route Generation: allow generating URLs by named route or matched route.
  • Route Group: as known as subrouter.
  • Friendly to APIs: it is easy to design RESTful APIs and versioning your APIs by route group.
  • Middleware: plug middleware in route group or particular route, supports global middleware as well. Compatible with most of third-party middleware.
  • Logger: a generic logger interface, supports zap and logrus. Logger can be used in middleware or handler.
  • ...

Examples

Checkout example for details.

Contribute

Contributions are welcome.

  • Star it and spread the package.
  • File an issue to ask questions, request features or report bugs.
  • Fork and make a pull request.
  • Improve documentations.

Credit

See CREDIT.md.

clevergo's People

Contributors

beikege avatar echonil avatar erichuang1994 avatar headwindfly avatar monkeywithacupcake avatar razonyang avatar whatdoesfoxsaywoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clevergo's Issues

路由功能加强

建议支持这种路由

r.Get("/a", func(c *clevergo.Context) error {
	return c.String(http.StatusOK, "/a")
})
r.Get("/a/b", func(c *clevergo.Context) error {
	return c.String(http.StatusOK, "/a/b")
})
// 优先级最低
r.Get("/*any", func(c *clevergo.Context) error {
	return c.String(http.StatusOK, c.Params.String("any"))
})

当前提示

panic: wildcard segment '*any' conflicts with existing children in path '/*any'

如何在中间件中获取ctx.Response响应状态?

当前无法获取 ctx.Response 状态码

func TimeShow(next clevergo.Handle) clevergo.Handle {
	return func(ctx *clevergo.Context) error {
		start := time.Now()
		next(ctx)
		// ctx.Response.Status()
		fmt.Printf("%s %s %d\n", time.Since(start).String(), ctx.Request.URL.String(), 200)
		return nil
	}
}

需要包装 http.ResponseWriter 接口

type ResponseWriter struct {
	http.ResponseWriter
	size   int
	status int
	written bool
}

建议添加一些常用的快捷输出方法.

类似echo框架

JSON(code int, i interface{}) error
HTML(code int, html string) error
String(code int, s string) error
Render(code int, name string, data interface{}) error
Cookies() []*http.Cookie
Cookie(name string) (*http.Cookie, error)
SetCookie(cookie *http.Cookie)
...

框架很好用👍感谢作者,有没有电报交流群?

session doc from https://clevergo.tech/zh/basics/session/ is empty

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

/hello/xxx/ match route app.Get("/hello/:name" should be 404 not 301

Describe the bug
http://localhost:8080/hello/aa retrun 200
http://localhost:8080/hello/aa/ return 301 should be 404

To Reproduce
Steps to reproduce the behavior:
1.
test.go

package main
import (
	"fmt"
	"net/http"

	"clevergo.tech/clevergo"
)
func hello(c *clevergo.Context) error {
	return c.String(http.StatusOK, fmt.Sprintf("Hello %s!", c.Params.String("name")))
}

func main() {
	app := clevergo.New()
	app.Get("/hello/:name", hello)   
	app.Run(":8080")
}

go run test.go
2.
curl curl http://localhost:8080/hello/aa/
Expected behavior
A clear and concise description of what you expected to happen.

curl http://localhost:8080/hello/aa/
return 404 like net/http
main.go

package main
import (
    "fmt"
    "net/http"
)
func main() {
    http.HandleFunc("/hello/aa", func (w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome to my website!")
    })
   http.ListenAndServe(":80", nil)
}
go run main.go 

curl http://localhost:80/hello/aa/ return 404 page not found

Additional context
Add any other context about the problem here.

中间件优先级疑问

英文太差,请原谅我使用中文.

package main

import (
	"fmt"
	"net/http"

	"github.com/clevergo/clevergo"
)

func middleware(name string) clevergo.MiddlewareFunc {
	return func(next clevergo.Handle) clevergo.Handle {
		return func(ctx *clevergo.Context) error {
			fmt.Printf("开始: %s\n", name)
			if err := next(ctx); err != nil {
				return err
			}
			fmt.Printf("结束: %s\n", name)
			return nil
		}
	}
}

func myHandle(ctx *clevergo.Context) error {
	fmt.Println("执行: handle")
	return nil
}

func main() {
	router := clevergo.NewRouter()
	router.Use(middleware("全局中间件"))

	test := router.Group("/test", clevergo.RouteGroupMiddleware(middleware("路由组中间件")))
	test.Get("/abc", myHandle, clevergo.RouteMiddleware(middleware("路由中间件")))

	http.ListenAndServe(":80", router)
}
http://127.0.0.1/test/abc
输出:
开始: 全局中间件
开始: 路由中间件
开始: 路由组中间件
执行: handle
结束: 路由组中间件
结束: 路由中间件
结束: 全局中间件

路由中间件的优先级高于路由组的,是否合理?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.