Example #1
0
func TestInvalidFlagValue(t *testing.T) {
	const name = "TestInvalidFlagValue"
	s := flag.NewFlagSet(name, flag.PanicOnError)
	s.Int("bar", 0, "")
	os.Setenv(named(name, "bar"), "a")
	ensure.Err(t, flagenv.ParseSet(name, s),
		regexp.MustCompile(`failed to set flag "bar" with value "a"`))
}
Example #2
0
func TestReturnsFirstError(t *testing.T) {
	const name = "TestReturnsFirstError"
	s := flag.NewFlagSet(name, flag.PanicOnError)
	s.Int("bar1", 0, "")
	s.Int("bar2", 0, "")
	os.Setenv(named(name, "bar1"), "a")
	ensure.Err(t, flagenv.ParseSet(name, s),
		regexp.MustCompile(`failed to set flag "bar1" with value "a"`))
}
Example #3
0
func TestExplicitAreIgnored(t *testing.T) {
	const name = "TestExplicitAreIgnored"
	s := flag.NewFlagSet(name, flag.PanicOnError)
	const bar = int(43)
	barActual := s.Int("bar", 0, "")
	s.Parse([]string{"-bar", fmt.Sprint(bar)})
	os.Setenv(named(name, "bar"), "44")
	ensure.Nil(t, flagenv.ParseSet(name, s))
	ensure.DeepEqual(t, *barActual, bar)
}
Example #4
0
func TestAFewFlags(t *testing.T) {
	const name = "TestAFewFlags"
	s := flag.NewFlagSet(name, flag.PanicOnError)
	const foo = "42"
	const bar = int(43)
	fooActual := s.String("foo", "", "")
	barActual := s.Int("bar", 0, "")
	os.Setenv(named(name, "foo"), foo)
	os.Setenv(named(name, "bar"), fmt.Sprint(bar))
	ensure.Nil(t, flagenv.ParseSet(name, s))
	ensure.DeepEqual(t, *fooActual, foo)
	ensure.DeepEqual(t, *barActual, bar)
}
Example #5
0
func main() {
	const signedRequestMaxAge = time.Hour * 24
	runtime.GOMAXPROCS(runtime.NumCPU())

	flagSet := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ExitOnError)
	dev := flagSet.Bool("dev", runtime.GOOS != "linux", "development mode")
	addr := flagSet.String("addr", defaultAddr(), "server address to bind to")
	adminPath := flagSet.String("admin-path", "", "secret admin path")
	facebookAppID := flagSet.Uint64("fb-app-id", 342526215814610, "facebook application id")
	facebookAppSecret := flagSet.String("fb-app-secret", "", "facebook application secret")
	facebookAppNS := flagSet.String("fb-app-ns", "", "facebook application namespace")
	empCheckerAppID := flagSet.Uint64("empcheck-app-id", 0, "empcheck application id")
	empCheckerAppSecret := flagSet.String("empcheck-app-secret", "", "empcheck application secret")
	parseAppID := flagSet.String("parse-app-id", "", "parse application id")
	parseMasterKey := flagSet.String("parse-master-key", "", "parse master key")
	publicDir := flagSet.String(
		"public-dir", pkgDir("github.com/daaku/rell/public"), "public files directory")
	examplesDir := flagSet.String(
		"examples-dir", pkgDir("github.com/daaku/rell/examples/db"), "example files directory")

	flagSet.Parse(os.Args[1:])
	if err := flagenv.ParseSet("RELL_", flagSet); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}

	if *dev {
		devrestarter.Init()
	}

	logger := log.New(os.Stderr, "", log.LstdFlags)
	// for systemd started servers we can skip the date/time since journald
	// already shows it
	if os.Getppid() == 1 {
		logger.SetFlags(0)
	}

	fbApp := fbapp.New(
		*facebookAppID,
		*facebookAppSecret,
		*facebookAppNS,
	)
	forwarded := &trustforward.Forwarded{
		X:          true,
		CloudFlare: true,
	}
	bid := &browserid.Cookie{
		Name:      "z",
		MaxAge:    time.Hour * 24 * 365 * 10, // 10 years
		Length:    16,
		Logger:    logger,
		Forwarded: forwarded,
	}
	xsrf := &xsrf.Provider{
		BrowserID: bid,
		MaxAge:    time.Hour * 24,
		SumLen:    10,
	}
	publicFS := http.Dir(*publicDir)
	static := &static.Handler{
		Path: "/static/",
		Box:  static.FileSystemBox(publicFS),
	}
	httpTransport := &httpcontrol.Transport{
		MaxIdleConnsPerHost:   http.DefaultMaxIdleConnsPerHost,
		DialTimeout:           2 * time.Second,
		ResponseHeaderTimeout: 3 * time.Second,
		RequestTimeout:        30 * time.Second,
	}
	parseClient := &parse.Client{
		Transport: httpTransport,
		Credentials: parse.MasterKey{
			ApplicationID: *parseAppID,
			MasterKey:     *parseMasterKey,
		},
	}
	fbApiClient := &fbapi.Client{
		Redact:    true,
		Transport: httpTransport,
	}
	lruCache := lru.New(10000)
	empChecker := &empcheck.Checker{
		FbApiClient: fbApiClient,
		App:         fbapp.New(*empCheckerAppID, *empCheckerAppSecret, ""),
		Logger:      logger,
		Cache:       lruCache,
	}
	appNSFetcher := &appns.Fetcher{
		Apps:        []fbapp.App{fbApp},
		FbApiClient: fbApiClient,
		Logger:      logger,
		Cache:       lruCache,
	}
	exampleStore := &examples.Store{
		Parse: parseClient,
		DB:    examples.MustMakeDB(*examplesDir),
		Cache: lruCache,
	}
	webHandler := &web.Handler{
		Static: static,
		App:    fbApp,
		Logger: logger,
		EnvParser: &rellenv.Parser{
			App:                 fbApp,
			EmpChecker:          empChecker,
			AppNSFetcher:        appNSFetcher,
			SignedRequestMaxAge: signedRequestMaxAge,
			Forwarded:           forwarded,
		},
		PublicFS:       publicFS,
		ContextHandler: &viewcontext.Handler{},
		ExamplesHandler: &viewexamples.Handler{
			ExampleStore: exampleStore,
			Xsrf:         xsrf,
			Static:       static,
		},
		OgHandler: &viewog.Handler{
			Static:       static,
			ObjectParser: &og.Parser{Static: static},
		},
		OauthHandler: &oauth.Handler{
			BrowserID:     bid,
			App:           fbApp,
			HttpTransport: httpTransport,
			Static:        static,
		},
		AdminHandler: &adminweb.Handler{
			Forwarded: forwarded,
			Path:      *adminPath,
			SkipHTTPS: *dev,
		},
		SignedRequestMaxAge: signedRequestMaxAge,
	}
	httpServer := &http.Server{
		Addr:    *addr,
		Handler: webHandler,
	}
	hdConfig := &httpdown.HTTP{
		StopTimeout: 9 * time.Second, // heroku provides 10 seconds to terminate
	}

	if err := httpdown.ListenAndServe(httpServer, hdConfig); err != nil {
		logger.Fatal(err)
	}
}
Example #6
0
func TestNothingToDo(t *testing.T) {
	const name = "TestNothingToDo"
	s := flag.NewFlagSet(name, flag.PanicOnError)
	s.String(named(name, "foo"), "", "")
	ensure.Nil(t, flagenv.ParseSet(name, s))
}