ginsessionmongodb

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: MIT Imports: 4 Imported by: 0

README

gin-session-mongodb

Build Status Go Report Card codecov

Gin middleware for session management with MongoDB (MongoDB Go Driver) support

Installation

go get github.com/vincentinttsh/gin-session-mongodb

Example

package main

import (
	"context"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	ginsessionmongodb "github.com/vincentinttsh/gin-session-mongodb"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	r := gin.Default()
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	connect := options.Client().ApplyURI("localhost:27017")
	client, err := mongo.Connect(ctx, connect)
	if err != nil {
		panic(err)
	}
	coll := client.Database("test").Collection("sessions")
	store := ginsessionmongodb.NewStore(coll, 3600, true, []byte("secret"))
	r.Use(sessions.Sessions("mysession", store))

	r.GET("/hello", func(c *gin.Context) {
		session := sessions.Default(c)

		if session.Get("hello") != "world" {
			session.Set("hello", "world")
			session.Save()
		}

		c.JSON(200, gin.H{"hello": session.Get("hello")})
	})
	r.Run(":8000")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

type Store interface {
	sessions.Store
}

Store 連接 gin session

func NewStore

func NewStore(c *mongo.Collection, maxAge int, ensureTTL bool, k []byte) Store

NewStore 連接 mongodb

Jump to

Keyboard shortcuts

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