gorilla

Golang http mux change handler function

别等时光非礼了梦想. 提交于 2019-12-08 17:14:02
问题 I am fairly new to Go and have not been able to find any information on this, maybe it is just not possible at this time. I am trying to delete or replace a mux route (using http.NewServeMux, or gorilla's mux.Router). My end goal is to be able to enable/disable a route or set of routes without having to restart the program. I can probably accomplish this on a handler to handler basis and just return 404 if that feature is "disabled", but I would rather find a more general way to do this since

Go: serve CSS files with gorilla mux

杀马特。学长 韩版系。学妹 提交于 2019-12-08 03:52:57
问题 I have this directory structure and I'm using Gorilla mux: Directory structure twitter layout stylesheets log.css log.html twitter.go Following the advice here: http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html I did this: var router = mux.NewRouter() func ServeStatic(router *mux.Router, staticDirectory string) { staticPaths := map[string]string{ "styles": staticDirectory + "stylesheets", } for pathName, pathValue := range staticPaths { pathPrefix := "/" + pathName + "/"

Golang RESTful API load testing causing too many database connections

青春壹個敷衍的年華 提交于 2019-12-07 15:26:21
问题 I think I am having serious issue managing database connection pool in Golang. I built an RESTful API using Gorilla web toolkit which works great when only few requests are being sent over to the server. But now I started performing load testing using loader.io site. I apologize for the long post, but I wanted to give you the full picture. Before going further, here are some info on the server running the API and MySQL: Dedicated Hosting Linux 8GB RAM Go version 1.1.1 Database connectivity

Golang logging http responses (in addition to requests)

谁都会走 提交于 2019-12-07 01:55:29
问题 I am using Go and the Gorilla web toolkit's mux and handler packages to build a complex application, part of which requires a http server. Gorilla's mux and handler packages work wonderfully and I am able to successfully get the http server up and running and it has been quite simple to log requests. However, I am unable to determine how I may log responses. Ideally, I would like a mechanism, similar to Gorilla's LoggingHandler, that "wraps" the logging mechanism easily. Is there a Golang

Websockets over WebAssembly generated by golang?

不打扰是莪最后的温柔 提交于 2019-12-06 11:02:45
Is it possible to write a Websocket client in wasm over go? I have tried using gorilla/websocket , but no success: func main() { ws := func(this js.Value, inputs []js.Value) interface{} { go func() { wsDial, r, err := websocket.DefaultDialer.Dial("ws://localhost:3000/ws", nil) fmt.Println(wsDial, r, err) }() return nil } js.Global().Set("ws", js.FuncOf(ws)) select {} } I get the following error when calling ws() : dial tcp: Protocol not available I have solved it by retrieving the WebSocket object from the global JavaScript object, which in my case is window because I am running this in a

golang go-endpoints Session using gorilla-toolkit

一世执手 提交于 2019-12-06 10:23:27
问题 I'm trying to implement Session handling and combine it with the go-endpoints package ! The package that i use to handle the session is Gorilla Sessions (github.com/gorilla/sessions), i would like some help.. I'm able to store a cookie to the client .. and when i call the endpoints is can see that the cookie is sent to the server. The problem while i try to get the Session values from the Session storage while the api is called, i cant get threw to the cookie .. it seams that the endpoints

Running two web server at the same time in one go programm

我的未来我决定 提交于 2019-12-06 08:09:56
问题 In a go program, I want to run two web servers at the same time, obviously they will be serving on two different ports (and ip addresses if necessary), the problem is with the call to http.handle , when I try to register handler for '/' for the second server, it panics and says there is already a handler associated with '/', I guess I need to create a mux in addition to the DefaultServeMux and I tried to do it using the gorillaMux but couldn't figure it out, Is there something fundamentally

How to use gorilla mux with http.TimeoutHandler

℡╲_俬逩灬. 提交于 2019-12-06 05:31:19
问题 In an HTTP server written in go, I use gorilla/mux for routing, I want to use http.TimeoutHandler (and/or other "middleware") but I can't understand where I can fit them. To make it clear: I create a new Router by gorillaMux := mux.NewRouter() add my routes by calls like gorillaMux.HandleFunc("/", rootHandler) I create the server by server := &http.Server{Addr:":1234"} and server.ListenAndServe() Where can I insert the http.TimeoutHandler or any other middleware for that matter? 回答1: Here is

How to call a route by its name from inside a handler?

折月煮酒 提交于 2019-12-06 04:08:12
How do I properly refer to route names from inside handlers? Should mux.NewRouter() be assigned globally instead of standing inside a function? func AnotherHandler(writer http.ResponseWriter, req *http.Request) { url, _ := r.Get("home") // I suppose this 'r' should refer to the router http.Redirect(writer, req, url, 302) } func main() { r := mux.NewRouter() r.HandleFunc("/", HomeHandler).Name("home") r.HandleFunc("/nothome/", AnotherHandler).Name("another") http.Handle("/", r) http.ListenAndServe(":8000", nil) } You have the method mux.CurrentRoute() that returns the route for a given request.

What is the advantage of using Gorilla sessions custom backend?

余生长醉 提交于 2019-12-05 18:38:14
问题 I want to use Redis for session management. But I can't figure out what the advantage is of using Redis as a custom back-end for Gorilla sessions package over using it directly? link to the Gorilla session package: http://www.gorillatoolkit.org/pkg/sessions 回答1: Gorilla sessions provides a means to wire up a storage system for session management provided you adhere to the interface provided. Currently, they give you two stores out of the box. One is a FilesystemStore that adheres to the