github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/web/examples/arcchallenge.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  	"fmt"
    11  	"math/rand"
    12  	"time"
    13  
    14  	"github.com/chai2010/gopkg/web"
    15  )
    16  
    17  var form = `
    18  <form action="say" method="POST">
    19  	<input name="said">
    20  	<input type="submit">
    21  </form>
    22  `
    23  
    24  var users = map[string]string{}
    25  
    26  func main() {
    27  	rand.Seed(time.Now().UnixNano())
    28  	web.Config.CookieSecret = "7C19QRmwf3mHZ9CPAaPQ0hsWeufKd"
    29  	web.Get("/", func(ctx *web.Context) string {
    30  		ctx.Redirect(302, "/said")
    31  		return ""
    32  	})
    33  	web.Get("/said", func() string { return form })
    34  	web.Post("/say", func(ctx *web.Context) string {
    35  		uid := fmt.Sprintf("%d\n", rand.Int63())
    36  		ctx.SetSecureCookie("user", uid, 3600)
    37  		users[uid] = ctx.Params["said"]
    38  		return `<a href="/final">Click Here</a>`
    39  	})
    40  	web.Get("/final", func(ctx *web.Context) string {
    41  		uid, _ := ctx.GetSecureCookie("user")
    42  		return "You said " + users[uid]
    43  	})
    44  	web.Run("0.0.0.0:9999")
    45  }