<commit_before>// Copyright 2015 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package web

import (
	"net/http"
	"sync"

	"github.com/issue9/logs"
	"github.com/issue9/session"
	"github.com/issue9/session/providers"
	"github.com/issue9/session/stores"
)

var (
	sessionMgr *session.Manager
	sessions   = map[*http.Request]*session.Session{}
	sessionsMu sync.Mutex
)

// 初始化session
// vars接受以下四个参数：idname,lifetime,type,saveDir
func initSession() error {
	c := cfg.Session
	prv := providers.NewCookie(c.Lifetime, c.IDName, "/", "", true)

	switch c.Type {
	case "", "memory":
		sessionMgr = session.New(stores.NewMemory(c.Lifetime), prv)
	case "file":
		f, err := stores.NewFile(c.SaveDir, c.Lifetime, logs.ERROR())
		if err != nil {
			panic(err)
		}
		sessionMgr = session.New(f, prv)
	default:
		panic("initSession:无效的session.type值")
	}

	return nil
}

// 获取Session实例。
func Session(w http.ResponseWriter, req *http.Request) *session.Session {
	sessionsMu.Lock()
	defer sessionsMu.Unlock()

	if sess, found := sessions[req]; found {
		return sess
	}

	sess, err := sessionMgr.Start(w, req)
	if err != nil {
		logs.Error(err)
		return nil
	}
<commit_msg>去掉initSession无用的返回值<commit_after>// Copyright 2015 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package web

import (
	"net/http"
	"sync"

	"github.com/issue9/logs"
	"github.com/issue9/session"
	"github.com/issue9/session/providers"
	"github.com/issue9/session/stores"
)

var (
	sessionMgr *session.Manager
	sessions   = map[*http.Request]*session.Session{}
	sessionsMu sync.Mutex
)

// 初始化session
// vars接受以下四个参数：idname,lifetime,type,saveDir
func initSession() {
	c := cfg.Session
	prv := providers.NewCookie(c.Lifetime, c.IDName, "/", "", true)

	switch c.Type {
	case "", "memory":
		sessionMgr = session.New(stores.NewMemory(c.Lifetime), prv)
	case "file":
		f, err := stores.NewFile(c.SaveDir, c.Lifetime, logs.ERROR())
		if err != nil {
			panic(err)
		}
		sessionMgr = session.New(f, prv)
	default:
		panic("initSession:无效的session.type值")
	}
}

// 获取Session实例。
func Session(w http.ResponseWriter, req *http.Request) *session.Session {
	sessionsMu.Lock()
	defer sessionsMu.Unlock()

	if sess, found := sessions[req]; found {
		return sess
	}

	sess, err := sessionMgr.Start(w, req)
	if err != nil {
		logs.Error(err)
		return nil
	}

{'fraction_non_alphanumeric': 0.14465664364079642, 'fraction_numerical': 0.00650142218610321, 'mean_word_length': 3.9437751004016066, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1239332', 'n_tokens_mistral': 962, 'n_tokens_neox': 939, 'n_words': 209}