Example #1
0
// Renders the example content including support for context sensitive
// text substitution.
func (c *exampleContent) Write(ctx context.Context, w io.Writer) (int, error) {
	e := c.Example
	wwwURL := fburl.URL{
		Env: rellenv.FbEnv(c.Context),
	}
	w = htmlwriter.New(w)
	tpl, err := template.New("example-" + e.URL).Parse(string(e.Content))
	if err != nil {
		// if template parsing fails, we ignore it. it's probably malformed html
		return fmt.Fprint(w, e.Content)
	}
	countingW := counting.NewWriter(w)
	err = tpl.Execute(countingW,
		struct {
			Rand     string // a random token
			RellFBNS string // the OG namespace
			RellURL  string // local http://www.fbrell.com/ URL
			WwwURL   string // server specific http://www.facebook.com/ URL
		}{
			Rand:     randString(10),
			RellFBNS: rellenv.FbApp(c.Context).Namespace(),
			RellURL:  c.Env.AbsoluteURL("/").String(),
			WwwURL:   wwwURL.String(),
		})
	if err != nil {
		// if template execution fails, we ignore it. it's probably malformed html
		return fmt.Fprint(w, e.Content)
	}
	return countingW.Count(), err
}
Example #2
0
func (e *envSelector) HTML(ctx context.Context) (h.HTML, error) {
	if !rellenv.IsEmployee(e.Context) {
		return nil, nil
	}
	fbEnv := rellenv.FbEnv(e.Context)
	frag := h.Frag{
		h.HiddenInputs(url.Values{
			"server": []string{fbEnv},
		}),
	}
	for _, pair := range sortutil.StringMapByValue(envOptions) {
		if fbEnv == pair.Key {
			continue
		}
		ctxCopy := e.Env.Copy()
		ctxCopy.Env = pair.Key
		frag = append(frag, &h.Li{
			Inner: &h.A{
				Inner:  h.String(pair.Value),
				Target: "_top",
				HREF:   ctxCopy.ViewURL(e.Example.URL),
			},
		})
	}

	title := envOptions[fbEnv]
	if title == "" {
		title = fbEnv
	}
	return &h.Div{
		Class: "btn-group",
		Inner: h.Frag{
			&h.Button{
				Class: "btn",
				Inner: h.Frag{
					&h.I{Class: "icon-road"},
					h.String(" "),
					h.String(title),
				},
			},
			&h.Button{
				Class: "btn dropdown-toggle",
				Data: map[string]interface{}{
					"toggle": "dropdown",
				},
				Inner: &h.Span{
					Class: "caret",
				},
			},
			&h.Ul{
				Class: "dropdown-menu",
				Inner: frag,
			},
		},
	}, nil
}
Example #3
0
File: og.go Project: stoyan/rell
// Get a URL to the Like button for the Object.
func (o *Object) LikeURL() string {
	values := url.Values{}
	values.Set("href", o.URL())
	u := &fburl.URL{
		Scheme:    o.env.Scheme,
		Env:       rellenv.FbEnv(o.context),
		SubDomain: fburl.DWww,
		Path:      "/plugins/like",
		Values:    values,
	}
	return u.String()
}
Example #4
0
File: og.go Project: stoyan/rell
// Get a URL to the Lint tool for the Object.
func (o *Object) LintURL() string {
	values := url.Values{}
	values.Set("q", o.URL())
	u := &fburl.URL{
		Scheme:    o.env.Scheme,
		Env:       rellenv.FbEnv(o.context),
		SubDomain: fburl.DDevelopers,
		Path:      "/tools/debug/og/object",
		Values:    values,
	}
	return u.String()
}
Example #5
0
func (a *Handler) Response(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	c, err := rellenv.FromContext(ctx)
	if err != nil {
		return err
	}
	if r.FormValue("state") != a.state(w, r) {
		return ctxerr.Wrap(ctx, errInvalidState)
	}

	values := url.Values{}
	values.Set("client_id", strconv.FormatUint(a.App.ID(), 10))
	values.Set("client_secret", a.App.Secret())
	values.Set("redirect_uri", redirectURI(c))
	values.Set("code", r.FormValue("code"))

	atURL := &fburl.URL{
		Scheme:    "https",
		SubDomain: fburl.DGraph,
		Env:       rellenv.FbEnv(ctx),
		Path:      "/oauth/access_token",
		Values:    values,
	}

	req, err := http.NewRequest("GET", atURL.String(), nil)
	if err != nil {
		return ctxerr.Wrap(ctx, errOAuthFail)
	}
	res, err := a.HttpTransport.RoundTrip(req)
	if err != nil {
		return ctxerr.Wrap(ctx, err)
	}
	defer res.Body.Close()
	bd, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return ctxerr.Wrap(ctx, err)
	}
	h.WriteResponse(w, r, &h.Frag{
		&h.Script{Inner: h.Unsafe("window.location.hash = ''")},
		h.String(string(bd)),
	})
	return nil
}
Example #6
0
File: oauth.go Project: daaku/rell
func (a *Handler) Start(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	c, err := rellenv.FromContext(ctx)
	if err != nil {
		return err
	}
	values := url.Values{}
	values.Set("client_id", strconv.FormatUint(rellenv.FbApp(ctx).ID(), 10))
	if scope := r.FormValue("scope"); scope != "" {
		values.Set("scope", scope)
	}

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

	dialogURL := fburl.URL{
		Scheme:    "https",
		SubDomain: fburl.DWww,
		Env:       rellenv.FbEnv(ctx),
		Path:      "/dialog/oauth",
		Values:    values,
	}

	if c.ViewMode == rellenv.Website {
		http.Redirect(w, r, dialogURL.String(), 302)
	} else {
		b, _ := json.Marshal(dialogURL.String())
		_, err := h.Write(ctx, w, &h.Script{
			Inner: h.Unsafe(fmt.Sprintf("top.location=%s", b)),
		})
		return err
	}
	return nil
}