コード例 #1
0
ファイル: admin.go プロジェクト: hypertornado/prago
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
ファイル: admin.go プロジェクト: hypertornado/prago
func (a *Admin) bindAdminCommand(app *prago.App) error {
	adminCommand := app.CreateCommand("admin", "Admin tasks (migrate|drop|thumbnails)")

	adminSubcommand := adminCommand.Arg("admincommand", "").Required().String()

	app.AddCommand(adminCommand, func(app *prago.App) error {
		switch *adminSubcommand {
		case "migrate":
			println("Migrating database")
			err := a.Migrate(true)
			if err == nil {
				println("Migrate done")
			}
			return err
		case "drop":
			if utils.ConsoleQuestion("Really want to drop table?") {
				println("Dropping table")
				return a.UnsafeDropTables()
			}
			return nil
		case "thumbnails":
			println("Updating thumbnails")
			return updateFiles(a)
		default:
			println("unknown admin subcommand " + *adminSubcommand)
		}
		return nil
	})

	return nil
}
コード例 #3
0
ファイル: sessions.go プロジェクト: hypertornado/prago
//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
}
コード例 #4
0
ファイル: development.go プロジェクト: hypertornado/prago
func (m MiddlewareDevelopment) Init(app *prago.App) error {
	app.RecoveryFunc = DevelopmentRecovery

	devCommand := app.CreateCommand("dev", "Development")
	portFlag := devCommand.Flag("port", "server port").Short('p').Default("8585").Int()
	developmentMode := devCommand.Flag("development", "Is in development mode").Default("t").Short('d').Bool()

	app.AddCommand(devCommand, func(app *prago.App) error {
		for _, v := range m.Settings.Less {
			go developmentLess(v.SourceDir, v.Target)
		}

		for _, v := range m.Settings.TypeScript {
			go developmentTypescript(v)
		}

		return app.ListenAndServe(*portFlag, *developmentMode)
	})
	return nil
}
コード例 #5
0
ファイル: mysql.go プロジェクト: hypertornado/prago
//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
}
コード例 #6
0
ファイル: admin.go プロジェクト: hypertornado/prago
func bindDBBackupCron(app *prago.App) {
	app.AddCronTask("backup db", func() {
		err := extensions.BackupApp(app)
		if err != nil {
			app.Log().Error("Error while creating backup:", err)
		}
	}, func(t time.Time) time.Time {
		return t.AddDate(0, 0, 1)
	})

	app.AddCronTask("remove old backups", func() {
		app.Log().Println("Removing old backups")
		deadline := time.Now().AddDate(0, 0, -7)
		backupPath := app.DotPath() + "/backups"
		files, err := ioutil.ReadDir(backupPath)
		if err != nil {
			app.Log().Println("error while removing old backups:", err)
			return
		}

		for _, file := range files {
			if file.ModTime().Before(deadline) {
				removePath := backupPath + "/" + file.Name()
				err := os.RemoveAll(removePath)
				if err != nil {
					app.Log().Println("Error while removing old backup file:", err)
				}
			}
		}
		app.Log().Println("Old backups removed")
	}, func(t time.Time) time.Time {
		return t.Add(1 * time.Hour)
	})
}
コード例 #7
0
ファイル: admin.go プロジェクト: hypertornado/prago
//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
}