當前位置: 首頁>>代碼示例>>Golang>>正文


Golang sessions.Sessions函數代碼示例

本文整理匯總了Golang中github.com/martini-contrib/sessions.Sessions函數的典型用法代碼示例。如果您正苦於以下問題:Golang Sessions函數的具體用法?Golang Sessions怎麽用?Golang Sessions使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Sessions函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {
	ga.AdminPort = 8000
	ga.MysqlDsn = "root:[email protected](192.168.1.15:3306)/galaxy2"
	ga.TemplatePath = "/Users/sunlei/go/src/galaxy/templates"
	ga.DbName = "default"
	go ga.Run()
	m := martini.Classic()
	store := sessions.NewCookieStore([]byte("secret123"))
	m.Use(render.Renderer(render.Options{
		Directory:  "/Users/sunlei/go/src/github.com/go-galaxy/galaxy/example/template", // Specify what path to load the templates from.
		Extensions: []string{".tmpl", ".html"},                                          // Specify extensions to load for templates.
		Layout:     "",
	}))
	m.Use(sessions.Sessions("my_session", store))
	m.Use(func(res http.ResponseWriter, req *http.Request, session sessions.Session) {
		if strings.Contains(req.URL.String(), "login") {
			return
		}
		v := session.Get("login")
		if v == nil {
			http.Redirect(res, req, "login", http.StatusFound)
		}
	})
	m.Use(sessions.Sessions("my_session", store))
	m.Get("/login", Login)
	m.Get("/", Index)
	m.Post("/login", Login)
	m.Get("/logout", LogOut)
	m.Get("/test", Test)
	m.RunOnAddr(":3000")
}
開發者ID:go-galaxy,項目名稱:galaxy,代碼行數:31,代碼來源:example.go

示例2: InitSession

//InitSession - initializes authentication middleware for controllers
func InitSession(m *martini.ClassicMartini, rc redisCreds) {
	m.Use(render.Renderer())

	if rediStore, err := sessions.NewRediStore(10, "tcp", rc.Uri(), rc.Pass(), []byte(sessionSecret)); err == nil {
		m.Use(sessions.Sessions(sessionName, rediStore))
	}
}
開發者ID:malston,項目名稱:pezauth,代碼行數:8,代碼來源:auth.go

示例3: main

func main() {
	m := martini.Classic()
	m.Map(SetupDB())
	m.Use(martini.Logger())
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
	}))

	store := sessions.NewCookieStore([]byte(CookieSecret))
	m.Use(sessions.Sessions("bunkaisession", store))
	log.Println("env is", martini.Env)
	m.Map(martini.Env)

	m.Group("/", func(m martini.Router) {
		m.Get("", Home)
	}, AssetMap)

	m.Group("/api", func(m martini.Router) {
		m.Post("/login", PostLogin)
		m.Post("/users", UserCreate)

		m.Group("/sentences", func(m martini.Router) {
			m.Post("", SentenceCreate)
			m.Get("", SentenceList)
			m.Delete("/:id", SentenceDelete)
		}, RequireLogin)

		m.Group("/users", func(m martini.Router) {
			m.Post("/logout", Logout)
			m.Get("/me", UserGet)
		}, RequireLogin)
	})

	m.Run()
}
開發者ID:ukitazume,項目名稱:bunkai,代碼行數:35,代碼來源:server.go

示例4: main

func main() {
	m := martini.Classic()
	dbMap := initDb()
	m.Map(dbMap)
	store := sessions.NewCookieStore([]byte("nonotestetstsst"))
	m.Use(sessions.Sessions("gogonki", store))
	m.Use(render.Renderer())
	m.Get("/", Index)

	m.Get("/coords", Coords)
	m.Get("/move/:x/:y", Move) // ToDo: make this post request
	m.Get("/state", GetState)
	m.Get("/profile", ShowProfile)
	m.Get("/login", LoginForm)
	m.Post("/login", Login)
	m.Get("/logout", Logout)
	m.Post("/create", binding.Bind(User{}), CreateUser)
	m.Get("/signup", SignupForm)
	m.Post("/signup", Signup)
	m.Post("/create-room", CreateRoom)
	m.Post("/list-rooms", ListRooms)

	//m.Use(Auth)
	m.Run()
}
開發者ID:kilatev,項目名稱:GoGonki,代碼行數:25,代碼來源:server.go

