func InitLogger(m *martini.ClassicMartini) *os.File { f, err := os.OpenFile("logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } m.Map(log.New(f, "[martini]", log.LstdFlags)) return f }
func initMySQL(m *martini.ClassicMartini) *sqlx.DB { db, err := sqlx.Connect("mysql", "test:test@tcp(127.0.0.1:3306)/instagram") if err != nil { log.Fatalln(err) } m.Map(db) return db }
func mid(m *martini.ClassicMartini) { //载入config文件信息 conf := goini.SetConfig("./config/conf.ini") //port := conf.GetValue("route", "port") //m.RunOnAddr(":" + port) db := helper.NewDb() m.Map(conf) m.Map(db) //过滤器handle m.Use(func(c martini.Context, log *log.Logger) { log.Println("before a request") c.Next() log.Println("after a request") }) }
func main() { var ( err error cmdConfig *config.CommandLineConfiguration cfg *config.Config m *martini.ClassicMartini log *logging.Logger ) cmdConfig, err = config.GetCommandLineConfiguration() if err != nil { panic(err) } cfg, err = config.GetConfig(cmdConfig.ConfigFilePath) if err != nil { panic(err) } defer cfg.Logger.LogFile.Close() log, err = logger.GetLogger(&cfg.Logger) if err != nil { panic(err) } session, err := db.GetSession(&cfg.DatabaseConnectOpts) if err != nil { panic(err) } m = martini.Classic() m.Map(log) m.Map(&cfg.WebSocketConfig) m.Map(&cfg.HttpServer) m.Map(&cfg) m.Map(session) m.Use(render.Renderer(render.Options{ Layout: "base", })) routing.Configure(m) log.Info("Listening") m.RunOnAddr(cfg.ListenHost + ":" + cfg.ListenPort) log.Info("Listening") }