//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 }
//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) }) return nil }