示例5: Test_LogoutOnAccessTokenExpiration

func Test_LogoutOnAccessTokenExpiration(t *testing.T) {
	recorder := httptest.NewRecorder()
	s := sessions.NewCookieStore([]byte("secret123"))

	m := martini.Classic()
	m.Use(sessions.Sessions("my_session", s))
	m.Use(Google(&Options{
	// no need to configure
	}))

	m.Get("/addtoken", func(s sessions.Session) {
		s.Set(keyToken, "dummy token")
	})

	m.Get("/", func(s sessions.Session) {
		if s.Get(keyToken) != nil {
			t.Errorf("User not logged out although access token is expired.")
		}
	})

	addtoken, _ := http.NewRequest("GET", "/addtoken", nil)
	index, _ := http.NewRequest("GET", "/", nil)
	m.ServeHTTP(recorder, addtoken)
	m.ServeHTTP(recorder, index)
}
開發者ID:natebeck,項目名稱:oauth2,代碼行數:25,代碼來源:oauth2_test.go

示例6: main

func main() {
	m := martini.Classic()

	store := sessions.NewCookieStore([]byte("secret-isucon"))
	m.Use(sessions.Sessions("isucon_go_session", store))

	m.Use(martini.Static("../public"))
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
	}))

	m.Get("/", func(r render.Render, session sessions.Session) {
		r.HTML(200, "index", map[string]string{"Flash": getFlash(session, "notice")})
	})

	m.Post("/login", func(req *http.Request, r render.Render, session sessions.Session) {
		user, err := attemptLogin(req)

		notice := ""
		if err != nil || user == nil {
			switch err {
			case ErrBannedIP:
				notice = "You're banned."
			case ErrLockedUser:
				notice = "This account is locked."
			default:
				notice = "Wrong username or password"
			}

			session.Set("notice", notice)
			r.Redirect("/")
			return
		}

		session.Set("user_id", strconv.Itoa(user.ID))
		r.Redirect("/mypage")
	})

	m.Get("/mypage", func(r render.Render, session sessions.Session) {
		currentUser := getCurrentUser(session.Get("user_id"))

		if currentUser == nil {
			session.Set("notice", "You must be logged in")
			r.Redirect("/")
			return
		}

		currentUser.getLastLogin()
		r.HTML(200, "mypage", currentUser)
	})

	m.Get("/report", func(r render.Render) {
		r.JSON(200, map[string][]string{
			"banned_ips":   bannedIPs(),
			"locked_users": lockedUsers(),
		})
	})

	http.ListenAndServe(":8080", m)
}
開發者ID:shogo82148,項目名稱:isucon4_powawa,代碼行數:60,代碼來源:main.go

示例7: main

func main() {
	m := martini.Classic()
	conf := config.LoadConf(martini.Env)
	//create new session middleware
	store := sessions.NewCookieStore([]byte("MushareSecret"))
	store.Options(sessions.Options{
		Path:     "/",
		Domain:   conf.App.Host,
		MaxAge:   60 * 60 * 60 * 24,
		HttpOnly: true,
	})

	//middleware
	m.Handlers(
		middlewares.LogOutput,
		middlewares.Recovery(),
		martini.Logger(),
		sessions.Sessions("_session", store),
		martini.Static("static", martini.StaticOptions{}),
		middlewares.InjectRedis(),
		middlewares.InjectDB(),
	)
	m.Map(conf)

	//routers
	router.Include(m)
	//start server
	m.RunOnAddr(conf.App.Host + ":" + conf.App.Port)
}
開發者ID:ts25504,項目名稱:MuShare,代碼行數:29,代碼來源:app.go

示例8: adminserver

