示例#1
0
文件: main.go 项目: julan/Koderank
func whiteboardHandler(w http.ResponseWriter, r *http.Request) {
	vm := NewWhiteboardVM()
	c := appengine.NewContext(r)

	code := r.FormValue("x")
	repo := repository.NewRepo(c)
	candidate, interviewer, err := repo.GetSession(code)

	if err != nil {
		c.Errorf("repo.GetSession: %v", err)
		http.Redirect(w, r, "/", http.StatusMovedPermanently)
		return
	}

	vm.Candidate, vm.Interviewer = candidate, interviewer

	tc := twilio.NewTwilioCapability(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

	vm.IsInterviewer = (code == interviewer)
	if vm.IsInterviewer {
		vm.Message = "Interviewer mode."
		tc.AllowClientOutgoing(TWILIO_APP_SID, nil)
	} else {
		vm.Message = "Candidate mode."
		tc.AllowClientIncoming(candidate)
	}

	tokenExpiration := time.Duration(48) * time.Hour

	twilioToken, err := tc.Generate(tokenExpiration)

	if err != nil {
		c.Errorf("capability.Generate: %s", err)
		http.Error(w, "Twilio setup error.", http.StatusInternalServerError)
		return
	}
	vm.TwilioToken = twilioToken

	// Create channel token.
	tok, err := channel.Create(c, code)
	if err != nil {
		c.Errorf("template.ParseSetFiles: %s", err)
		http.Error(w, "Channel error.", http.StatusInternalServerError)
		return
	}
	vm.ChannelToken = tok

	renderPage(w, c, "view/whiteboard.html", vm)
}
示例#2
0
文件: main.go 项目: julan/Koderank
func messageHandler(w http.ResponseWriter, r *http.Request) {

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

	c := appengine.NewContext(r)
	if r.Method != "POST" {
		c.Errorf("messageHandler did not expect %s", r.Method)
		http.Error(w, "Method not allowed.", http.StatusInternalServerError)
		return
	}

	var patchSet map[string]interface{}

	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(&patchSet); err != nil {
		c.Errorf("json.NewDecoder: %s", err)
		http.Error(w, "Error decoding json.", http.StatusInternalServerError)
		return
	}

	code := patchSet["key"].(string)
	repo := repository.NewRepo(c)

	candidate, interviewer, err := repo.GetSession(code)
	if err != nil {
		c.Errorf("repo.GetSession: %v", err)
		http.Redirect(w, r, "/", 301)
		return
	}

	if code == interviewer {
		err := channel.SendJSON(c, candidate, patchSet)
		if err != nil {
			c.Errorf("channel.SendJSON, problem sending: %s.", err)
			http.Error(w, "Error decoding json.", http.StatusInternalServerError)
		}
	} else {
		err := channel.SendJSON(c, interviewer, patchSet)
		if err != nil {
			c.Errorf("channel.SendJSON, problem sending: %s.", err)
			http.Error(w, "Error decoding json.", http.StatusInternalServerError)
		}
	}
}
示例#3
0
文件: main.go 项目: julan/Koderank
func createUserHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if r.Method != "POST" {
		c.Errorf("createUserHandler did not expect %s", r.Method)
		http.Error(w, "Method not allowed.", http.StatusInternalServerError)
		return
	}

	repo := repository.NewRepo(c)
	candidate, interviewer, err := repo.CreateSession()
	if err != nil {
		c.Errorf("repository.CreateSession: %s", err)
		http.Error(w, "Problem with datastore.", http.StatusInternalServerError)
		return
	}

	vm := map[string]string{
		"c": candidate,
		"i": interviewer,
	}

	renderJson(w, c, vm)
}