github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/app/app.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  
    11  	"github.com/bitcubate/cryptojournal/src/lib/mail"
    12  	"github.com/bitcubate/cryptojournal/src/lib/mail/adapters/sendgrid"
    13  )
    14  
    15  // appAssets holds a reference to our assets for use in asset setup
    16  var appAssets *assets.Collection
    17  
    18  // Setup sets up our application
    19  func Setup() {
    20  
    21  	// Setup log
    22  	err := SetupLog()
    23  	if err != nil {
    24  		println("failed to set up logs %s", err)
    25  		os.Exit(1)
    26  	}
    27  
    28  	// Log server startup
    29  	msg := "Starting server"
    30  	if config.Production() {
    31  		msg = msg + " in production"
    32  	}
    33  
    34  	log.Info(log.Values{"msg": msg, "port": config.Get("port")})
    35  	defer log.Time(time.Now(), log.Values{"msg": "Finished loading server"})
    36  
    37  	// Set up external service interfaces (twitter)
    38  	SetupServices()
    39  
    40  	// Set up our assets
    41  	SetupAssets()
    42  
    43  	// Setup our view templates
    44  	SetupView()
    45  
    46  	// Setup our database
    47  	SetupDatabase()
    48  
    49  	// Setup our authentication and authorisation
    50  	SetupAuth()
    51  
    52  	// Setup our router and handlers
    53  	SetupRoutes()
    54  
    55  	// Setup mail from config
    56  	SetupMail()
    57  
    58  }
    59  
    60  // SetupLog sets up logging
    61  func SetupLog() error {
    62  
    63  	// Set up a stderr logger with time prefix
    64  	logger, err := log.NewStdErr(log.PrefixDateTime)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	log.Add(logger)
    69  
    70  	// Set up a file logger pointing at the right location for this config.
    71  	fileLog, err := log.NewFile(config.Get("log"))
    72  	if err != nil {
    73  		return err
    74  	}
    75  	log.Add(fileLog)
    76  
    77  	return nil
    78  }
    79  
    80  // SetupMail sets us up to send mail via sendgrid (requires key).
    81  func SetupMail() {
    82  	mail.Production = config.Production()
    83  	mail.Service = sendgrid.New(config.Get("mail_from"), config.Get("mail_secret"))
    84  }