gorilla

How to have an optional query in GET request using Gorilla Mux?

☆樱花仙子☆ 提交于 2019-12-21 05:02:35
问题 I would like to have some of my query parameters be optional. As for now, I have r.HandleFunc("/user", userByValueHandler). Queries( "username", "{username}", "email", "{email}", ). Methods("GET") But in this case "username" AND "email" needs to be present in the request. I want to have more flexible choice: have 2 of them OR have just one of them (but not zero parameters). Thanks! 回答1: So I found the solution to re-write my logic as: r.HandleFunc("/user", UserByValueHandler).Methods("GET")

Can't use go tool pprof with an existing server

百般思念 提交于 2019-12-21 03:37:16
问题 I have an existing http server which I would like to profile. I have included _ "net/http/pprof" to my imports, and I already have http server running: router := createRouter() server := &http.Server { Addr: ":8080", Handler: router, ReadTimeout: 15*time.Second, WriteTimeout: 15*time.Second, // MaxHeaderBytes: 4096, } log.Fatal(server.ListenAndServe()) When I'm trying to access http://localhost:8080/debug/pprof/ I get 404 page not found . That's what I get when using go tool pprof on a local

Gorilla WebSocket disconnects after a minute

寵の児 提交于 2019-12-20 12:29:26
问题 I'm using Go (Golang) 1.4.2 with Gorilla WebSockets behind an nginx 1.4.6 reverse proxy. My WebSockets are disconnecting after about a minute of having the page open. Same behavior occurs on Chrome and Firefox. At first, I had problems connecting the server and client with WebSockets. Then, I read that I needed to tweak my nginx configuration. This is what I have. server { listen 80; server_name example.com; proxy_pass_header Server; location / { proxy_set_header Host $host; proxy_set_header

How do I pass arguments to my handler

风格不统一 提交于 2019-12-20 09:10:53
问题 I am trying to pass my database object along to my handlers, instead of having a global object. But I don't know if this is possible, I'm using Gorilla Mux package, and I can see that it takes a closure as a second param. // https://github.com/gorilla/mux/blob/master/mux.go#L174 // HandleFunc registers a new route with a matcher for the URL path. // See Route.Path() and Route.HandlerFunc(). func (r *Router) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) *Route { return r

Gorilla mux custom middleware

元气小坏坏 提交于 2019-12-17 21:53:35
问题 I am using gorilla mux for manage routing. What I am missing is to integrate a middleware between every request. For example package main import ( "fmt" "github.com/gorilla/mux" "log" "net/http" "strconv" ) func HomeHandler(response http.ResponseWriter, request *http.Request) { fmt.Fprintf(response, "Hello home") } func main() { port := 3000 portstring := strconv.Itoa(port) r := mux.NewRouter() r.HandleFunc("/", HomeHandler) http.Handle("/", r) log.Print("Listening on port " + portstring + "

Gorilla mux, best way to 'catch' response codes

♀尐吖头ヾ 提交于 2019-12-14 02:08:59
问题 I'm using Gorilla mux for all my routing. Now my app is working fine, I want to find a way to log all my response codes to -for example- statds. I have found this package: https://godoc.org/github.com/gorilla/handlers#LoggingHandler Which allows me to output all responses into apache format. Although this is nice, it's not 100% what I want. I just want to extract the response statusses and send them to statds. Now what's the best/easiest way to achieve this? package main import ( "log" "net

Gorilla Sessions - How to Automatically Update Cookie Expiration on Request?

可紊 提交于 2019-12-13 14:42:56
问题 I know many other languages and web frameworks will automatically update a cookie's expiration time to the session timeout every time a session is accessed via the backend (or some action like that). I don't believe Gorilla provides this utility. I am consider just writing some request middleware that, if it detects a valid session, will extend the cookie lifetime but I am wondering if there is a better method of doing this. What are the best practices for updating cookie expiration,

config CORS in Gorilla Mux: 403 error on POST request

独自空忆成欢 提交于 2019-12-12 20:42:13
问题 I have an API, currently am trying to consume one of its endpoints. The endpoint is for POST requests, the endpoint is working as expected. The API is running in the cloud, I tested it with curl and it was perfect, then from my react app I was trying to consume it but I get 403 status code . Watching in the console of the browser I see that I get that error on a OPTIONS request, and the POST never get done. Here is a screenshot of the result displayed in the console: Then, I made a simple

Gorilla sessions not working for CORS from client

匆匆过客 提交于 2019-12-12 13:28:06
问题 I have set up a Go rest api. And on login I do this: session, _ := store.New(r, sessionId) session.Options.MaxAge = 12 * 3600 err := session.Save(r, w) //treat error and for checking the session i have smth like this: session, err := store.Get(r, sessionId) //treat error if session.IsNew { http.Error(w, "Unauthorized session.", http.StatusUnauthorized) return } If I do the requests from postman it works fine, but when I do them from my client I get 401. Has any of you experienced something

Websockets over WebAssembly generated by golang?

半腔热情 提交于 2019-12-12 09:59:47
问题 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 回答1: I have solved it by retrieving the