Beispiel #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("/")
}
Beispiel #2
0
func NewServer(storage *models.Storage) (*SocketServer, error) {
	s, err := socketio.NewServer(nil)
	if err != nil {
		return nil, err
	}

	server := &SocketServer{s, nil, storage}

	server.On("connection", func(so socketio.Socket) {
		so.Join(RoomName)
		so.On("start", func() {
			if server.session == nil || !server.session.IsAuthorized {
				return
			}
			user := models.NewUser(server.session.Username)
			user.Start(models.Iterate30Minutes)
			so.BroadcastTo(RoomName, "start", user.Name)
		})
		so.On("stop", func() {
			if server.session == nil || !server.session.IsAuthorized {
				return
			}
			user := models.NewUser(server.session.Username)
			user.Stop()
			so.BroadcastTo(RoomName, "stop", user.Name)
		})
		so.On("disconnection", func() {
			log.Println("on disconnect")
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	server.initTimer()

	return server, nil
}
Beispiel #3
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()
}
Beispiel #4
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)
}