gorilla

Why is GorillaMux not allowing me to pass in a URL as a parameter?

早过忘川 提交于 2019-12-12 06:17:42
问题 I'm building a URL shortener API and I'm trying to pass a URL into a function using GorillaMux. The route handler is like so: router.HandleFunc("/new/{url}", createURL) The only thing is, if I pass in: https://www.google.com (as in localhost:8080/new/https://www.google.com) then it responds with 404 page not found and the URL is changed to https:/www.google.com. I've tried adding a regexp pattern in with the {url} bit like so: {url:[a-zA-Z0-9/]+} but that didn't seem to work and seems a bit

How to use Gorilla Sessions in Golang in different packages

喜夏-厌秋 提交于 2019-12-12 01:16:47
问题 I have main package (main.go): package main import ( "github.com/gorilla/sessions" ... ) func main() { var store = sessions.NewCookieStore([]byte("secret")) http.HandleFunc("/", routes.Index) http.ListenAndServe(":8080", nil) ... And I have another package (index.go): package routes import ( "github.com/gorilla/sessions" .. ) func Index(res http.ResponseWriter, req *http.Request) { session, _ := store.Get(req, "session-name") ... How can I get session value from another package? Or should I

How to send back an image in Go from the public folder?

China☆狼群 提交于 2019-12-11 15:38:19
问题 I'm new to go if it's an obvious question then sorry but I didn't find anything so I'm posting here. This is a two-part question actually. 1) So I have this web folder in my project dir. And it is public and I have a middleware function which is checking for the JWT. I'm using this logic to avoid JWT auth but it's now working. I'm getting unauthorized . // List of endpoints that don't require auth by an access token notAuth := []string{"/", "/refreshtokens", "/students/signup", "/students

Golang & Gorilla Sessions - Cache Prevents Logout Functionality

◇◆丶佛笑我妖孽 提交于 2019-12-11 09:48:38
问题 I've built an application that uses the Go Gorilla sessions package. Everything seems fine, except when on logout I implement func logout(w http.ResponseWriter, r *http.Request) { session, _ := store.Get(r, "authsesh") session.Values["access"] = "denied" session.Save(r, w) http.Redirect(w, r, "/", 302) return } Because the page requiring authentication is cached by the browser, it can still be accessed after logout. How can I get around that? Is there a way to prevent the browser from caching

How can I set up the logging context from middleware?

核能气质少年 提交于 2019-12-11 06:37:45
问题 I want to populate the logging context by items in the request, for example: r.Header.Get("X-Request-Id") . I assumed I could override the Log type in the handler from middleware. Though it doesn't seem to work and I am not sure why! package main import ( "fmt" "net/http" "os" "github.com/apex/log" "github.com/gorilla/mux" ) // Assumption: handler is the shared state between the functions type handler struct{ Log *log.Entry } // New creates a handler for this application to co-ordinate shared

Pass a reference to a Redis instance to a Gorilla/Mux Handler

断了今生、忘了曾经 提交于 2019-12-11 05:54:39
问题 I'm trying to get my hands dirty while playing with some Gorilla/Mux and Go-Redis but I'm facing a little implementation problem here. Essentially I have a project structured like the following: Where redismanager.go handles the initialization of a Redis Client: package redismanager import ( "fmt" "github.com/go-redis/redis" ) func InitRedisClient() redis.Client { client := redis.NewClient(&redis.Options{ Addr : "localhost:6379", Password: "", DB : 0, //default }) pong, err := client.Ping()

Golang net/http and Gorilla: run code before handler

痴心易碎 提交于 2019-12-11 04:51:52
问题 Is it possible using the net/http package and/or any of the gorilla libraries to make some code execute on EVERY URL before going to the handler? For example, to check if a connection is coming from a black listed IP address? 回答1: Create a handler that invokes another handler after checking the IP address: type checker struct { h http.Handler } func (c checker) ServeHTTP(w http.ResponseWriter, r *http.Request) { if blackListed(r.RemoteAddr) { http.Error(w, "not authorized", http

Can I use gorilla schema with an sql.NullString?

∥☆過路亽.° 提交于 2019-12-11 02:53:48
问题 I am using gorilla schema to populate a struct based on a user's form submission. My struct contains sql.NullString and I am currently getting the following error: schema: converter not found for sql.NullString How can I use sql.NullString in a struct that I want to populate with gorilla schema? 回答1: I created a gist ( https://gist.github.com/carbocation/51b55297702c7d30d3ef ) that shows one way to approach this. You need to create a schema.Converter for each of the four types: sql.NullString

Go log thread id in Gorilla Handler

帅比萌擦擦* 提交于 2019-12-10 23:36:32
问题 How do we get the thread id or any other unique Id of the http request being handled by the handler in logging inside Gorilla Handlers ? In Java, when Tomcat or other container handles multiple http requests, thread id helps to track all the log messages for respective http request handling. What is the equivalent in Go ? Given a Rest API developed using Gorilla library, how do I track all log statements of specific http request inside a handler processing? 回答1: The gorilla/handlers library

Why does App Engine Flexible Enviroment not allow WebSockets and HTTP/2 traffic?

♀尐吖头ヾ 提交于 2019-12-10 12:14:45
问题 I'm more interested in WebSocket, but after reading documentation here, I was surprised when I read that: The following features are not supported by App Engine flexible environment: HTTP/2 traffic to the backend service, Websockets , HTTP requests that directly access instances I was planning to build an app with gorilla-websocket, but now that I've read this, I don't know how I could accomplish it. I understand WebSocket is not get supported in App Engine Standard Environment, but why not