gorm

package module
v0.0.0-...-7d5f87d Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2019 License: MIT Imports: 9 Imported by: 0

README

GORM store for Session

Build Codecov ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v github.com/go-session/gorm
Create file server.go
package main

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

	gormstore "github.com/go-session/gorm"
	"github.com/go-session/session"

	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

func main() {
	store := gormstore.MustStore(gormstore.Config{}, "sqlite3", "session.db")
	defer store.Close()

	session.InitManager(
		session.SetStore(store),
	)

	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) 2019 Lyric

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustStore

func MustStore(cfg Config, dialect string, args ...interface{}) session.ManagerStore

MustStore Create an instance of a gorm store(Throw a panic if an error occurs)

func MustStoreWithDB

func MustStoreWithDB(db *gorm.DB, tableName string, gcInterval int) session.ManagerStore

MustStoreWithDB Create an instance of a gorm store(Throw a panic if an error occurs)

func NewStore

func NewStore(cfg Config, dialect string, args ...interface{}) (session.ManagerStore, error)

NewStore Create an instance of a gorm store

func NewStoreWithDB

func NewStoreWithDB(db *gorm.DB, tableName string, gcInterval int) (session.ManagerStore, error)

NewStoreWithDB Create an instance of a gorm store, tableName Specify the stored table name (default session), gcInterval Time interval for executing GC (in seconds, default 600)

Types

type Config

type Config struct {
	Debug           bool          // start debug mode
	ConnMaxLifetime time.Duration // sets the maximum amount of time a connection may be reused
	MaxOpenConns    int           // sets the maximum number of open connections to the database
	MaxIdleConns    int           // sets the maximum number of connections in the idle connection pool
	TableName       string        // Specify the stored table name (default session)
	GCInterval      int           // Time interval for executing GC (in seconds, default 600)
}

Config configuration parameter

type SessionItem

type SessionItem struct {
	ID        string    `gorm:"column:id;size:255;primary_key;"`
	Value     string    `gorm:"column:value;size:2048;"`
	CreatedAt time.Time `gorm:"column:created_at;"`
	ExpiredAt time.Time `gorm:"column:expired_at;"`
}

SessionItem Data items stored in mysql

Jump to

Keyboard shortcuts

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