Gorilla mux optional query values

I would just register your handler twice. router.Path(“/articles/{id:[0-9]+}”). Queries(“key”, “{[0-9]*?}”). HandlerFunc(YourHandler). Name(“YourHandler”) router.Path(“/articles/{id:[0-9]+}”).HandlerFunc(YourHandler) Here is a working program to demonstrate. Notice that I am using r.FormValue to get the query parameter. Note: make sure you have an up to date version go get -u github.com/gorilla/mux since a bug of query params not getting added the … Read more

Gorilla mux custom middleware

Just create a wrapper, it’s rather easy in Go: func HomeHandler(response http.ResponseWriter, request *http.Request) { fmt.Fprintf(response, “Hello home”) } func Middleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println(“middleware”, r.URL) h.ServeHTTP(w, r) }) } func main() { r := mux.NewRouter() r.HandleFunc(“https://stackoverflow.com/”, HomeHandler) http.Handle(“https://stackoverflow.com/”, Middleware(r)) }

Golang Gorilla mux with http.FileServer returning 404

I posted this on golang-nuts discussion group and got this solution from Toni Cárdenas … The standard net/http ServeMux (which is the standard handler you are using when you use http.Handle) and the mux Router have different ways of matching an address. See the differences between http://golang.org/pkg/net/http/#ServeMux and http://godoc.org/github.com/gorilla/mux. So basically, http.Handle(‘/images/’, …) matches ‘/images/whatever’, … Read more

How to add Authorization Header to Angular http request?

Regarding the best way of handling Authentication headers in Angular > 4 it’s best to use Http Interceptors for adding them to each request, and afterwards using Guards for protecting your routes. Here’s a full example of an AuthInterceptor that I’m using in my app: auth.interceptor.ts import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from ‘@angular/common/http’; … Read more

Making golang Gorilla CORS handler work

Please read the link Markus suggested, and also about what triggers CORS pre-flight requests. Pre-flight requests: You may have a content type like JSON, or some other custom header that’s triggering a pre-flight request, which your server may not be handling. Try adding this one, if you’re using the ever-common AJAX in your front-end: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With … Read more

Unit testing for functions that use gorilla/mux URL parameters

gorilla/mux provides the SetURLVars function for testing purposes, which you can use to inject your mock vars. func TestGetRequest(t *testing.T) { r, _ := http.NewRequest(“GET”, “/test/abcd”, nil) w := httptest.NewRecorder() //Hack to try to fake gorilla/mux vars vars := map[string]string{ “mystring”: “abcd”, } // CHANGE THIS LINE!!! r = mux.SetURLVars(r, vars) GetRequest(w, r) assert.Equal(t, http.StatusOK, … Read more

How do I pass arguments to my handler

Welcome to Go. It is acceptable to have global variables and specially database objects. However, there are few ways to workaround that if you prefer not to, for example you can create a struct and define your showHandler on it. type Users struct { db *gorm.DB } func (users *Users) showHandler(w http.ResponseWriter, r *http.Request) { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)