首页 > 其他分享 >Greenlight - Choosing a router

Greenlight - Choosing a router

时间:2024-11-14 11:40:21浏览次数:1  
标签:httprouter responses ServeMux Choosing go router com Greenlight

We’re going to use the popular third-party package httprouter as the router for our application, instead of using http.ServeMux from the standard-library.

There are two reasons for this:

  • We want our API to consistently send JSON responses wherever possible. Unfortunately, http.ServeMux sends plaintext (non-JSON) 404 and 405 responses when a matching route cannot be found, and it’s not possible to easily customize these without causing a knock-on effect that inhibits the automatic sending of 405 responses. There is an open proposal to improve this in future versions of Go, but for now it’s a pretty significant drawback.
  • Additionally — and less importantly for most applications — http.ServeMux does not automatically handle OPTIONS requests.

Both of these things are supported by httprouter , along with providing all the other functionality that we need. The package itself is stable and well-tested, and as a bonus it’s also extremely fast thanks to its use of a radix tree for URL matching. If you’re building a REST API for public consumption, then httprouter is a solid choice.

 

zzh@ZZHPC:~/zd/Github/greenlight$ go get github.com/julienschmidt/httprouter@v1
go: downloading github.com/julienschmidt/httprouter v1.3.0
go: added github.com/julienschmidt/httprouter v1.3.0

 

标签:httprouter,responses,ServeMux,Choosing,go,router,com,Greenlight
From: https://www.cnblogs.com/zhangzhihui/p/18545686

相关文章

  • Greenlight - API versioning
    APIversioningAPIswhichsupportreal-worldbusinessesandusersoftenneedtochangetheirfunctionality andendpointsovertime—sometimesinabackwards-incompatibleway.So,toavoid problemsandconfusionforclients,it’sagoodideatoalwaysimp......
  • Greenlight - Directory Structure
     $mkdir-pbincmd/apiinternalmigrationsremote$touchMakefile$touchcmd/api/main.goAtthispointyourprojectdirectoryshouldlookexactlylikethis:greenlight├──bin├──cmd│└──api│└──main.go├──internal├─......
  • Greenlight - Endpoints and Actions
    MethodURLPatternActionGET/v1/healthcheckShowapplicationhealthandversioninformationGET/v1/moviesShowthedetailsofallmoviesPOST/v1/moviesCreateanewmovieGET/v1/movies/:idShowthedetailsofaspecificmoviePA......
  • 【React Router】基于 react-router-dom 的路由配置与导航详解
    文章目录一、ReactRouter组件概述1.ReactRouter的作用2.主要组件介绍二、ReactRouter的基本使用1.安装与配置2.配置基础路由3.路由重定向三、嵌套路由与布局1.嵌套路由2.带参路由四、路由守卫与重定向1.路由守卫2.路由重定向五、总结React是现......
  • "vue-router/composables" 中为什么没有提供 onBeforeRouteEnter?
    在Vue3中,vue-router提供了新的组合式API(Composables),这些API旨在与Vue3的CompositionAPI一起使用。然而,onBeforeRouteEnter这个特定的导航守卫并没有直接在vue-router/composables中提供。原因主要有以下几点:1.组合式API的设计理念组合式API的设计理念是将......
  • React-Router-Dom6 最佳实践
    React-Router-Dom6最佳实践一咻世界都变了​关注 6人赞同了该文章​展开目录 react-router-dom6使用之前只使用过一次react-router,目前官方从5开始就已经放弃了原有的react-router库,统一命名为 react-router-dom了实现......
  • vuex、vue-router实现原理
    Vuex和VueRouter是Vue.js生态系统中非常重要的两个库,分别用于状态管理和路由管理。它们各自的实现原理如下:Vuex实现原理1.状态管理Vuex是一个专为Vue.js应用程序开发的状态管理模式。它使用集中式的存储管理所有组件的状态,并以一种可预测的方式来确保状态以一种可追......
  • Vue项目中动态路由与权限控制:router.beforeEach的使用及无token重定向登录页
    在现代前端项目中,权限控制是一个非常重要的环节。VueRouter作为Vue官方的路由管理器,为我们提供了强大的路由管理功能。在本文中,我们将探讨如何在Vue项目中使用router.beforeEach钩子函数来实现动态路由权限控制,并在用户未登录时自动重定向到登录页。步骤一:登录并获取Token首......
  • vue3 如何使用router路由表 创建 Menu 导航菜单
    vue3如何使用router路由表创建Menu导航菜单1.vue3如何使用router路由表创建Menu导航菜单1.1.安装VueRouter1.2.设置路由1.3.在主应用中使用路由1.4.创建导航菜单组件1.5.在布局中使用导航菜单 1.vue3如何使用router路由表创建M......
  • 3.fastapi的路由分发include_router
    1.main文件中添加prefix指定参数,urls中不添加路由前缀的效果2.main文件中添加prefix指定参数,urls中添加路由前缀的效果3.购物中心接口运行结果_get请求_food4.购物中心接口运行结果_get请求_bed5.用户中心接口运行结果_post请求_login6.用户中心接口运行结果_post请求_reg......