Skip to content

Commit

Permalink
added command line options to the application
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Sep 25, 2023
1 parent 2e499d0 commit 6b187b8
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/gob"
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -18,6 +19,7 @@ import (
)

const portNumber = ":8080"
const versionNumber = "v1.0.169"

var app config.AppConfig
var session *scs.SessionManager
Expand Down Expand Up @@ -61,11 +63,34 @@ func run() (*driver.DB, error) {
gob.Register(models.Restriction{})
gob.Register(map[string]int{})

// read flags as arguments from the command line
inProduction := flag.Bool("production", true, "Application is in production mode")
useCache := flag.Bool("cache", true, "Use template cache")
dbHost := flag.String("dbhost", "localhost", "Database host")
dbName := flag.String("dbname", "", "Database name")
dbUser := flag.String("dbuser", "", "Database user")
dbPass := flag.String("dbpass", "", "Database password")
dbPort := flag.String("dbport", "5432", "Database port")
dbSSL := flag.String("dbssl", "disable", "Database ssl settings (disable, prefer, require)")
version := flag.Bool("version", false, "Prints the version number")

flag.Parse()

if *dbName == "" || *dbUser == "" {
fmt.Println("Required flags missing - no user credentials for db access?")
os.Exit(1)
}

if *version == true {
fmt.Println(versionNumber)
}

mailChan := make(chan models.MailData)
app.MailChan = mailChan

// don't forget to change to true in Production!
app.InProduction = false
app.InProduction = *inProduction
app.UseCache = *useCache

infoLog = log.New(os.Stdout, "[INFO]\t", log.Ldate|log.Ltime)
app.InfoLog = infoLog
Expand All @@ -83,7 +108,8 @@ func run() (*driver.DB, error) {

// connecting to database
log.Println("Connecting to database...")
db, err := driver.ConnectSQL("host=localhost port=5432 dbname=mygowebapp user=postgres password=")
connectionString := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s", *dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
db, err := driver.ConnectSQL(connectionString)
if err != nil {
log.Fatal("No connection to database! Terminating ...")
}
Expand All @@ -96,7 +122,6 @@ func run() (*driver.DB, error) {
}

app.TemplateCache = tc
app.UseCache = false

repo := handlers.NewRepo(&app, db)
handlers.NewHandlers(repo)
Expand Down

0 comments on commit 6b187b8

Please sign in to comment.