@@ -41,5 +41,8 @@ func main() {
}
})
+ // Delete expired session files
+ session.Clean("./tmp")
+
http.ListenAndServe(":8080", nil)
@@ -0,0 +1,27 @@
+package session
+import (
+ "io/ioutil"
+ "os"
+ "time"
+)
+func Clean(tmpdir string) error {
+ files, err := ioutil.ReadDir(tmpdir)
+ if err != nil {
+ return err
+ }
+ now := time.Now()
+ exp := 7 * 24 * time.Hour
+ for _, file := range files {
+ if len(file.Name()) == 40 {
+ if diff := now.Sub(file.ModTime()); diff > exp {
+ err = os.Remove(tmpdir + string(os.PathSeparator) + file.Name())
+ return nil
+}
@@ -52,7 +52,7 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *session {
Name: "session",
Value: sess.i,
Path: "/",
- Expires: time.Now().Add(365 * 24 * time.Hour),
+ Expires: time.Now().Add(7 * 24 * time.Hour),
HttpOnly: true,