Example #1
0
func handleCreateChannel(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	tok, err := channel.Create(ctx, "example")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	io.WriteString(res, tok)
}
Example #2
0
func (api *API) handlePostChannel(res http.ResponseWriter, req *http.Request) error {
	ctx := appengine.NewContext(req)
	u := user.Current(ctx)
	clientID := getClientID(u.Email)
	token, err := channel.Create(ctx, clientID)
	if err != nil {
		return err
	}
	err = saveNewConnection(ctx, clientID)
	if err != nil {
		return err
	}
	return json.NewEncoder(res).Encode(token)
}
Example #3
0
func (api *API) handlePostChannel(res http.ResponseWriter, req *http.Request) error {
	// create channels
	ctx := appengine.NewContext(req)
	u := user.Current(ctx)
	tok, err := channel.Create(ctx, u.Email)
	if err != nil {
		return err
	}
	err = addConnection(ctx, u.Email)
	if err != nil {
		return err
	}
	// sends token to client
	json.NewEncoder(res).Encode(tok)
	return nil
}
Example #4
0
// create the channel
func (api *API) handlePostChannel(res http.ResponseWriter, req *http.Request) error {
	ctx := appengine.NewContext(req)
	u := user.Current(ctx)
	tok, err := channel.Create(ctx, u.Email)
	if err != nil {
		return err
	}
	room, err := getRoom(ctx)
	if err != nil {
		return err
	}
	room.Emails = append(room.Emails, u.Email)
	err = putRoom(ctx, room)
	if err != nil {
		return err
	}
	json.NewEncoder(res).Encode(tok)
	return nil
}
Example #5
0
func registerHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	clientId := r.URL.Query().Get("clientId")
	if clientId == "" {
		failAndLog(ctx, w, http.StatusBadRequest, "missing required query param \"clientId\"")
		return
	}

	key := datastore.NewKey(ctx, ChannelInfoKind, ChannelInfoId, 0, nil)
	channelInfo := ChannelInfo{
		Created:    time.Now(),
		RemoteAddr: r.RemoteAddr,
		ClientId:   clientId,
	}

	if _, err := datastore.Put(ctx, key, &channelInfo); err != nil {
		failAndLog(ctx, w, http.StatusInternalServerError, "failed to put %s to datastore: %s", key, err)
		return
	}

	token, err := channel.Create(ctx, clientId)
	if err != nil {
		failAndLog(ctx, w, http.StatusInternalServerError, "failed to create channel %q: %s", clientId, err)
		return
	}

	log.Debugf(ctx, "created channel %q with token %s", clientId, token)

	rsp := RegisterResponse{
		Token: token,
	}

	w.Header().Set("Content-Type", "application/json")

	if err := json.NewEncoder(w).Encode(&rsp); err != nil {
		failAndLog(ctx, w, http.StatusInternalServerError, "failed to write json to response: %s", err)
		return
	}
}
Example #6
0
File: main.go Project: pubnub/go
func handler(w http.ResponseWriter, r *http.Request) {
	c := createContext(r)
	log.Infof(c, "IN handler")
	uuid := ""
	pubInstance := messaging.New(c, uuid, w, r, publishKey, subscribeKey, secretKey, "", false)
	if pubInstance == nil {
		log.Errorf(c, "Couldn't create pubnub instance")
		http.Error(w, "Couldn't create pubnub instance", http.StatusInternalServerError)
		return
	}
	nuuid := pubInstance.GetUUID()

	session, err := store.Get(r, "example-session")
	if err != nil {
		log.Errorf(c, "Session store error %s", err.Error())
		http.Error(w, "Session store error", http.StatusInternalServerError)
		return
	}

	//Enhancement: can be kept in memcache
	tok, err := channel.Create(c, nuuid)
	if err != nil {
		http.Error(w, "Couldn't create Channel", http.StatusInternalServerError)
		log.Errorf(c, "channel.Create: %v", err)
		return
	}
	session.Values["token"] = tok

	err1 := mainTemplate.Execute(w, map[string]string{
		"token":        tok,
		"uuid":         nuuid,
		"subscribeKey": subscribeKey,
		"publishKey":   publishKey,
		"secretKey":    secretKey,
	})
	if err1 != nil {
		log.Errorf(c, "mainTemplate: %v", err1)
	}
}
Example #7
0
File: main.go Project: pubnub/go
func connect(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	pubKey := q.Get("pubKey")
	subKey := q.Get("subKey")
	uuid := q.Get("uuid")
	secKey := q.Get("secKey")
	cipher := q.Get("cipher")
	ssl := q.Get("ssl")
	bSsl := false
	if ssl == "1" {
		bSsl = true
	}

	//	c := context.NewContext(r)
	c := createContext(r)
	messaging.SetSessionKeys(c, w, r, pubKey, subKey, secKey, cipher, bSsl, uuid)

	session, err := store.Get(r, "example-session")
	if err != nil {
		log.Errorf(c, "Session store error %s", err.Error())
		http.Error(w, "Session store error", http.StatusInternalServerError)
		return
	}

	tok, err := channel.Create(c, uuid)

	if err != nil {
		http.Error(w, "Couldn't create Channel", http.StatusInternalServerError)
		log.Errorf(c, "channel.Create: %v", err)
		return
	}
	session.Values["token"] = tok
	err1 := session.Save(r, w)
	if err1 != nil {
		log.Errorf(c, "error1, %s", err1.Error())
	}
	fmt.Fprintf(w, tok)
}