github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/web/examples/session.go (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ingore
     6  
     7  package main
     8  
     9  import (
    10  	"html/template"
    11  	"log"
    12  	"net/http"
    13  	"os"
    14  	"strings"
    15  
    16  	"github.com/chai2010/gopkg/web"
    17  )
    18  
    19  const page = `
    20  <html>
    21  <meta charset="utf-8"/>
    22  <body>
    23  {{if .Value}}.
    24  Hi {{.Value}}.
    25  <form method="post" action="/logout">
    26  <input type="submit" name="method" value="logout" />
    27  </form>
    28  You will logout after 10 seconds. Then try to reload.
    29  {{else}}
    30  <form method="post" action="/login">
    31  <label for="name">Name:</label>
    32  <input type="text" id="name" name="name" value="" />
    33  <input type="submit" name="method" value="login" />
    34  </form>
    35  {{end}}
    36  </body>
    37  </html>
    38  `
    39  
    40  var tmpl = template.Must(template.New("x").Parse(page))
    41  
    42  func main() {
    43  	logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
    44  	manager := web.NewSessionManager(logger)
    45  	manager.OnStart(func(session *web.Session) {
    46  		println("started new session:", session.Id)
    47  	})
    48  	manager.OnTouch(func(session *web.Session) {
    49  		println("touch session:", session.Id)
    50  	})
    51  	manager.OnEnd(func(session *web.Session) {
    52  		println("abandon:", session.Id)
    53  	})
    54  	manager.SetTimeout(10)
    55  
    56  	http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    57  		session := manager.GetSession(w, req)
    58  		w.Header().Set("Pragma", "no-cache")
    59  		w.Header().Set("Content-Type", "text/html; charset=utf-8")
    60  		tmpl.Execute(w, session)
    61  	}))
    62  	http.Handle("/login", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    63  		name := strings.Trim(req.FormValue("name"), " ")
    64  		if name != "" {
    65  			logger.Printf("User \"%s\" login", name)
    66  
    67  			// XXX: set user own object.
    68  			manager.GetSession(w, req).Value = name
    69  		}
    70  		http.Redirect(w, req, "/", http.StatusFound)
    71  	}))
    72  	http.Handle("/logout", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    73  		if manager.GetSession(w, req).Value != nil {
    74  			// XXX: get user own object.
    75  			name := manager.GetSession(w, req).Value.(string)
    76  			logger.Printf("User \"%s\" logout", name)
    77  			manager.GetSession(w, req).Abandon()
    78  		}
    79  		http.Redirect(w, req, "/", http.StatusFound)
    80  	}))
    81  	err := http.ListenAndServe(":6061", nil)
    82  	if err != nil {
    83  		log.Fatal("ListenAndServe:", err)
    84  	}
    85  }