Example #1
0
// Handler for /info/ to see a JSON view of some server context.
func Info(w http.ResponseWriter, r *http.Request) {
	context, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	info := map[string]interface{}{
		"request": map[string]interface{}{
			"method": r.Method,
			"form":   r.Form,
			"url": map[string]interface{}{
				"path":  r.URL.Path,
				"query": r.URL.RawQuery,
			},
			"headers": headerMap(r.Header),
		},
		"context":    context,
		"pageTabURL": context.PageTabURL("/"),
		"canvasURL":  context.CanvasURL("/"),
		"sdkURL":     context.SdkURL(),
	}
	if version != "" {
		info["version"] = version
	}
	humanJSON(info, w, r)
}
Example #2
0
func Raw(w http.ResponseWriter, r *http.Request) {
	context, example, err := parse(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	if !example.AutoRun {
		view.Error(
			w, r, errors.New("Not allowed to view this example in raw mode."))
		return
	}
	service.Stats.Inc("viewed example in raw mode")
	view.Write(w, r, &exampleContent{
		Context: context,
		Example: example,
	})
}
Example #3
0
func Saved(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" && r.URL.Path == savedPath {
		c, err := context.FromRequest(r)
		if err != nil {
			view.Error(w, r, err)
			return
		}
		if !xsrf.Validate(r.FormValue(paramName), w, r, savedPath) {
			service.Stats.Inc(savedPath + " xsrf failure")
			view.Error(w, r, errTokenMismatch)
			return
		}
		content := bytes.TrimSpace([]byte(r.FormValue("code")))
		content = bytes.Replace(content, []byte{13}, nil, -1) // remove CR
		id := examples.ContentID(content)
		db := examples.GetDB(c.Version)
		example, ok := db.Reverse[id]
		if ok {
			http.Redirect(w, r, c.ViewURL(example.URL), 302)
			return
		}
		err = exampleStore.Save(id, content)
		if err != nil {
			view.Error(w, r, err)
			return
		}
		service.Stats.Inc("saved example")
		http.Redirect(w, r, c.ViewURL(savedPath+id), 302)
		return
	} else {
		context, example, err := parse(r)
		if err != nil {
			view.Error(w, r, err)
			return
		}
		service.Stats.Inc("viewed saved example")
		view.Write(w, r, &page{
			Writer:  w,
			Request: r,
			Context: context,
			Example: example,
		})
	}
}
Example #4
0
func Simple(w http.ResponseWriter, r *http.Request) {
	context, example, err := parse(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	if !example.AutoRun {
		view.Error(
			w, r, errors.New("Not allowed to view this example in simple mode."))
		return
	}
	service.Stats.Inc("viewed example in simple mode")
	view.Write(w, r, &h.Document{
		Inner: &h.Frag{
			&h.Head{
				Inner: &h.Frag{
					&h.Meta{Charset: "utf-8"},
					&h.Title{h.String(example.Title)},
				},
			},
			&h.Body{
				Inner: &h.Frag{
					&loader.HTML{
						Resource: []loader.Resource{
							&fb.Init{
								AppID:      context.AppID,
								ChannelURL: context.ChannelURL(),
								URL:        context.SdkURL(),
							},
						},
					},
					&h.Div{
						ID: "example",
						Inner: &exampleContent{
							Context: context,
							Example: example,
						},
					},
				},
			},
		},
	})
}
Example #5
0
func SdkChannel(w http.ResponseWriter, r *http.Request) {
	const maxAge = 31536000 // 1 year
	context, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	service.Stats.Inc("viewed channel")
	w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", maxAge))
	view.Write(w, r, &h.Script{Src: context.SdkURL()})
}
Example #6
0
func List(w http.ResponseWriter, r *http.Request) {
	context, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	service.Stats.Inc("viewed examples listing")
	view.Write(w, r, &examplesList{
		Context: context,
		DB:      examples.GetDB(context.Version),
	})
}
Example #7
0
func Response(w http.ResponseWriter, r *http.Request) {
	c, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	if r.FormValue("state") != state(w, r) {
		view.Error(w, r, errInvalidState)
		return
	}
	values := url.Values{}
	values.Set("client_id", strconv.FormatUint(fbapp.Default.ID(), 10))
	values.Set("client_secret", fbapp.Default.Secret())
	values.Set("redirect_uri", redirectURI(c))
	values.Set("code", r.FormValue("code"))
	res, err := fbapi.GetRaw("/oauth/access_token", values)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	w.Write(res)
}
Example #8
0
func Example(w http.ResponseWriter, r *http.Request) {
	context, example, err := parse(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	service.Stats.Inc("viewed stock example")
	view.Write(w, r, &page{
		Writer:  w,
		Request: r,
		Context: context,
		Example: example,
	})
}
Example #9
0
func Start(w http.ResponseWriter, r *http.Request) {
	c, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	values := url.Values{}
	values.Set("client_id", strconv.FormatUint(c.AppID, 10))
	if scope := r.FormValue("scope"); scope != "" {
		values.Set("scope", scope)
	}

	if c.ViewMode == context.Website {
		values.Set("redirect_uri", redirectURI(c))
		values.Set("state", state(w, r))
	} else {
		values.Set("redirect_uri", c.ViewURL("/auth/session"))
	}

	dialogURL := fburl.URL{
		Scheme:    "https",
		SubDomain: fburl.DWww,
		Env:       c.Env,
		Path:      "/dialog/oauth",
		Values:    values,
	}

	if c.ViewMode == context.Website {
		http.Redirect(w, r, dialogURL.String(), 302)
	} else {
		b, _ := json.Marshal(dialogURL.String())
		view.Write(w, r, &h.Script{
			Inner: h.Unsafe(fmt.Sprintf("top.location=%s", b)),
		})
	}
}
Example #10
0
func (a *Handler) handleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
	a.Logger.Printf("Error at %s\n%s\n", r.URL, ctxerr.RichString(err))
	view.Error(w, r, a.Static, err)
}