mongo

package module
v2.0.0-...-5fc206e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2019 License: MIT Imports: 8 Imported by: 0

README

mongo

A mongo-based session store for http://gopkg.in/session.v2

ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v gopkg.in/nodely/mongo-session.v2
Create file server.go
package main

import (
	"context"
	"fmt"
	"net/http"

	"gopkg.in/nodely/mongo-session.v2"
	"gopkg.in/session.v3"
)

func main() {
	session.InitManager(
		session.SetCookieName("session_id"),
		session.SetSign([]byte("sign")),
		session.SetStore(mongo.NewMongoStore(&mongo.Options{
            		Connection:"mongodb://localhost:27010/session-storage",
		})),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		store.Set("foo", "bar")
		err = store.Save()
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		http.Redirect(w, r, "/foo", 302)
	})

	http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		foo, ok := store.Get("foo")
		if ok {
			fmt.Fprintf(w, "foo:%s", foo)
			return
		}
		fmt.Fprint(w, "does not exist")
	})

	http.ListenAndServe(":8080", nil)
}
Build and run
$ go build server.go
$ ./server
Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Nodely

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMongoStore

func NewMongoStore(opt *Options) session.ManagerStore

NewMongoStore Create an instance of a mongo store

Types

type Options

type Options struct {
	// connection string
	Connection string

	// collection name
	Collection string

	// database name
	DB string
}

Options Mongo parameter options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL