Пример #1
0
func PostLoginHandler(w http.ResponseWriter, rnd render.Render, r *http.Request, s *session.Session) {
	username := r.FormValue("username")
	password := r.FormValue("password")

	s.Username = username
	s.IsAuthorized = true

	fmt.Println(username)
	fmt.Println(password)

	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims["Name"] = "token"
	token.Claims["exp"] = time.Now().Add(time.Minute * 5).Unix()
	tokenString, err := token.SignedString([]byte(session.TOKEN_STR))
	fmt.Println(tokenString)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Printf("Token Signing error: %v\n", err)
		fmt.Fprintln(w, "Sorry, error while Signing Token!")
	}

	http.SetCookie(w, &http.Cookie{
		Name:  session.TOKEN_NAME,
		Value: tokenString,
	})
	s.Id = tokenString

	storage, err := models.GetStorage()
	if err != nil {
		log.Fatalln(err)
	}
	storage.SaveUser(models.NewUser(username))
	rnd.Redirect("/")
}
Пример #2
0
func main() {

	path := "/tmp/pc"

	graph.InitQuadStore("bolt", path, nil)

	m := martini.Classic()

	unescapeFuncMap := template.FuncMap{"unescape": unescape}

	m.Use(session.Middleware)

	m.Use(render.Renderer(render.Options{
		Directory:  "templates",                         // Specify what path to load the templates from.
		Layout:     "layout",                            // Specify a layout template. Layouts can call {{ yield }} to render the current template.
		Extensions: []string{".tmpl", ".html"},          // Specify extensions to load for templates.
		Funcs:      []template.FuncMap{unescapeFuncMap}, // Specify helper function maps for templates to access.
		Charset:    "UTF-8",                             // Sets encoding for json and html content-types. Default is "UTF-8".
		IndentJSON: true,                                // Output human readable JSON
	}))

	storage, err := models.GetStorage()
	if err != nil {
		log.Fatalln(err)
	}

	server, err := server.NewServer(storage)
	if err != nil {
		log.Fatalln(err)
	}

	user := models.NewUser("admin")
	user.Iteration()

	staticOptions := martini.StaticOptions{Prefix: "assets"}
	m.Use(martini.Static("assets", staticOptions))
	m.Get("/", routes.IndexHandler)
	m.Get("/login", routes.GetLoginHandler)
	m.Get("/logout", routes.LogoutHandler)
	m.Post("/login", routes.PostLoginHandler)
	m.Get("/view:id", routes.ViewHandler)
	m.Post("/gethtml", routes.GetHtmlHandler)
	m.Get("/socket.io/", func(w http.ResponseWriter, rnd render.Render, r *http.Request, s *session.Session) {
		server.SetSession(s)
		server.ServeHTTP(w, r)
	})
	m.Run()
}
Пример #3
0
func IndexHandler(rnd render.Render, s *session.Session) {
	user := models.NewUser(s.Username)
	storage, err := models.GetStorage()
	if err != nil {
		log.Fatalln(err)
	}

	data := Home{
		Current:      user,
		Users:        storage.GetUsers(user),
		IsAuthorized: s.IsAuthorized,
	}

	if user.Iteration() {
		data.SecondsLeft = user.IterationTime() - time.Now().Unix()
	} else if !user.CanStart() {
		data.SecondsLeft = models.Wait5Minutes - (time.Now().Unix() - user.IterationTime())
	}

	rnd.HTML(200, "index", data)
}