func (s *Server) saveSession(w http.ResponseWriter, r *http.Request, ses *session.Session, domain, path string) (rr *http.Request, err error) {
	defer func() {
		rr = r.WithContext(context.WithValue(r.Context(), contextKeySession, ses))
	}()
	if ses == nil {
		ses = &session.Session{}
	}
	if ses.ID == "" {
		if ses, err = s.SessionService.CreateSession(&session.Options{
			Values: &ses.Values,
			MaxAge: &ses.MaxAge,
		}); err != nil {
			return
		}
	} else {
		if ses, err = s.SessionService.UpdateSession(ses.ID, &session.Options{
			Values: &ses.Values,
			MaxAge: &ses.MaxAge,
		}); err != nil {
			return
		}
	}
	if path == "" {
		path = "/"
	}
	cookie := &http.Cookie{
		Name:     s.SessionCookieName,
		Value:    ses.ID,
		Path:     path,
		Domain:   domain,
		MaxAge:   ses.MaxAge,
		Secure:   r.TLS != nil,
		HttpOnly: true,
	}
	if cookie.MaxAge > 0 {
		cookie.Expires = time.Now().Add(time.Duration(cookie.MaxAge) * time.Second)
	} else if cookie.MaxAge < 0 {
		cookie.Expires = time.Unix(1, 0)
	}
	http.SetCookie(w, cookie)
	return
}
saveSession saves session data using specified session service and
sets a session cookie using http.ResponseWriter.
The session is also cached in http.Request context.
package server
import (
	"context"
	"net/http"
	"time"
	"gopherpit.com/gopherpit/services/session"
)