示例#1
0
文件: server.go 项目: mssola/todo
func main() {
	// Initialize app.
	lib.InitSession()
	app.InitDB()
	defer app.CloseDB()

	// Routing.
	n := negroni.Classic()
	r := route()
	n.UseHandler(r)

	port := fmt.Sprintf(":%v", app.EnvOrElse("TODO_PORT", "3000"))

	// Try to run on HTTPS.
	cert := os.Getenv("TODO_CERT_PATH")
	key := os.Getenv("TODO_KEY_PATH")
	if cert != "" && key != "" {
		log.Printf("Running on port %s", port)
		if err := http.ListenAndServeTLS(port, cert, key, n); err != nil {
			log.Fatalf("Could not start server: %v", err)
		}
		os.Exit(0)
	}

	// Falling back to normal HTTP.
	log.Printf("Warning: this server does not use a safe connection!")
	n.Run(port)
}
示例#2
0
// Use this in the end of every unit test.
func closeTestDB() {
	_ = app.Db.TruncateTables()
	app.CloseDB()
}