示例#1
0
// NewMockDB creates a new mock database
func NewMockDB() error {
	var err error
	db, err := gorm.Open("sqlite3", fmt.Sprintf("/tmp/test-%s-%s.db", time.Now().String(), string(rand.Int())))
	db.SetLogger(log.New(os.Stdout, "", 0))
	db.LogMode(true)

	models.DB = db
	models.Migrate()

	return err
}
示例#2
0
func main() {
	flag.Parse()
	listen := fmt.Sprintf("%s:%d", *host, *port)

	lo, ll, err := logger.StringToFlag(*logOutput, *logLevel)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	logger.New(lo, ll)

	if *db == "" {
		logger.Error.Println("A db connection string (flag --db='') is required.\n" + dbConnectionHelp)
		os.Exit(1)
	}

	if err = models.New(*db); err != nil {
		logger.Error.Println(err)
		os.Exit(1)
	}

	if *migrate {
		logger.Info.Println("Migrating Database.")
		models.Migrate()
		os.Exit(0)
	}

	r := mux.NewRouter()
	r.StrictSlash(true)
	auth.New(r)

	http.Handle("/", r)
	logger.Info.Printf("Chatterbox listening on %s.\n", listen)
	http.ListenAndServe(listen, nil)
}