Skip to content

Commit

Permalink
Return error on init
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirok5959 committed Jun 28, 2022
1 parent 7bf900f commit 348de02
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 42 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import (

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Init session
sess := session.New(w, r, "./tmp")
// Create new or load saved session
var sess *session.Session
var err error
sess, err = session.New(w, r, "./tmp")
if err != nil {
fmt.Printf("%s\n", err.Error())
}
defer sess.Close()

if r.URL.Path == "/" {
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import (

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Init session
sess := session.New(w, r, "./tmp")
// Create new or load saved session
var sess *session.Session
var err error
sess, err = session.New(w, r, "./tmp")
if err != nil {
fmt.Printf("%s\n", err.Error())
}
defer sess.Close()

if r.URL.Path == "/" {
Expand Down
42 changes: 29 additions & 13 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/fs"
"math/rand"
"net/http"
"os"
Expand All @@ -29,8 +30,9 @@ type Session struct {
hash string
}

// New to create new session
func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
// New to create new or load saved session,
// returns error if can't load saved session
func New(w http.ResponseWriter, r *http.Request, tmpdir string) (*Session, error) {
s := Session{
w: w,
r: r,
Expand All @@ -50,16 +52,30 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
// Load from file
s.hash = cookie.Value
fname := strings.Join([]string{s.tmpdir, s.hash}, string(os.PathSeparator))
if f, err := os.Open(fname); err == nil {
defer f.Close()
dec := json.NewDecoder(f)
if err := dec.Decode(&s.varlist); err == nil {
// Update file last modify time
if info, err := os.Stat(fname); err == nil {
if time.Since(info.ModTime()) > 30*time.Minute {
_ = os.Chtimes(fname, time.Now(), time.Now())
}
}
var f *os.File
f, err = os.Open(fname)
if err != nil {
return &s, err
}
defer f.Close()

dec := json.NewDecoder(f)
err = dec.Decode(&s.varlist)
if err != nil {
return &s, err
}

// Update file last modify time

var info fs.FileInfo
info, err = os.Stat(fname)
if err != nil {
return &s, err
}

if time.Since(info.ModTime()) > 30*time.Minute {
if err := os.Chtimes(fname, time.Now(), time.Now()); err != nil {
return &s, err
}
}
} else {
Expand All @@ -86,7 +102,7 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
})
}

return &s
return &s, nil
}

// Close to close session and save data to local file
Expand Down
50 changes: 25 additions & 25 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSessionBool(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetBool("some_bool")))); err != nil {
Expand All @@ -44,7 +44,7 @@ func TestSessionBool(t *testing.T) {
}
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/set" {
sess.SetBool("some_bool", true)
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestSessionBool(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false)))); err != nil {
Expand All @@ -100,7 +100,7 @@ func TestSessionBool(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetBool("some_bool")))); err != nil {
Expand All @@ -125,7 +125,7 @@ func TestSessionBool(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/del" {
sess.DelBool("some_bool")
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestSessionInt(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt("some_int")))); err != nil {
Expand All @@ -178,7 +178,7 @@ func TestSessionInt(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/set" {
sess.SetInt("some_int", 5)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestSessionInt(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
if _, err := w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0)))); err != nil {
Expand All @@ -234,7 +234,7 @@ func TestSessionInt(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt("some_int")))); err != nil {
Expand All @@ -259,7 +259,7 @@ func TestSessionInt(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/del" {
sess.DelInt("some_int")
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestSessionInt64(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt64("some_int64")))); err != nil {
Expand All @@ -312,7 +312,7 @@ func TestSessionInt64(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/set" {
sess.SetInt64("some_int64", 10)
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestSessionInt64(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
if _, err := w.Write([]byte(fmt.Sprintf("%d", sess.GetInt64("some_int64", 0)))); err != nil {
Expand All @@ -368,7 +368,7 @@ func TestSessionInt64(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt64("some_int64")))); err != nil {
Expand All @@ -393,7 +393,7 @@ func TestSessionInt64(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/del" {
sess.DelInt64("some_int64")
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestSessionString(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetString("some_str")))); err != nil {
Expand All @@ -446,7 +446,7 @@ func TestSessionString(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/set" {
sess.SetString("some_str", "test")
Expand Down Expand Up @@ -477,7 +477,7 @@ func TestSessionString(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
if _, err := w.Write([]byte(sess.GetString("some_str", ""))); err != nil {
Expand All @@ -502,7 +502,7 @@ func TestSessionString(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/isset" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetString("some_str")))); err != nil {
Expand All @@ -527,7 +527,7 @@ func TestSessionString(t *testing.T) {
request.Header.Set("Cookie", "session="+SessionId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/del" {
sess.DelString("some_str")
Expand Down Expand Up @@ -572,7 +572,7 @@ func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
}
recorder := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/set" {
sess.SetBool("some_bool", false)
Expand Down Expand Up @@ -610,7 +610,7 @@ func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
request.Header.Set("Cookie", "session="+sessId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
if _, err := w.Write([]byte(fmt.Sprintf(
Expand Down Expand Up @@ -650,7 +650,7 @@ func TestSessionDestroy(t *testing.T) {
}
recorder := httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/set" {
sess.SetInt("some_var", 1)
Expand Down Expand Up @@ -685,7 +685,7 @@ func TestSessionDestroy(t *testing.T) {
request.Header.Set("Cookie", "session="+sessId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0)))); err != nil {
Expand All @@ -710,7 +710,7 @@ func TestSessionDestroy(t *testing.T) {
request.Header.Set("Cookie", "session="+sessId)
recorder = httptest.NewRecorder()
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := New(w, r, "./../tmp")
sess, _ := New(w, r, "./../tmp")
defer sess.Close()
if r.URL.Path == "/get" {
sess.SetInt("some_var", 2)
Expand Down

0 comments on commit 348de02

Please sign in to comment.