func LoadConfig() { redisServer := beego.AppConfig.String("redis_resource") redisServer = "{\"conn\":\"" + redisServer + "\"}" store, err := cache.NewCache("redis", redisServer) if err != nil { log.Panic("缓存出错" + err.Error()) } Captcha = captcha.NewWithFilter("/captcha/", store) driverName := beego.AppConfig.String("driverName") dataSource := beego.AppConfig.String("dataSource") maxIdle, _ := beego.AppConfig.Int("maxIdle") maxOpen, _ := beego.AppConfig.Int("maxOpen") orm.RegisterDriver("mysql", orm.DR_MySQL) // set default database err = orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen) if err != nil { beego.Error(err) } orm.RunCommand() err = orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) } SocialAuthInit() config.InitConfig() }
func main() { setting.Initialize() beego.SetLogFuncCall(true) beego.SetViewsPath("views") if setting.IsProMode { beego.Info(setting.PostgresMigrateConnection) //auto migrate db //todo: we may want to disable this later //dbMigrate() } else { beego.Info("Develment mode enabled") } dbMigrate() beego.Info(beego.BConfig.AppName, setting.APP_VER, setting.AppUrl) testOnly() //todo:remove this //dev directory listing if !setting.IsProMode { beego.SetStaticPath("/assets", "static/dist/assets") beego.BConfig.WebConfig.DirectoryIndex = true } orm.RegisterDriver("postgres", orm.DRPostgres) orm.RegisterDataBase("default", "postgres", setting.PostgresConnection) services.Register() orm.RunCommand() orm.Debug = true controllers.RegisterControllers() if beego.BConfig.RunMode == "dev" { // beego.Router("/test/:tmpl(mail/.*)", new(base.TestRouter)) } beego.Run() }
func main() { tracelog.StartFile(tracelog.LevelTrace, "logs", 1) tracelog.Started("main", "Initializing Postgres") var confName string = os.Getenv("BOOKS_CONF") if len(confName) == 0 { confName = "default" } s := []string{"conf/db/", confName, ".json"} jsonconf, err := config.NewConfig("json", strings.Join(s, "")) if err != nil { tracelog.Errorf(err, "main", "config.NewConfig", "Failed to find config", strings.Join(s, "")) } var User string = jsonconf.String("User") var Pass string = jsonconf.String("Pass") var DBName string = jsonconf.String("DBName") var buffer bytes.Buffer buffer.WriteString("user="******" password="******" dbname=") buffer.WriteString(DBName) s2 := []string{"user="******" password="******" dbname=", DBName, " sslmode=disable"} orm.RegisterDriver("postgres", orm.DR_Postgres) orm.RegisterDataBase("default", "postgres", strings.Join(s2, "")) beego.SetStaticPath("/images", "static/images") beego.SetStaticPath("/css", "static/css") beego.SetStaticPath("/js", "static/js") beego.SetStaticPath("/fonts", "static/fonts") beego.SetStaticPath("/partials", "static/partials") beego.EnableAdmin = true orm.RunCommand() beego.Run() }
func init() { orm.RegisterDriver("mysql", orm.DR_MySQL) orm.RegisterDataBase("default", "mysql", "root:pass@/pets-clinic?charset=utf8") orm.RegisterModel(new(Pet)) orm.RegisterModel(new(Visit)) orm.RunCommand() orm.Debug = true }
func (command *BeegoOrmSyncDbCommand) Execute(context *console.Context) error { //TODO register database config stuff. os.Args = []string{ os.Args[0], "orm", "syncdb", } orm.RunCommand() return nil }
func main() { orm.RunCommand() if beego.RunMode == "dev" { beego.DirectoryIndex = true beego.StaticDir["/swagger"] = "swagger" } beego.Run() }
func main() { beego.SetLogger("file", `{"filename":"logs/beego_bootstrap_api.log"}`) orm.RunCommand() if beego.BConfig.RunMode == "dev" { orm.Debug = true file, _ := os.OpenFile("logs/orm.log", os.O_APPEND|os.O_WRONLY, 0600) orm.DebugLog = orm.NewLog(file) } beego.Run() }
func main() { orm.Debug = true orm.RunCommand() err := orm.RunSyncdb("default", false, true) if err != nil { beego.Error(err) } beego.Run() }
func main() { if len(os.Args) > 1 { // fmt.Println(os.Args[1]) if os.Args[1] == "run" { runWeb() } else if os.Args[1] == "orm" { orm.RunCommand() } } }
func RegisterDB() { driverName := beego.AppConfig.String("driverName") dataSource := beego.AppConfig.String("dataSource") maxIdle, _ := beego.AppConfig.Int("maxIdle") maxOpen, _ := beego.AppConfig.Int("maxOpen") // 注册驱动 orm.RegisterDriver("mysql", orm.DR_MySQL) // 注册默认数据库 // set default database err := orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen) orm.RunCommand() //不自动建表 err = orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) } }
func main() { // Beego related orm.RegisterDataBase("default", "sqlite3", "db.sqlite") orm.RegisterModel(new(Category)) orm.RegisterModel(new(BlogPost)) orm.RunCommand() // Set up admin a, err := admin.New("/admin", "sqlite3", "db.sqlite") if err != nil { panic(err) } // Override settings as needed a.Title = "Example admin" a.NameTransform = snakeString // Optional, but needed here to be compatible with Beego ORM a.User("admin", "example") // Username / password to log in. group, err := a.Group("Blog") if err != nil { panic(err) } group.RegisterModel(new(Category)) group.RegisterModel(new(BlogPost)) // Get a http.Handler to attach to your router/mux. adminHandler, err := a.Handler() if err != nil { panic(err) } // Serve admin. router := http.NewServeMux() router.Handle("/admin/", adminHandler) router.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { rw.Write([]byte("Nothing to see here. Visit /admin/ instead.")) }) http.ListenAndServe(":8000", router) }
// LoadConfig loads configuration file. func LoadConfig() *goconfig.ConfigFile { var err error if fh, _ := os.OpenFile(AppConfPath, os.O_RDONLY|os.O_CREATE, 0600); fh != nil { fh.Close() } // Load configuration, set app version and log level. Cfg, err = goconfig.LoadConfigFile(GlobalConfPath) if Cfg == nil { Cfg, err = goconfig.LoadConfigFile(AppConfPath) if err != nil { fmt.Println("Fail to load configuration file: " + err.Error()) os.Exit(2) } } else { Cfg.AppendFiles(AppConfPath) } Cfg.BlockMode = false // set time zone of wetalk system TimeZone = Cfg.MustValue("app", "time_zone", "UTC") if _, err := time.LoadLocation(TimeZone); err == nil { os.Setenv("TZ", TimeZone) } else { fmt.Println("Wrong time_zone: " + TimeZone + " " + err.Error()) os.Exit(2) } // Trim 4th part. AppVer = strings.Join(strings.Split(APP_VER, ".")[:3], ".") beego.RunMode = Cfg.MustValue("app", "run_mode") beego.HttpPort = Cfg.MustInt("app", "http_port") IsProMode = beego.RunMode == "pro" if IsProMode { beego.SetLevel(beego.LevelInfo) } // cache system Cache, err = cache.NewCache("memory", `{"interval":360}`) Captcha = captcha.NewCaptcha("/captcha/", Cache) Captcha.FieldIdName = "CaptchaId" Captcha.FieldCaptchaName = "Captcha" // session settings beego.SessionOn = true beego.SessionProvider = Cfg.MustValue("session", "session_provider", "file") beego.SessionSavePath = Cfg.MustValue("session", "session_path", "sessions") beego.SessionName = Cfg.MustValue("session", "session_name", "wetalk_sess") beego.SessionCookieLifeTime = Cfg.MustInt("session", "session_life_time", 0) beego.SessionGCMaxLifetime = Cfg.MustInt64("session", "session_gc_time", 86400) beego.EnableXSRF = true // xsrf token expire time beego.XSRFExpire = 86400 * 365 driverName := Cfg.MustValue("orm", "driver_name", "mysql") dataSource := Cfg.MustValue("orm", "data_source", "root:root@/wetalk?charset=utf8&loc=UTC") maxIdle := Cfg.MustInt("orm", "max_idle_conn", 30) maxOpen := Cfg.MustInt("orm", "max_open_conn", 50) // set default database err = orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen) if err != nil { beego.Error(err) } orm.RunCommand() err = orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) } reloadConfig() if SphinxEnabled { // for search config SphinxHost = Cfg.MustValue("search", "sphinx_host", "127.0.0.1:9306") SphinxMaxConn = Cfg.MustInt("search", "sphinx_max_conn", 5) orm.RegisterDriver("sphinx", orm.DR_MySQL) } social.DefaultAppUrl = AppUrl // OAuth var clientId, secret string clientId = Cfg.MustValue("oauth", "github_client_id", "your_client_id") secret = Cfg.MustValue("oauth", "github_client_secret", "your_client_secret") GithubAuth = apps.NewGithub(clientId, secret) clientId = Cfg.MustValue("oauth", "google_client_id", "your_client_id") secret = Cfg.MustValue("oauth", "google_client_secret", "your_client_secret") GoogleAuth = apps.NewGoogle(clientId, secret) err = social.RegisterProvider(GithubAuth) if err != nil { beego.Error(err) } err = social.RegisterProvider(GoogleAuth) if err != nil { beego.Error(err) } settingLocales() settingCompress() configWatcher() return Cfg }
// We have to call a initialize function manully // because we use `bee bale` to pack static resources // and we cannot make sure that which init() execute first. func initialize() { var err error // Load configuration, set app version and log level. utils.Cfg, err = utils.LoadConfig("conf/app.ini") if err != nil { panic("Fail to load configuration file: " + err.Error()) } err = i18n.SetMessage("conf/message.ini") if err != nil { panic("Fail to set message file: " + err.Error()) } // Trim 4th part. utils.AppVer = strings.Join(strings.Split(APP_VER, ".")[:3], ".") beego.AppName = utils.Cfg.MustValue("beego", "app_name") beego.RunMode = utils.Cfg.MustValue("beego", "run_mode") beego.HttpPort = utils.Cfg.MustInt("beego", "http_port_"+beego.RunMode) utils.AppName = beego.AppName utils.AppUrl = utils.Cfg.MustValue("app", "app_url") utils.AppDescription = utils.Cfg.MustValue("app", "description") utils.AppKeywords = utils.Cfg.MustValue("app", "keywords") utils.AppJsVer = utils.Cfg.MustValue("app", "js_ver") utils.AppCssVer = utils.Cfg.MustValue("app", "css_ver") utils.MailUser = utils.Cfg.MustValue("app", "mail_user") utils.MailFrom = utils.Cfg.MustValue("app", "mail_from") utils.SecretKey = utils.Cfg.MustValue("app", "secret_key") utils.ActiveCodeLives = utils.Cfg.MustInt("app", "acitve_code_live_days") utils.ResetPwdCodeLives = utils.Cfg.MustInt("app", "resetpwd_code_live_days") utils.IsBeta = utils.Cfg.MustBool("server", "beta") utils.IsProMode = beego.RunMode == "pro" if utils.IsProMode { beego.SetLevel(beego.LevelInfo) beego.Info("Product mode enabled") beego.Info(beego.AppName, APP_VER) } orm.Debug, _ = utils.Cfg.Bool("orm", "debug_log") driverName, _ := utils.Cfg.GetValue("orm", "driver_name") dataSource, _ := utils.Cfg.GetValue("orm", "data_source") maxIdle, _ := utils.Cfg.Int("orm", "max_idle_conn") // session settings beego.SessionOn = true beego.SessionProvider = utils.Cfg.MustValue("app", "session_provider") beego.SessionSavePath = utils.Cfg.MustValue("app", "session_path") beego.SessionName = utils.Cfg.MustValue("app", "session_name") // set mailer connect args mailer.MailHost = utils.Cfg.MustValue("mailer", "host") mailer.AuthUser = utils.Cfg.MustValue("mailer", "user") mailer.AuthPass = utils.Cfg.MustValue("mailer", "pass") // set default database orm.RegisterDataBase("default", driverName, dataSource, maxIdle) orm.RunSyncdb("default", false, true) orm.RunCommand() }
func main() { // Usage: go run main.go orm syncdb -force=1 orm.RunCommand() beego.Run() }
func main() { orm.RunCommand() // dev only beego.Info("Team-BlackCMS Version: ", APP_Version) beego.Run() }
// LoadConfig loads configuration file. func LoadConfig() *goconfig.ConfigFile { var err error if fh, _ := os.OpenFile(AppConfPath, os.O_RDONLY|os.O_CREATE, 0600); fh != nil { fh.Close() } if fh, _ := os.OpenFile(AppConfPath_MachineSpecific, os.O_RDONLY|os.O_CREATE, 0600); fh != nil { fh.Close() } // Load configuration, set app version and log level. Cfg_General, err = goconfig.LoadConfigFile(AppConfPath) Cfg_General.BlockMode = false if err != nil { panic("Failed to load configuration (general) file: " + err.Error()) } Cfg_MachineSpecific, err = goconfig.LoadConfigFile(AppConfPath_MachineSpecific) Cfg_MachineSpecific.BlockMode = false if err != nil { panic("Failed to load configuration (machine specific) file: " + err.Error()) } // Trim 4th part. AppVer = strings.Join(strings.Split(APP_VER, ".")[:3], ".") configWatcher() reloadConfig() reloadConfig_MachineSpecific() IsProMode = beego.RunMode == "pro" runSassCommand() //Must be after IsProMode if IsProMode { beego.SetLevel(beego.LevelInfo) } else { beego.SetLevel(beego.LevelDebug) } // cache system Cache, err = cache.NewCache("memory", `{"interval":360}`) // session settings beego.SessionOn = true beego.SessionProvider = Cfg_General.MustValue("app", "session_provider") beego.SessionSavePath = Cfg_General.MustValue("app", "session_path") beego.SessionName = Cfg_General.MustValue("app", "session_name") beego.EnableXSRF = true // xsrf token expire time beego.XSRFExpire = 86400 * 365 driverName := Cfg_General.MustValue("orm", "driver_name") dataSource := Cfg_General.MustValue("orm", "data_source") maxIdle := Cfg_General.MustInt("orm", "max_idle_conn") maxOpen := Cfg_General.MustInt("orm", "max_open_conn") orm.DefaultTimeLoc = time.UTC // set default database orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen) orm.RunCommand() /*Rather call via commandline, for help call: ./main orm syncdb -h Refer to: http://beego.me/docs/Models_Cmd err = orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) }*/ settingLocales() settingCompress() return Cfg_General }