Go 语言编写的一个轻量、高性能的 HTTP Router

栏目: IT技术 · 发布时间: 4年前

内容简介:CleverGo is an enhanced version ofCleverGoOnly explicit matches:With other routers, like

CleverGo

CleverGo is an enhanced version of julienschmidt/httprouter which providesextra useful features.

Contents

Motivation

CleverGo isn't an another web framework , it aims to be a lightweight , feature-rich and high performance HTTP router which can be intergrates with any third-party packages(such as HTTP middlewares) easily.

Features

Only explicit matches:With other routers, like http.ServeMux , a requested URL path could match multiple patterns. Therefore they have some awkward pattern priority rules, like longest match or first registered, first matched . By design of this router, a request can only match exactly one or no route. As a result, there are also no unintended matches, which makes it great for SEO and improves the user experience.

Stop caring about trailing slashes:Choose the URL style you like, the router automatically redirects the client if a trailing slash is missing or if there is one extra. Of course it only does so, if the new path has a handler. If you don't like it, you can turn off this behavior .

Path auto-correction:Besides detecting the missing or additional trailing slash at no extra cost, the router can also fix wrong cases and remove superfluous path elements (like ../ or // ). Is CAPTAIN CAPS LOCK one of your users? HttpRouter can help him by making a case-insensitive look-up and redirecting him to the correct URL.

Parameters in your routing pattern:Stop parsing the requested URL path, just give the path segment a name and the router delivers the dynamic value to you. Because of the design of the router, path parameters are very cheap.

Zero Garbage:The matching and dispatching process generates zero bytes of garbage. The only heap allocations that are made are building the slice of the key-value pairs for path parameters, and building new context and request objects (the latter only in the standard Handler / HandlerFunc API). In the 3-argument API, if the request path contains no parameters not a single heap allocation is necessary.

High Performance

Perfect for APIs:The router design encourages to build sensible, hierarchical RESTful APIs. Moreover it has built-in native support for OPTIONS requests and 405 Method Not Allowed replies.

Of course you can also set custom NotFound and MethodNotAllowed handlers and serve static files .

Extra Features

  • Named Routes: allow the reverse route generation of URLs.
  • Save Matched Route: allow to retrieve matched route in handler, it is useful to generate URLs of the current route.
  • Nestable Route Groups: as known as subrouter.
  • Middleware: just a function func(http.Handler) http.Handler , it can not only integrates third-party middleware easily, but also can be used in three scopes: root router, subrouter and route.

Usage

All usage and examples can be found at GoDoc :

Middleware

There are a lot of third-party middlewares can be used out of box, such as:

  • clevergo/middleware : a collection of HTTP middleware, adapter for gorilla handlers(compress and logging).
  • gorilla/handlers : a collection of useful middleware for Go HTTP services & web applications.
  • goji/httpauth : basic auth middleware.
  • List other middlewares here by PR.

Chaining

Chain allow to attach any middlewares on a http.Handler .

Differences

You can skip this section if you have not use httprouter before.

The usage of this package is very similar to httprouter, but there are serveral important differences you should pay attention for.

  • There is no Handle type anymore, you can registers http.Handler and http.HandlerFunc by Router.Handle and Router.HandleFunc respectively. And GetParams is the only way to retrieve Params in handler.
  • Params.ByName was renamed to Params.Get , and added some useful functions for converting value type:
  • ParamsFromContext was removed, use GetParams instead.
  • Router.PanicHandler was removed, it is more reasonable to use RecoveryMiddleware in the top level instead.
  • Router methods GET , POST , PUT , DELETE , PATCH , HEAD , OPTIONS were renamed to Get , Post , Put , Delete , Patch , Head , Options respectively.

Contribute

  • Give it a :star:️ and spread the package.
  • File an issue for features or bugs.
  • Fork and make a pull request.

FAQ

Why not contribute to the original repository?

There are multiple reasons:

  • Slow maintenance, such as the PR of subrouter is still unaccepted.
  • Breaking compatibility for introducing features, seeabove.

Anyway, httprouter is definitely an awesome package.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据库系统实现

数据库系统实现

加西亚-莫利纳(Hector Garcia-Molina)、Jeffrey D.Ullman、Jennifer Widom / 杨冬青、吴愈青、包小源 / 机械工业出版社 / 2010-5 / 59.00元

《数据库系统实现(第2版)》是斯坦福大学计算机科学专业数据库系列课程第二门课的教科书。书中对数据库系统实现原理进行了深入阐述,并具体讨论了数据库管理系统的三个主要成分——存储管理器、查询处理器和事务管理器的实现技术。此外,第2版充分反映了数据管理技术的新进展,对内容进行了扩充,除了在第1版中原有的“信息集成”一章(第10章)中加入了新的内容外,还增加了两个全新的章:“数据挖掘”(第11章)和“数据......一起来看看 《数据库系统实现》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换