Пример #1
0
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
0
//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
0
//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
0
//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
}