func themePath() string {
	if config.Get("theme") == "" {
		return ""
	}
	return filepath.Join("themes", config.Get("theme"), "src")
}
themePath returns the path to the theme src dir (if a theme is chosen)
package app
import (
	"os"
	"path/filepath"
	"strings"
	"time"
	"github.com/fragmenta/assets"
	"github.com/fragmenta/query"
	"github.com/fragmenta/server/config"
	"github.com/fragmenta/server/log"
	"github.com/fragmenta/view"
	"github.com/fragmenta/fragmenta-cms/src/lib/mail"
	"github.com/fragmenta/fragmenta-cms/src/lib/mail/adapters/sendgrid"
)
var appAssets *assets.Collection
func Setup() {
	err := SetupLog()
	if err != nil {
		println("failed to set up logs %s", err)
		os.Exit(1)
	}
	msg := "Starting server"
	if config.Production() {
		msg = msg + " in production"
	}
	log.Info(log.Values{"msg": msg, "port": config.Get("port")})
	defer log.Time(time.Now(), log.Values{"msg": "Finished loading server"})
	SetupMail()
	SetupAssets()
	SetupView()
	SetupDatabase()
	SetupAuth()
	SetupRoutes()
}
func SetupDatabase() {
	defer log.Time(time.Now(), log.V{"msg": "Finished opening database", "db": config.Get("db"), "user": config.Get("db_user")})
	options := map[string]string{
		"adapter":  config.Get("db_adapter"),
		"user":     config.Get("db_user"),
		"password": config.Get("db_pass"),
		"db":       config.Get("db"),
	}
	if len(config.Get("db_host")) > 0 {
		options["host"] = config.Get("db_host")
	}
	if len(config.Get("db_port")) > 0 {
		options["port"] = config.Get("db_port")
	}
	if len(config.Get("db_params")) > 0 {
		options["params"] = config.Get("db_params")
	}
	err := query.OpenDatabase(options)
	if err != nil {
		log.Fatal(log.V{"msg": "unable to read database", "db": config.Get("db"), "error": err})
		os.Exit(1)
	}
}
func SetupLog() error {
	logger, err := log.NewStdErr(log.PrefixDateTime)
	if err != nil {
		return err
	}
	log.Add(logger)
	fileLog, err := log.NewFile(config.Get("log"))
	if err != nil {
		return err
	}
	log.Add(fileLog)
	return nil
}
func SetupMail() {
	mail.Production = config.Production()
	mail.Service = sendgrid.New(config.Get("mail_from"), config.Get("mail_secret"))
}
func SetupAssets() {
	defer log.Time(time.Now(), log.V{"msg": "Finished loading assets"})
	assetsCompiled := config.GetBool("assets_compiled")
	appAssets = assets.New(assetsCompiled)
	load := true
	if config.Production() {
		load = (appAssets.Load() != nil)
	}
	if load {
		log.Info(log.V{"msg": "Compiling Asssets"})
		err := appAssets.Compile("src", "public")
		if err != nil {
			log.Fatal(log.V{"a": "unable to compile assets", "error": err})
			os.Exit(1)
		}
		if themePath() != "" {
			err = appAssets.Compile(themePath(), "public")
			if err != nil {
				log.Fatal(log.V{"a": "unable to compile assets", "error": err})
				os.Exit(1)
			}
		}
	}
}
func SetupView() {
	defer log.Time(time.Now(), log.V{"msg": "Finished loading templates"})
	view.Production = config.Production()
	paths := []string{"src"}
	if themePath() != "" {
		log.Log(log.V{"msg": "loading templates for theme", "theme": config.Get("theme")})
		paths = append(paths, themePath())
	}
	err := view.LoadTemplatesAtPaths(paths, helperFuncs())
	if err != nil {
		log.Fatal(log.V{"msg": "unable to read templates", "error": err})
		os.Exit(1)
	}
}
func helperFuncs() map[string]interface{} {
	helpers := view.DefaultHelpers()
	helpers["style"] = appAssets.StyleLink
	helpers["script"] = appAssets.ScriptLink
	rootURL := config.Get("root_url")
	host, err := os.Hostname()
	if err == nil && strings.HasSuffix(host, ".local") {
		rootURL = "http://localhost:3000"
	}
	helpers["root_url"] = func() string {
		return rootURL
	}
	return helpers
}