func adminserver() {
	m := martini.Classic()
	// m.Use(martini.Static("./assets/"))

	store := sessions.NewCookieStore([]byte("secret123"))
	m.Use(sessions.Sessions("51jczj_session", store))

	m.Use(render.Renderer(render.Options{
		Layout: "admin_layout",
	}))

	/***********static*************/
	m.Get("/css/**", http.FileServer(http.Dir("./assets")).ServeHTTP)
	m.Get("/editor/**", http.FileServer(http.Dir("./assets")).ServeHTTP)
	m.Get("/fonts/**", http.FileServer(http.Dir("./assets")).ServeHTTP)
	m.Get("/img/**", http.FileServer(http.Dir("./assets")).ServeHTTP)
	m.Get("/js/**", http.FileServer(http.Dir("./assets")).ServeHTTP)

	/***********biz*************/
	m.Get("/", admin.ArchivesHandler)
	m.Get("/archives", admin.ArchivesHandler)

	m.Post("/login", admin.LoginHandler)
	m.Get("/logout", admin.LogoutHandler)
	m.Get("/login", admin.GoLoginHandler)
	m.Get("/login/:path", admin.GoLoginHandler)

	http.ListenAndServe(fmt.Sprintf(":%s", base.AdminWebPort), m)
}
開發者ID:chinab,項目名稱:enterprise-manage,代碼行數:29,代碼來源:server_admin.go

示例9: NewApi

func NewApi(address string, rdb *db.Rethinkdb, auth auth.Authenticator, sessionKey string) (*dialogueApi, error) {
	m := martini.Classic()
	// sessions
	store := sessions.NewCookieStore([]byte(sessionKey))
	m.Use(sessions.Sessions("dialogue", store))

	a := &dialogueApi{
		m:       m,
		rdb:     rdb,
		auth:    auth,
		address: address,
	}
	// middleware
	m.Use(render.Renderer())
	// routes
	// content
	m.Get("/topics", a.apiAuthorize, a.GetTopics)
	m.Post("/topics", a.apiAuthorize, a.PostTopics)
	m.Post("/topics/:topicId", a.apiAuthorize, a.PostTopicsPosts)
	m.Get("/topics/:topicId", a.apiAuthorize, a.GetTopic)
	m.Delete("/topics/:topicId", a.apiAuthorize, a.DeleteTopic)
	m.Delete("/posts/:postId", a.apiAuthorize, a.DeletePost)

	// authentication
	m.Post("/auth", a.Authenticate)
	m.Post("/users", a.apiAuthorize, a.PostUsers)
	m.Put("/users/:username", a.apiAuthorize, a.PutUser)
	// setup
	m.Get("/setup", a.Setup)

	return a, nil
}
開發者ID:carriercomm,項目名稱:dialogue-1,代碼行數:32,代碼來源:api.go

示例10: main

func main() {
	m := martini.Classic()

	// set up the session, and map it into martini
	store := sessions.NewCookieStore([]byte("secret-pass"))
	m.Use(sessions.Sessions("go-webapp", store))

	//on login, we will set the users username in the session
	m.Post("/login", func(w http.ResponseWriter, r *http.Request, session sessions.Session) string {
		username := r.FormValue("username")
		session.Set("username", username)
		http.Redirect(w, r, "/home", http.StatusFound)
		return "OK"
	})

	//if the user has no username, we will redirect them to the login page
	m.Get("/home", authorize, func(user *User) string {
		return "Welcome back, " + user.Username
	})

	//on logout, clear the username in the session
	m.Get("/logout", func(w http.ResponseWriter, r *http.Request, session sessions.Session) string {
		session.Delete("username")
		http.Redirect(w, r, "/login", http.StatusFound)
		return "OK"
	})

	m.Run()
}
開發者ID:kvannotten,項目名稱:projects,代碼行數:29,代碼來源:server.go

示例11: main

func main() {
	m := martini.Classic()
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
		Funcs: []template.FuncMap{
			{
				"add": func(a, b int) int {
					return a + b
				},
			},
		},
	}))

	m.Use(sessions.Sessions("reader_session", nil))

	m.Get("/", controllers.HomeController)

	m.Get("/read/*", controllers.ReadController)

	fmt.Println("Listening on port 8989")
	err := http.ListenAndServe(":8989", m)
	if err != nil {
		fmt.Println(err)
	}
}
開發者ID:mpgarate,項目名稱:justread,代碼行數:25,代碼來源:server.go

示例12: main

