github.com/jamiefdhurst/journal@v0.9.2/internal/app/app.go (about)

     1  package app
     2  
     3  import (
     4  	"database/sql"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/jamiefdhurst/journal/pkg/database/rows"
     9  )
    10  
    11  // Database Define same interface as database
    12  type Database interface {
    13  	Close()
    14  	Connect(dbFile string) error
    15  	Exec(sql string, args ...interface{}) (sql.Result, error)
    16  	Query(sql string, args ...interface{}) (rows.Rows, error)
    17  }
    18  
    19  // GiphyAdapter Interface for API
    20  type GiphyAdapter interface {
    21  	SearchForID(s string) (string, error)
    22  }
    23  
    24  // Container Define the main container for the application
    25  type Container struct {
    26  	Configuration Configuration
    27  	Db            Database
    28  	Giphy         GiphyAdapter
    29  	Version       string
    30  }
    31  
    32  // Configuration can be modified through environment variables
    33  type Configuration struct {
    34  	ArticlesPerPage     int
    35  	DatabasePath        string
    36  	Description         string
    37  	EnableCreate        bool
    38  	EnableEdit          bool
    39  	GoogleAnalyticsCode string
    40  	Port                string
    41  	Title               string
    42  }
    43  
    44  // DefaultConfiguration returns the default settings for the app
    45  func DefaultConfiguration() Configuration {
    46  	return Configuration{
    47  		ArticlesPerPage:     20,
    48  		DatabasePath:        os.Getenv("GOPATH") + "/data/journal.db",
    49  		Description:         "A private journal containing Jamie's innermost thoughts",
    50  		EnableCreate:        true,
    51  		EnableEdit:          true,
    52  		GoogleAnalyticsCode: "",
    53  		Port:                "3000",
    54  		Title:               "Jamie's Journal",
    55  	}
    56  }
    57  
    58  // ApplyEnvConfiguration applies the env variables on top of existing config
    59  func ApplyEnvConfiguration(config *Configuration) {
    60  	articles, _ := strconv.Atoi(os.Getenv("J_ARTICLES_PER_PAGE"))
    61  	if articles > 0 {
    62  		config.ArticlesPerPage = articles
    63  	}
    64  	database := os.Getenv("J_DB_PATH")
    65  	if database != "" {
    66  		config.DatabasePath = database
    67  	}
    68  	description := os.Getenv("J_DESCRIPTION")
    69  	if description != "" {
    70  		config.Description = description
    71  	}
    72  	enableCreate := os.Getenv("J_CREATE")
    73  	if enableCreate == "0" {
    74  		config.EnableCreate = false
    75  	}
    76  	enableEdit := os.Getenv("J_EDIT")
    77  	if enableEdit == "0" {
    78  		config.EnableEdit = false
    79  	}
    80  	config.GoogleAnalyticsCode = os.Getenv("J_GA_CODE")
    81  	port := os.Getenv("J_PORT")
    82  	if port != "" {
    83  		config.Port = port
    84  	}
    85  	title := os.Getenv("J_TITLE")
    86  	if title != "" {
    87  		config.Title = title
    88  	}
    89  }