Categorygithub.com/eucalytus/session
modulepackage
0.0.0-20210716083308-dc9c1597e215
Repository: https://github.com/eucalytus/session.git
Documentation: pkg.go.dev

# README

session

GitHub Language Go Report Card Build Status CircleCI Codecov

How to use it

package main

import (
	"log"
	"net/http"

	"github.com/eucalytus/session"
)

func main() {
	manager := session.NewManager(session.Options{
		MaxInactiveInterval: 1800, MaxAge: 84000, HttpOnly: true,
	},
		session.CreateMemSession,
		//listen session event
		func(s session.Session, event int) {
			if event == session.Created {
				log.Printf("new session is created, sessionId=%s\n", s.GetMaskedSessionId())
			} else if event == session.Destroyed {
				log.Printf("session is destroyed, sessionId=%s\n", s.GetMaskedSessionId())
			} else {
				log.Printf("session is updated, sessionId=%s\n", s.GetMaskedSessionId())
			}
		},
	)

	//private resource
	http.HandleFunc("/private", func(response http.ResponseWriter, request *http.Request) {
		s := manager.GetSession(request)
		if s != nil {
			if _, found := s.Get("key"); found {
				response.Write([]byte("OK"))
				return
			}
		}
		response.WriteHeader(http.StatusUnauthorized)
		response.Write([]byte("StatusUnauthorized"))
	})

	//login
	http.HandleFunc("/login", func(response http.ResponseWriter, request *http.Request) {
		s := manager.GetSession(request)
		if s == nil {
			temp, err := manager.CreateSession(request, response)
			if err != nil {
				log.Printf("create session failed: %v\n", err)
			}
			s = temp
		}
		s.Set("key", "login")
		response.Write([]byte("OK"))
	})

	//logout
	http.HandleFunc("/logout", func(response http.ResponseWriter, request *http.Request) {
		s := manager.GetSession(request)
		if s != nil {
			s.Set("key", nil)
			s.Invalidate()
		}
		response.Write([]byte("OK"))
	})

	http.ListenAndServe("0.0.0.0:8000", nil)
}

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
GetSession get the session from gin context.
No description provided by the author
NewManager init new session manager and start gc.
No description provided by the author
UseSession gin session middleware.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Manager manage the created sessions.
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author