Пример #1
0
func genSession(w http.ResponseWriter, r *http.Request) (*sessions.Session, error) {
	// Create a session and store it in cookie so that we can recognize the user when he/she gets back
	// TODO: read session/cookie name from config
	s, err := sessionStore.Get(r, "gapp")
	if err != nil {
		return nil, err
	}

	// Changed maximum age of the session to one month
	s.Options = &sessions.Options{
		Path:   "/",
		MaxAge: 86400 * 30,
	}

	// Generate new userid if there isn't any
	userid, ok := s.Values["userid"]
	if !ok {
		userid = utils.GenId(16)
		s.Values["userid"] = userid
	}

	// Saving session everytime it gets access helps to push expiry date further
	err = s.Save(r, w)

	return s, err
}
Пример #2
0
func (p *Project) Save() error {

	_, err := url.ParseRequestURI(p.URL)
	if err != nil {
		return InvalidUrlError
	}

	c := redisPool.Get()
	defer c.Close()

	hc := hdis.Conn{c}

	// Use a short project id and double checked for it's existance
	for {
		p.Id = utils.GenId(3)
		key := "p:" + p.Id

		if exists, _ := redis.Bool(hc.Do("HEXISTS", key)); !exists {
			break
		}
	}

	if ok, _ := redis.Bool(c.Do("SADD", "jobs-in-queue", p.Id)); ok {

		hc.Do("HINCRBY", "u:"+p.OwnerId+":jobs", 1)

		j := job{project: p, err: make(chan error)}

		// Put the job in the queue, this will block if it's full
		saveQueue <- j

		// Read the result from err channel
		return <-j.err
	}

	return ProjectSaveError
}
Пример #3
0
func newContext(w http.ResponseWriter, r *http.Request) (*Context, error) {

	context := &Context{Response: w, Request: r}

	// Create a session and store it in cookie so that we can recognize the user when he/she gets back
	// TODO: read session/cookie name from config
	var err error
	context.Session, err = sessionStore.Get(r, "gapp")
	if err != nil {
		log.Printf("error in getting session store: %q", err)
	}

	// Changed maximum age of the session to one month
	context.Session.Options = &sessions.Options{
		Path:   "/",
		MaxAge: 86400 * 30,
	}

	// Generate new userid if there isn't any
	userid, ok := context.Session.Values["userid"].(string)
	if !ok {
		// It's an anonymous user
		userid = utils.GenId(16)
		context.Session.Values["userid"] = userid

	} else {
		// Let's fetch user's profile

		conn := RedisPool.Get()
		defer conn.Close()

		hc := hdis.Conn{conn}

		key := "u:" + userid + ":gob"

		if ok, _ := redis.Bool(hc.Do("HEXISTS", key)); ok {
			u, err := redis.Bytes(hc.Get(key))
			if err == nil {
				var user *User

				buffer := bytes.NewBuffer(u)
				dec := gob.NewDecoder(buffer)
				err = dec.Decode(&user)

				if err == nil {
					context.User = user
				} else {
					log.Printf("error in decoding user's (%s) profile: %q", userid, err)
				}
			} else {
				log.Printf("error in fetching user's (%s) profile from redis: %q", userid, err)
			}
		}
	}

	if context.User == nil {
		context.User = &User{Id: userid, Name: "Anonymous"}
	}

	// Saving session everytime it gets access helps to push expiry date further
	err = context.Session.Save(r, w)

	return context, err
}