github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/app/view.go (about)

     1  package app
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/fragmenta/assets"
     8  	"github.com/fragmenta/server/config"
     9  	"github.com/fragmenta/server/log"
    10  	"github.com/fragmenta/view"
    11  
    12  	"github.com/bitcubate/cryptojournal/src/lib/helpers"
    13  )
    14  
    15  // SetupAssets compiles or copies our assets from src into the public assets folder.
    16  func SetupAssets() {
    17  	defer log.Time(time.Now(), log.V{"msg": "Finished loading assets"})
    18  
    19  	// Compilation of assets is done on deploy
    20  	// We just load them here
    21  	assetsCompiled := config.GetBool("assets_compiled")
    22  
    23  	// Init the pkg global for use in ServeAssets
    24  	appAssets = assets.New(assetsCompiled)
    25  
    26  	// Load asset details from json file on each run
    27  	if config.Production() {
    28  		err := appAssets.Load()
    29  		if err != nil {
    30  			log.Info(log.V{"msg": "Compiling Asssets"})
    31  			err := appAssets.Compile("src", "public")
    32  			if err != nil {
    33  				log.Fatal(log.V{"a": "unable to compile assets", "error": err})
    34  				os.Exit(1)
    35  			}
    36  		}
    37  	} else {
    38  		log.Info(log.V{"msg": "Compiling Asssets in dev mode"})
    39  		err := appAssets.Compile("src", "public")
    40  		if err != nil {
    41  			log.Fatal(log.V{"a": "unable to compile assets", "error": err})
    42  		}
    43  	}
    44  
    45  	// Set up helpers which are aware of fingerprinted assets
    46  	// These behave differently depending on the compile flag above
    47  	// when compile is set to no, they use precompiled assets
    48  	// otherwise they serve all files in a group separately
    49  	view.Helpers["style"] = appAssets.StyleLink
    50  	view.Helpers["script"] = appAssets.ScriptLink
    51  
    52  }
    53  
    54  // SetupView sets up the view package by loadind templates.
    55  func SetupView() {
    56  	defer log.Time(time.Now(), log.V{"msg": "Finished loading templates"})
    57  
    58  	view.Helpers["markup"] = helpers.Markup
    59  	view.Helpers["timeago"] = helpers.TimeAgo
    60  	view.Helpers["root_url"] = helpers.RootURL
    61  	view.Helpers["getimg"] = helpers.GetImg
    62  
    63  
    64  	view.Production = config.Production()
    65  	err := view.LoadTemplates()
    66  	if err != nil {
    67  		//	server.Fatalf("Error reading templates %s", err)
    68  		log.Fatal(log.V{"msg": "unable to read templates", "error": err})
    69  		os.Exit(1)
    70  	}
    71  
    72  }