Example #1
0
// NewProviderGroup makes a provider group. Requires a session namespace (will
// be prepended to ":"+provider_name), the base URL of the ProviderGroup's
// http.Handler, a collection of URLs for redirecting, and a list of specific
// configured providers.
func NewProviderGroup(session_namespace string, group_base_url string,
	urls RedirectURLs, providers ...*Provider) (*ProviderGroup, error) {

	group_base_url = strings.TrimRight(group_base_url, "/")

	g := &ProviderGroup{
		handlers:       make(map[string]*ProviderHandler, len(providers)),
		urls:           urls,
		group_base_url: group_base_url}

	g.mux = webhelp.DirMux{
		"all": webhelp.DirMux{"logout": webhelp.Exact(
			webhelp.HandlerFunc(g.logoutAll))},
	}

	for _, provider := range providers {
		if provider.Name == "" {
			return nil, fmt.Errorf("empty provider name")
		}
		_, exists := g.handlers[provider.Name]
		if exists {
			return nil, fmt.Errorf("two providers given with name %#v",
				provider.Name)
		}
		handler := NewProviderHandler(provider,
			fmt.Sprintf("%s-%s", session_namespace, provider.Name),
			fmt.Sprintf("%s/%s", group_base_url, provider.Name), urls)
		g.handlers[provider.Name] = handler
		g.mux[provider.Name] = handler
	}

	return g, nil
}
Example #2
0
// NewProviderHandler makes a provider handler. Requires a provider
// configuration, a session namespace, a base URL for the handler, and a
// collection of URLs for redirecting.
func NewProviderHandler(provider *Provider, session_namespace string,
	handler_base_url string, urls RedirectURLs) *ProviderHandler {
	if urls.DefaultLoginURL == "" {
		urls.DefaultLoginURL = "/"
	}
	if urls.DefaultLogoutURL == "" {
		urls.DefaultLogoutURL = "/"
	}
	h := &ProviderHandler{
		provider:          provider,
		session_namespace: session_namespace,
		handler_base_url:  strings.TrimRight(handler_base_url, "/"),
		urls:              urls}
	h.DirMux = webhelp.DirMux{
		"login":  webhelp.Exact(webhelp.HandlerFunc(h.login)),
		"logout": webhelp.Exact(webhelp.HandlerFunc(h.logout)),
		"_cb":    webhelp.Exact(webhelp.HandlerFunc(h.cb))}
	return h
}
Example #3
0
func ExampleArgMux(t *testing.T) {
	pageName := webhelp.NewArgMux()
	handler := webhelp.DirMux{
		"wiki": pageName.Shift(webhelp.Exact(webhelp.HandlerFunc(
			func(ctx context.Context, w webhelp.ResponseWriter, r *http.Request) error {
				name := pageName.Get(ctx)
				w.Header().Set("Content-Type", "text/plain")
				fmt.Fprintf(w, "Welcome to %s", name)
				return nil
			})))}

	webhelp.ListenAndServe(":0", handler)
}
Example #4
0
func main() {
	flag.Parse()
	loadOAuth2()
	secret, err := hex.DecodeString(*cookieSecret)
	if err != nil {
		panic(err)
	}

	renderer, err := NewRenderer()
	if err != nil {
		panic(err)
	}

	data, err := NewData()
	if err != nil {
		panic(err)
	}
	defer data.Close()

	endpoints := NewEndpoints(data)

	routes := webhelp.LoggingHandler(
		sessions.HandlerWithStore(sessions.NewCookieStore(secret),
			webhelp.OverlayMux{
				Fallback: endpoints.LoginRequired(webhelp.DirMux{
					"": webhelp.Exact(renderer.Render(endpoints.ProjectList)),

					"project": projectId.OptShift(

						webhelp.ExactPath(webhelp.MethodMux{
							"GET":  webhelp.RedirectHandler("/"),
							"POST": renderer.Process(endpoints.NewProject),
						}),

						webhelp.DirMux{
							"": webhelp.Exact(renderer.Render(endpoints.Project)),

							"sample": sampleId.OptShift(
								webhelp.ExactPath(webhelp.MethodMux{
									"GET": ProjectRedirector,
								}),
								webhelp.DirMux{
									"": webhelp.ExactGet(renderer.Render(endpoints.Sample)),
									"similar": webhelp.ExactGet(
										renderer.Render(endpoints.SampleSimilar)),
								},
							),

							"control": controlId.OptShift(
								webhelp.ExactPath(webhelp.MethodMux{
									"GET":  ProjectRedirector,
									"POST": renderer.Process(endpoints.NewControl),
								}),

								webhelp.DirMux{
									"": webhelp.Exact(renderer.Render(endpoints.Control)),
									"sample": webhelp.ExactPath(webhelp.ExactMethod("POST",
										renderer.Process(endpoints.NewSample))),
								},
							),

							"control_named": controlName.OptShift(
								webhelp.ExactGet(ProjectRedirector),
								webhelp.DirMux{
									"sample": webhelp.ExactPath(webhelp.ExactMethod("POST",
										renderer.Process(endpoints.NewSampleFromName))),
								}),

							"search": webhelp.ExactMethod("POST",
								webhelp.ExactPath(renderer.Render(endpoints.Search)),
							),
						},
					),

					"account": webhelp.DirMux{
						"apikeys": webhelp.ExactPath(webhelp.MethodMux{
							"GET":  renderer.Render(endpoints.APIKeys),
							"POST": renderer.Process(endpoints.NewAPIKey),
						}),
					},
				}),
				Overlay: webhelp.DirMux{
					"auth": oauth2,
				}}))

	switch flag.Arg(0) {
	case "createdb":
		err := data.CreateDB()
		if err != nil {
			panic(err)
		}
	case "serve":
		panic(webhelp.ListenAndServe(*listenAddr, routes))
	case "routes":
		webhelp.PrintRoutes(os.Stdout, routes)
	default:
		fmt.Printf("Usage: %s <serve|createdb|routes>\n", os.Args[0])
	}
}