示例#1
0
文件: paste.go 项目: husio/apps
func main() {
	log.SetFlags(log.Lshortfile)

	conf := struct {
		GithubKey      string `envconf:",required"`
		GithubSecret   string `envconf:",required"`
		HTTP           string
		Postgres       string
		Statics        string `envconf:",required"`
		Templates      string `envconf:",required"`
		TemplatesCache bool
		Schema         string
	}{
		HTTP:           "localhost:8000",
		Postgres:       "user=paste password=paste dbname=paste sslmode=disable",
		Schema:         "schema.sql",
		TemplatesCache: false,
	}
	envconf.Must(envconf.LoadEnv(&conf))

	tmpl.MustLoadTemplates(conf.Templates, conf.TemplatesCache)

	ctx := context.Background()

	ctx = auth.WithOAuth(ctx, map[string]*oauth2.Config{
		"github": &oauth2.Config{
			ClientID:     conf.GithubKey,
			ClientSecret: conf.GithubSecret,
			Scopes:       []string{},
			Endpoint:     oauth2gh.Endpoint,
		},
	})

	statics = http.StripPrefix("/static", http.FileServer(http.Dir(conf.Statics)))

	ctx = web.WithRouter(ctx, router)
	ctx = cache.WithLocalCache(ctx, 1000)

	if db, err := sql.Open("postgres", conf.Postgres); err != nil {
		log.Fatalf("cannot connect to PostgreSQL: %s", err)
	} else {
		ctx = pg.WithDB(ctx, db)
		if conf.Schema != "" {
			log.Printf("loading schema from %q", conf.Schema)
			pg.MustLoadSchema(db, conf.Schema)
		}
	}

	app := &application{
		ctx: ctx,
		rt:  router,
	}
	log.Printf("running HTTP server: %s", conf.HTTP)
	if err := http.ListenAndServe(conf.HTTP, app); err != nil {
		log.Fatalf("HTTP server error: %s", err)
	}
}
示例#2
0
文件: bb.go 项目: husio/apps
func main() {
	conf := struct {
		HTTP     string
		Postgres string
	}{
		HTTP:     "localhost:8000",
		Postgres: "host=localhost port=5432 user=postgres dbname=bb sslmode=disable",
	}
	envconf.Must(envconf.LoadEnv(&conf))

	ctx := context.Background()

	ctx = auth.WithOAuth(ctx, map[string]*oauth2.Config{
		"google": &oauth2.Config{
			ClientID:     "352914691292-2h70272sb408r3vibe4jm4egote804ka.apps.googleusercontent.com",
			ClientSecret: "L_bgOHLCgNYL-3KG8a5u99mF",
			RedirectURL:  "http://bb.example.com:8000/login/success",
			Scopes: []string{
				"https://www.googleapis.com/auth/userinfo.profile",
				"https://www.googleapis.com/auth/userinfo.email",
			},
			Endpoint: oauth2google.Endpoint,
		},
	})

	ctx = cache.WithLocalCache(ctx, 1000)

	db, err := sql.Open("postgres", conf.Postgres)
	if err != nil {
		log.Fatal("cannot open database", "error", err.Error())
	}
	defer db.Close()
	ctx = pg.WithDB(ctx, db)
	go func() {
		if err := db.Ping(); err != nil {
			log.Error("cannot ping database", "error", err.Error())
		}
	}()

	app := bb.NewApp(ctx)
	log.Debug("running HTTP server", "address", conf.HTTP)
	if err := http.ListenAndServe(conf.HTTP, app); err != nil {
		log.Error("HTTP server error", "error", err.Error())
	}
}