func main() {

	m := martini.Classic()
	m.Use(martini_amber.Renderer(map[string]string{

	}))
	store := sessions.NewCookieStore([]byte("secret...."))
	m.Use(sessions.Sessions("msession",store))
	m.Get("/", func(r martini_amber.Render,request *http.Request,session sessions.Session) {
		log.Println(request.UserAgent())
		name := session.Get("name")
		if name==nil && request.FormValue("name")!=""{
			session.Set("name",request.FormValue("name"))
		}
		vars := map[string]interface{}{"cats": []string{"Home", "Product", "Contact","Live Showing"},"arts":arts,"name":name}
		r.Amber(200, "home", vars)
	})
	m.Get("/test", func(r martini_amber.Render,request *http.Request,session sessions.Session) {
		log.Println(request.UserAgent())
		name := session.Get("name")
		if name==nil && request.FormValue("name")!=""{
			session.Set("name",request.FormValue("name"))
		}
		vars := map[string]interface{}{"cats": []string{"Home", "Product", "Contact","Live Showing"},"arts":arts,"name":name}
		r.Amber(200, "test", vars)
	})

	m.RunOnAddr(":8080")
}
開發者ID:secmask,項目名稱:mpage,代碼行數:29,代碼來源:server.go

示例13: main

func main() {
	cfg, err := os.Open("config.json")
	if err != nil {
		log.Fatal(err)
	}
	parser := json.NewDecoder(cfg)
	if err = parser.Decode(common.Config); err != nil {
		log.Fatal(err)
	}

	initDB()
	initQueue()
	initS3()

	m := martini.Classic()

	store := sessions.NewCookieStore([]byte(common.Config.SessionsSecret))
	m.Use(sessions.Sessions("semquery", store))

	m.Use(render.Renderer(render.Options{
		Layout: "layout",
	}))
	m.Use(common.UserInject)

	routes.RegisterRoutes(m)

	if len(os.Args) > 1 {
		m.RunOnAddr(os.Args[1])
	} else {
		m.Run()
	}
}
開發者ID:SemQuery,項目名稱:web,代碼行數:32,代碼來源:server.go

示例14: main

func main() {
	db := sqlx.MustConnect("postgres", "postgres://localhost:15432/auth_sh?sslmode=disable")
	defer db.Close()

	s := oauth.New(db)

	m := martini.Classic()
	m.Use(handlers.NoCache())
	m.Use(sessions.Sessions("auth-sh", sessions.NewCookieStore([]byte("secret123"))))
	m.Use(render.Renderer(render.Options{Layout: "layout"}))
	m.Use(dump_request)
	m.Map(db)
	m.Map(s)

	m.Get("/login", handlers.GET_login)
	m.Get("/link", handlers.GET_link)
	m.Any("/authorize", handlers.GET_authorize)
	m.Any("/token", handlers.GET_token)
	m.Any("/info", handlers.GET_info)
	m.Get("/continue/:provider", handlers.GET_continue)
	m.Get("/logout", handlers.GET_logout)
	m.Get("/callback", handlers.GET_callback)

	m.Get("/", handlers.MayAuthenticate(), handlers.GET_home)
	m.Get("/me", handlers.MustAuthenticate(), handlers.GET_me)
	m.Get("/profile", handlers.MustAuthenticate(), handlers.GET_profile)
	m.Post("/applications", handlers.MustAuthenticate(), handlers.POST_application)

	m.Run()
}
開發者ID:rogeriomarques,項目名稱:oapx,代碼行數:30,代碼來源:main.go

示例15: main

func main() {
	m := martini.Classic()
	m.Map(SetupDB())
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
		Funcs: []template.FuncMap{
			{
				"unescaped": func(args ...interface{}) template.HTML {
					return template.HTML(args[0].(string))
				},
			},
		},
	}))

	//Sessions
	store := sessions.NewCookieStore([]byte("secret123"))
	m.Use(sessions.Sessions("vsauth", store))

	m.Get("/", IndexArticles)
	m.Get("/create", CreateArticle)
	m.Get("/read", ReadArticle)
	m.Get("/update", UpdateArticle)
	m.Get("/delete", DeleteArticle)
	m.Post("/save", SaveArticle)
	m.Get("/login", LoginForm)
	m.Post("/login", UserLogin)
	m.Get("/signup", SignupForm)
	m.Post("/signup", UserSignup)
	m.Get("/logout", UserLogout)
	m.Get("/wrong", WrongLogin)

	m.Run()
}
開發者ID:qapi,項目名稱:goblog-playground,代碼行數:33,代碼來源:main.go


注:本文中的github.com/martini-contrib/sessions.Sessions函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。