本文整理匯總了Golang中github.com/hypertornado/prago.App.Data方法的典型用法代碼示例。如果您正苦於以下問題:Golang App.Data方法的具體用法?Golang App.Data怎麽用?Golang App.Data使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hypertornado/prago.App
的用法示例。
在下文中一共展示了App.Data方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: initTemplates
func (a *Admin) initTemplates(app *prago.App) error {
templates := app.Data()["templates"].(*template.Template)
if templates == nil {
return errors.New("Templates not initialized")
}
app.AddTemplateFunction("tmpl", func(templateName string, x interface{}) (template.HTML, error) {
var buf bytes.Buffer
err := templates.ExecuteTemplate(&buf, templateName, x)
return template.HTML(buf.String()), err
})
app.AddTemplateFunction("markdown", func(text string) template.HTML {
return template.HTML(markdown.New(markdown.Breaks(true)).RenderToString([]byte(text)))
})
app.AddTemplateFunction("message", func(language, id string) template.HTML {
return template.HTML(messages.Messages.Get(language, id))
})
app.AddTemplateFunction("thumb", func(ids string) string {
for _, v := range strings.Split(ids, ",") {
var image File
err := a.Query().WhereIs("uid", v).Get(&image)
if err == nil && image.isImage() {
return image.GetSmall()
}
}
return ""
})
return nil
}
示例2: Init
//Init sessions middleware
func (s *Sessions) Init(app *prago.App) error {
s.cookieStore = sessions.NewCookieStore([]byte(app.Config.GetString("random")))
s.app = app
app.Data()["sessionStore"] = s
app.MainController().AddAroundAction(s.around)
return nil
}
示例3: Init
//Init Mysql middleware
func (m *Mysql) Init(app *prago.App) error {
dbUser := app.Config.GetString("dbUser")
dbPassword := app.Config.GetString("dbPassword")
dbName := app.Config.GetString("dbName")
db, err := ConnectMysql(dbUser, dbPassword, dbName)
if err != nil {
return err
}
app.Data()["db"] = db
dumpCommand := app.CreateCommand("dump", "Dump database")
app.AddCommand(dumpCommand, func(app *prago.App) error {
cmd := exec.Command("mysqldump", "-u"+dbUser, "-p"+dbPassword, dbName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
panic(err)
}
return nil
})
return nil
}
示例4: Init
//Init admin middleware
func (a *Admin) Init(app *prago.App) error {
a.App = app
a.db = app.Data()["db"].(*sql.DB)
a.AdminAccessController = app.MainController().SubController()
a.AdminController = a.AdminAccessController.SubController()
googleApiKey := app.Config.GetStringWithFallback("google", "")
a.AdminController.AddBeforeAction(func(request prago.Request) {
request.SetData("google", googleApiKey)
})
bindDBBackupCron(app)
bindMarkdownAPI(a)
bindListResourceAPI(a)
var err error
a.sendgridClient = sendgrid.NewSendGridClientWithApiKey(app.Config.GetStringWithFallback("sendgridApi", ""))
a.noReplyEmail = app.Config.GetStringWithFallback("noReplyEmail", "")
err = a.bindAdminCommand(app)
if err != nil {
return err
}
err = a.initTemplates(app)
if err != nil {
return err
}
err = app.LoadTemplateFromString(adminTemplates)
if err != nil {
panic(err)
}
a.AdminController.AddAroundAction(func(request prago.Request, next func()) {
request.SetData("admin_yield", "admin_home")
session := request.GetData("session").(*sessions.Session)
userID, ok := session.Values["user_id"].(int64)
if !ok {
prago.Redirect(request, a.Prefix+"/user/login")
return
}
var user User
err := a.Query().WhereIs("id", userID).Get(&user)
if err != nil {
prago.Redirect(request, a.Prefix+"/user/login")
return
}
randomness := app.Config.GetString("random")
request.SetData("_csrfToken", user.CSRFToken(randomness))
request.SetData("currentuser", &user)
request.SetData("locale", GetLocale(request))
request.SetData("appName", a.AppName)
request.SetData("appCode", request.App().Data()["appName"].(string))
request.SetData("appVersion", request.App().Data()["version"].(string))
request.SetData("admin_header", a.adminHeaderData(request))
next()
})
a.AdminController.Get(a.Prefix, func(request prago.Request) {
prago.Render(request, 200, "admin_layout")
})
a.AdminController.Get(a.Prefix+"/_stats", stats)
a.AdminController.Get(a.Prefix+"/_static/admin.js", func(request prago.Request) {
request.Response().Header().Set("Content-type", "text/javascript")
request.Response().WriteHeader(200)
request.Response().Write([]byte(adminJS))
request.SetProcessed()
})
a.App.MainController().Get(a.Prefix+"/_static/admin.css", func(request prago.Request) {
request.Response().Header().Set("Content-type", "text/css; charset=utf-8")
request.Response().WriteHeader(200)
request.Response().Write([]byte(adminCSS))
request.SetProcessed()
})
for i := range a.Resources {
resource := a.Resources[i]
err = a.initResource(resource)
if err != nil {
return err
}
}
a.AdminController.Get(a.Prefix+"/*", func(request prago.Request) {
render404(request)
})
//.........這裏部分代碼省略.........