I am using a basic wrapper in a web application like this:
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
const (
PORT = "8083"
)
func navbarWrapper(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "header")
h(w, r) // call original function
fmt.Fprintln(w, "footer")
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/cookie_write", navbarWrapper(cookie_write))
hostname, _ := os.Hostname()
log.Printf("Listening on port %s https://%s:%s/", PORT, hostname, PORT)
http.ListenAndServe(":"+PORT, r)
}
I would like to set a cookie, but it is not being sent to the browser, but is also not providing an error
var (
key = []byte("16bytestufffffff")
store = sessions.NewCookieStore(key)
)
func cookie_write(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
session.Values["authenticated"] = true
session.Values["stuff"] = "important info"
if err := session.Save(r, w); err != nil {
fmt.Println(err)
fmt.Println("error")
} else {
fmt.Println("worked?")
}
}
If I remove the wrapper, then it works fine and I can see the cookie being generated in the browser:
r.HandleFunc("/cookie_write", cookie_write)
I know I must not be saving the session correctly, but I can't figure out how to do it.