Beispiel #1
0
// Init starts Fxh.Go application preparation.
// Load models and plugins, update views.
func Init() {

	// init storage
	model.Init(VERSION)

	// load all data
	model.All()

	// init plugin
	plugin.Init()

	// update plugin handlers
	plugin.Update(App)

	App.View().FuncMap["DateInt64"] = utils.DateInt64
	App.View().FuncMap["DateString"] = utils.DateString
	App.View().FuncMap["DateTime"] = utils.DateTime
	App.View().FuncMap["Now"] = utils.Now
	App.View().FuncMap["Html2str"] = utils.Html2str
	App.View().FuncMap["FileSize"] = utils.FileSize
	App.View().FuncMap["Setting"] = model.GetSetting
	App.View().FuncMap["Navigator"] = model.GetNavigators
	App.View().FuncMap["Md2html"] = utils.Markdown2HtmlTemplate
	App.View().IsCache = (model.GetSetting("theme_cache") == "true")

	println("app version @ " + strconv.Itoa(model.GetVersion().Version))
}
Beispiel #2
0
func CheckUpgrade(v int, print bool) bool {
	model.Init(v)
	appV := model.GetVersion()
	b := v > appV.Version
	if b && print {
		println("app version @ " + strconv.Itoa(v) + " is ahead of current version @ " + strconv.Itoa(appV.Version) + " , please run 'GoBlog upgrade'")
	}
	return b
}
Beispiel #3
0
func Init() {

	// init some settings
	if App.Get("upload_files") == "" {
		App.Set("upload_files", uploadFileSuffix)
	}
	if App.Get("upload_size") == "" {
		App.Set("upload_size", 1024*1024*10)
	}
	if App.Get("upload_dir") == "" {
		App.Set("upload_dir", "static/upload")
		os.MkdirAll("static/upload", os.ModePerm)
	}

	// init temp dir
	if App.Get("log_dir") == "" {
		App.Set("log_dir", "tmp/log")
		os.MkdirAll("tmp/log", os.ModePerm)
	}

	// set static files handler
	if App.Get("static_files") != "" {
		staticFileSuffix = App.Get("static_files")
	}
	App.Static(func(context *GoInk.Context) {
		static := App.Config().StringOr("static_dir", "static")
		url := strings.TrimPrefix(context.Url, "/")
		if url == "favicon.ico" {
			url = path.Join(static, url)
		}
		if !strings.HasPrefix(url, static) {
			return
		}
		if !strings.Contains(staticFileSuffix, context.Ext) {
			context.Status = 403
			context.End()
			return
		}
		f, e := os.Stat(url)
		if e == nil {
			if f.IsDir() {
				context.Status = 403
				context.End()
				return
			}
		}
		/*_, e := os.Stat(url)
		if e != nil {
			context.Throw(404)
			return
		}*/
		http.ServeFile(context.Response, context.Request, url)
		context.IsEnd = true
	})

	// set recover handler
	App.Recover(func(context *GoInk.Context) {
		go LogError(append(append(context.Body, []byte("\n")...), debug.Stack()...))
		if App.View().Has("error/error.html") {
			context.Layout("")
			context.Render("error/error", map[string]interface{}{
				"error":   string(context.Body),
				"stack":   string(debug.Stack()),
				"context": context,
			})
		} else {
			context.Body = append([]byte("<pre>"), context.Body...)
			context.Body = append(context.Body, []byte("\n")...)
			context.Body = append(context.Body, debug.Stack()...)
			context.Body = append(context.Body, []byte("</pre>")...)
		}
		context.End()
	})

	// set not found handler
	App.NotFound(func(context *GoInk.Context) {
		if App.View().Has("error/notfound.html") {
			context.Layout("")
			context.Render("error/notfound", map[string]interface{}{
				"context": context,
			})
		}
		context.End()
	})

	// init storage
	model.Init()

	// set version
	model.SetVersion(VERSION)
}