草庐IT

httpRouter

全部标签

go - 如何使用 julien schmidt 的路由器避免 Go 中的全局变量?

我正在使用Go开发RESTfulAPI,但由于应用配置、身份验证等原因,我有很多全局变量。由于流行的推荐,我正在使用JulienSchmidt的httprouter,并且正在寻找避免全局变量的可行方法。这是一些代码。我正在使用中间件对使用gorrila/securecookie的用户进行身份验证。funcAuthMiddleware(handlerhttprouter.Handle,isLoggedInbool)httprouter.Handle{returnfunc(whttp.ResponseWriter,r*http.Request,pshttprouter.Params){if

http - 重定向返回 http : multiple response. WriteHeader 调用

我正在使用JonCalhoun'sGoMVCframework来自github。框架使用julienschmidt/httprouter作为它唯一的依赖。我有一个与示例中类似的主要方法:funcmain(){//registerroutesrouter:=httprouter.New()//defaultrouter.GET("/",controllers.Login.Perform(controllers.Login.Index))//loginrouter.GET("/login",controllers.Login.Perform(controllers.Login.Login)

google-app-engine - 第三方路由器和静态文件

我在GoogleAppEngine上使用第三方路由器(httprouter),并希望从根目录提供静态文件。由于AppEngine,我需要将第三方路由器附加到/上的DefaultServeMux:router:=httprouter.New()//Doesn'twork,duplicated"/".http.Handle("/",http.FileServer(http.Dir("public")))//NeededbecauseofAppEngine.http.Handle("/",router)问题是这会重复/模式并出现“multipleregistrationsfor/”的pani

google-app-engine - 去包装 httprouter 有记录器?

我有以下httprouter处理程序,但我想自定义以便我可以注入(inject)我自己的记录器函数。router:=httprouter.New()router.Handle("GET","/mysite",mylogger(handler1))mylogger就像:varlogger=log.New(os.Stdout,"[Log]",0)funcmylogger(fnfunc(whttp.ResponseWriter,r*http.Request,paramhttprouter.Params))func(whttp.ResponseWriter,r*http.Request,par

go - httprouter 设置自定义 NotFound

我正在使用https://github.com/julienschmidt/httprouter在我的Go项目中。我刚才问了这个问题,@icza解决了这个问题:httprouterconfiguringNotFound但是现在,开始一个新项目并使用非常相似的代码时,我似乎在控制台中遇到了错误。尝试配置自定义处理程序NotFound和MethodNotAllowed我正在使用:router.NotFound=customNotFoundrouter.MethodNotAllowed=customMethodNotAllowed产生:cannotusecustomNotFound(type

go - httprouter.GET 不工作

我有一个使用get方法提交数据的html表单。WebSearchEnteryourtags(Commaseparated)这是我的代码packagemainimport("net/http""log""html/template""fmt""github.com/julienschmidt/httprouter")funcmain(){router:=httprouter.New()router.GET("/",Search)router.GET("/search?key=:tags",GrabQuestions)log.Fatal(http.ListenAndServe(":8080

golang 模板不适用于 httprouter

我已经创建了嵌套模板,当我使用“net/http”和http.HandelFunc时它可以工作,但是,我决定继续使用“github.com/julienschmidt/httprouter”,因为我想要移动灵active现在我的模板不起作用,出现404错误。拜托,你能帮忙吗?目录结构//main.go/templates/templates/tstats/file.go.html此代码有效funcinit(){tpl=template.Must(template.ParseGlob("templates/*.go.html"))}http.HandleFunc("/tstats/",s

go - 使用 Alice 和 HttpRouter 的中间件

我似乎不知道如何正确地一起使用中间件和HttpRouter。我的代码是:typeappContextstruct{db*mgo.Database}funcmain(){c:=appContext{session.DB("db-name")}commonHandlers:=alice.New(context.ClearHandler,basicAuthHandler)router:=NewRouter()router.Post("/",commonHandlers.ThenFunc(c.final))http.ListenAndServe(":5000",router)}最终中间件是:f

go - 将上下文传递给子目录

我有一个用于应用程序上下文的Context结构。ConfigureRouter函数接收上下文作为参数并设置全局变量c以便同一文件中的中间件可以使用它。varc*core.ContextfuncConfigureRouter(ctx*core.Context,router*httprouter.Router){c=ctx//makecontextavailableinallmiddlewarerouter.POST("/v1/tokens/create",token.Create)//usinghttprouter}上面列出的一个路由调用了token.Create(来自作为子目录的tok

http - 为什么我不能在 Go 中编写来自单独包的 HTTP 响应?

我正在使用httprouter设置API端点,但我在让它正确输出响应时遇到了一些问题。我目前的主文件:packagemainimport("fmt""github.com/julienschmidt/httprouter""infrastructure/routing""log""net/http")functestPrint(whttp.ResponseWriter,r*http.Request,phttprouter.Params){fmt.Fprint(w,"Testoutput.")}funcmain(){fmt.Printf("\n**********************