memstore icon indicating copy to clipboard operation
memstore copied to clipboard

example not work

Open xiaolin310 opened this issue 1 year ago • 4 comments

Hey, I just run the example, but not work.

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/quasoft/memstore"
)

func main() {
	// Create a memory store, providing authentication and
	// encryption key for securecookie
	store := memstore.NewMemStore(
		[]byte("authkey123"),
		[]byte("enckey12341234567890123456789012"),
	)

	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		// Get session by name.
		session, err := store.Get(r, "session1")
		if err != nil {
			log.Printf("Error retrieving session: %v", err)
		}

		// The name should be 'foobar' if home page was visited before that and 'Guest' otherwise.
		user, ok := session.Values["username"]
		if !ok {
			user = "Guest"
		}
		fmt.Fprintf(w, "Hello %s", user)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Get session by name.
		session, err := store.Get(r, "session1")
		if err != nil {
			log.Printf("Error retrieving session: %v", err)
		}

		// Add values to the session object
		session.Values["username"] = "foobar"
		session.Values["email"] = "[email protected]"

		// Save values
		err = session.Save(r, w)
		if err != nil {
			log.Fatalf("Error saving session: %v", err)
		}
	})

	log.Printf("listening on http://%s/", "127.0.0.1:8000")
	log.Fatal(http.ListenAndServe("127.0.0.1:8000", nil))
}

>Output

curl http://127.0.0.1:8000/
>>>
curl http://127.0.0.1:8000/hello
>>>
Hello Guest%

What happened?

xiaolin310 avatar Apr 12 '24 06:04 xiaolin310

Don't have a suitable environment to test right now, but can you also try with a browser instead of separate curl commands?

The library uses gorilla/sessions to keep track of the session, so my first guess it that fails to work for separate curl commands.

quasoft avatar Apr 12 '24 09:04 quasoft

emm..It work indeed in the browser. I find the session info store in browser cookies. I want to use terminal command in my case, so I need a pure memory struct like map to act as the session store.

xiaolin310 avatar Apr 12 '24 11:04 xiaolin310

If you also want memory storage on the client side you can simulate it by saving the received cookies for the first curl command with something like: curl -c cookies.txt http://example.com

and then reading them from the file and feeding them to the second curl command: curl -b cookies.txt http://example.com/somepage

This is not tested at all, just giving a vague idea. Might not be suitable for your usecase at all.

quasoft avatar Apr 12 '24 12:04 quasoft

If you also want memory storage on the client side you can simulate it by saving the received cookies for the first curl command with something like: curl -c cookies.txt http://example.com

and then reading them from the file and feeding them to the second curl command: curl -b cookies.txt http://example.com/somepage

This is not tested at all, just giving a vague idea. Might not be suitable for your usecase at all.

ok, thank you, let me have a try.

xiaolin310 avatar Apr 12 '24 14:04 xiaolin310