示例#1
0
文件: main.go 项目: xqbumu/learn
func mainRun() {
	lf, err := openToAppend(logPath)
	if err != nil {
		log.Panic(err)
	}
	defer lf.Close()
	log.SetOutput(lf)

	googleRedirect := "http://domain.com/auth/login"
	if !isInVPC {
		googleRedirect = fmt.Sprintf("http://localhost%s/auth/login", port)
	}
	auth.Config.CookieSecret = []byte("YOUR_COOKIE_SECRET")
	auth.Config.LoginSuccessRedirect = "/main"
	auth.Config.CookieSecure = false

	rootContext := context.Background()

	mainRouter := http.NewServeMux()
	// func (mux *ServeMux) Handle(pattern string, handler Handler)
	mainRouter.Handle("/static/", http.StripPrefix(
		"/static/",
		http.FileServer(http.Dir("static")),
	))
	mainRouter.Handle("/", &ContextAdapter{
		ctx:     rootContext,
		handler: ContextHandlerFunc(handlerRoot),
	})
	mainRouter.Handle("/auth/login", auth.Google(
		googleClientID,
		googleClientSecret,
		googleRedirect,
	))
	mainRouter.Handle("/auth/logout", &ContextAdapter{
		ctx:     rootContext,
		handler: ContextHandlerFunc(handlerLogout),
	})
	mainRouter.Handle("/main", &ContextAdapter{
		ctx:     rootContext,
		handler: WithAuthentication(ContextHandlerFunc(handlerMain)),
	})
	mainRouter.Handle("/main/post_form_sentence", &ContextAdapter{
		ctx:     rootContext,
		handler: WithAuthentication(ContextHandlerFunc(handlerMainPostFormSentence)),
	})
	mainRouter.Handle("/main/get_form_sentence", &ContextAdapter{
		ctx:     rootContext,
		handler: WithAuthentication(ContextHandlerFunc(handlerMainGetFormSentence)),
	})
	mainRouter.Handle("/main/reset", &ContextAdapter{
		ctx:     rootContext,
		handler: WithAuthentication(ContextHandlerFunc(handlerMainReset)),
	})
	mainRouter.Handle("/photo", &ContextAdapter{
		ctx:     rootContext,
		handler: WithAuthentication(ContextHandlerFunc(handlerPhoto)),
	})
	mainRouter.Handle("/sendjson", &ContextAdapter{
		ctx:     rootContext,
		handler: ContextHandlerFunc(handlerSendJSON),
	})
	mainRouter.Handle("/sendgob", &ContextAdapter{
		ctx:     rootContext,
		handler: ContextHandlerFunc(handlerSendGOB),
	})
	mainRouter.Handle("/json", &ContextAdapter{
		ctx:     rootContext,
		handler: ContextHandlerFunc(handlerJSON),
	})
	mainRouter.Handle("/gob", &ContextAdapter{
		ctx:     rootContext,
		handler: ContextHandlerFunc(handlerGOB),
	})
	mainRouter.Handle("/db", &ContextAdapter{
		ctx:     rootContext,
		handler: WithDatabase(WithAuthentication(ContextHandlerFunc(handlerDB))),
	})

	stdlog.Println("Server started at http://localhost" + port)
	log.Infoln("Server started at http://localhost" + port)
	graceful.Run(port, 10*time.Second, WithLogrus(mainRouter))
}
示例#2
0
文件: main.go 项目: MEDIGO/laika
func main() {
	app := cli.NewApp()
	app.Author = "MEDIGO GmbH"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "port",
			Value:  "8000",
			Usage:  "Service port",
			EnvVar: "LAIKA_PORT",
		},
		cli.IntFlag{
			Name:   "timeout",
			Value:  10,
			Usage:  "Shutdown timeout",
			EnvVar: "LAIKA_TIMEOUT",
		},
		cli.StringFlag{
			Name:   "mysql-host",
			Value:  "mysql",
			Usage:  "MySQL host",
			EnvVar: "LAIKA_MYSQL_HOST",
		},
		cli.StringFlag{
			Name:   "mysql-port",
			Value:  "3306",
			Usage:  "MySQL port",
			EnvVar: "LAIKA_MYSQL_PORT",
		},
		cli.StringFlag{
			Name:   "mysql-username",
			Value:  "root",
			Usage:  "MySQL username",
			EnvVar: "LAIKA_MYSQL_USERNAME",
		},
		cli.StringFlag{
			Name:   "mysql-password",
			Value:  "root",
			Usage:  "MySQL password",
			EnvVar: "LAIKA_MYSQL_PASSWORD",
		},
		cli.StringFlag{
			Name:   "mysql-dbname",
			Value:  "laika",
			Usage:  "MySQL dbname",
			EnvVar: "LAIKA_MYSQL_DBNAME",
		},
		cli.StringFlag{
			Name:   "statsd-host",
			Value:  "localhost",
			Usage:  "Statsd host",
			EnvVar: "LAIKA_STATSD_HOST",
		},
		cli.StringFlag{
			Name:   "statsd-port",
			Value:  "8125",
			Usage:  "Statsd port",
			EnvVar: "LAIKA_STATSD_PORT",
		},
		cli.StringFlag{
			Name:   "root-username",
			Usage:  "Root username",
			EnvVar: "LAIKA_ROOT_USERNAME",
		},
		cli.StringFlag{
			Name:   "root-password",
			Usage:  "Root password",
			EnvVar: "LAIKA_ROOT_PASSWORD",
		},
		cli.StringFlag{
			Name:   "slack-webhook-url",
			Usage:  "Slack webhook URL",
			EnvVar: "LAIKA_SLACK_WEBHOOK_URL",
		},
	}
	app.Commands = []cli.Command{
		{
			Name:  "run",
			Usage: "Runs laika's feature flag service",
			Action: func(c *cli.Context) {
				store, err := store.NewMySQLStore(
					c.GlobalString("mysql-username"),
					c.GlobalString("mysql-password"),
					c.GlobalString("mysql-host"),
					c.GlobalString("mysql-port"),
					c.GlobalString("mysql-dbname"),
				)

				if err != nil {
					log.Fatal("Failed to create Store: ", err)
				}

				stats, err := statsd.New(c.GlobalString("statsd-host") + ":" + c.GlobalString("statsd-port"))
				if err != nil {
					log.Fatal("Failed to create Statsd client: ", err)
				}

				notifier := notifier.NewSlackNotifier(c.GlobalString("slack-webhook-url"))

				server, err := api.NewServer(api.ServerConfig{
					RootUsername: c.GlobalString("root-username"),
					RootPassword: c.GlobalString("root-password"),
					Store:        store,
					Stats:        stats,
					Notifier:     notifier,
				})
				if err != nil {
					log.Fatal("Failed to create server: ", err)

				}

				log.Info("Starting server on port ", c.GlobalString("port"))
				graceful.Run(":"+c.GlobalString("port"), time.Duration(c.Int("timeout"))*time.Second, server)
			},
		},
		{
			Name:  "migrate",
			Usage: "Migrates the store schema to the latest available version",
			Action: func(c *cli.Context) {
				store, err := store.NewMySQLStore(
					c.GlobalString("mysql-username"),
					c.GlobalString("mysql-password"),
					c.GlobalString("mysql-host"),
					c.GlobalString("mysql-port"),
					c.GlobalString("mysql-dbname"),
				)

				if err != nil {
					log.Fatal("Failed to create Store: ", err)
				}

				if err := store.Ping(); err != nil {
					log.Fatal("Failed to connect with store: ", err)
				}

				if err := store.Migrate(); err != nil {
					log.Fatal("Failed to migrate store schema: ", err)
				}

			},
		},
	}

	app.Run(os.Args)
}