Use cmd flags
This commit is contained in:
		@ -2,6 +2,7 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/gob"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"go-udemy-web-1/internal/config"
 | 
			
		||||
	"go-udemy-web-1/internal/driver"
 | 
			
		||||
@ -55,13 +56,29 @@ func run() (*driver.DB, error) {
 | 
			
		||||
	gob.Register(models.Restriction{})
 | 
			
		||||
	gob.Register(map[string]int{})
 | 
			
		||||
 | 
			
		||||
	// read flags
 | 
			
		||||
	inProduction := flag.Bool("production", true, "Application is in production")
 | 
			
		||||
	useCache := flag.Bool("cache", true, "Use template cache")
 | 
			
		||||
	dbHost := flag.String("dbhost", "localhost", "Database host")
 | 
			
		||||
	dbName := flag.String("dbname", "bookings", "Database name")
 | 
			
		||||
	dbUser := flag.String("dbuser", os.Getenv("PGUSER"), "Database user")
 | 
			
		||||
	dbPass := flag.String("dbpass", os.Getenv("PGPWD"), "Database password")
 | 
			
		||||
	dbPort := flag.String("dbport", "5432", "Database port")
 | 
			
		||||
	dbSSL := flag.String("dbssl", "disable", "Database ssl settings (disable, prefer, require)")
 | 
			
		||||
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
	if *dbUser == "" {
 | 
			
		||||
		fmt.Println("Missing required flags")
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mailChan := make(chan models.MailData)
 | 
			
		||||
	app.MailChan = mailChan
 | 
			
		||||
 | 
			
		||||
	listenForMail()
 | 
			
		||||
 | 
			
		||||
	// change this to true when in production
 | 
			
		||||
	app.InProduction = false
 | 
			
		||||
	app.InProduction = *inProduction
 | 
			
		||||
 | 
			
		||||
	session = scs.New()
 | 
			
		||||
	session.Lifetime = 24 * time.Hour
 | 
			
		||||
@ -73,7 +90,8 @@ func run() (*driver.DB, error) {
 | 
			
		||||
 | 
			
		||||
	// connect to database
 | 
			
		||||
	log.Println("Connecting to database...")
 | 
			
		||||
	dsn := fmt.Sprintf("host=localhost port=5432 dbname=bookings user=%s password=%s", os.Getenv("PGUSER"), os.Getenv("PGPWD"))
 | 
			
		||||
	dsn := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s",
 | 
			
		||||
		*dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
 | 
			
		||||
	db, err := driver.ConnectSQL(dsn)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal("Cannot connect to database! Dying...")
 | 
			
		||||
@ -86,7 +104,7 @@ func run() (*driver.DB, error) {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	app.TemplateCahce = tc
 | 
			
		||||
	app.UseCache = false
 | 
			
		||||
	app.UseCache = *useCache
 | 
			
		||||
 | 
			
		||||
	infoLog = log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
 | 
			
		||||
	app.InfoLog = infoLog
 | 
			
		||||
 | 
			
		||||
@ -38,7 +38,9 @@ func routes(app *config.AppConfig) http.Handler {
 | 
			
		||||
	mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
 | 
			
		||||
 | 
			
		||||
	mux.Route("/admin", func(mux chi.Router) {
 | 
			
		||||
		// mux.Use(Auth)
 | 
			
		||||
		if app.InProduction {
 | 
			
		||||
			mux.Use(Auth)
 | 
			
		||||
		}
 | 
			
		||||
		mux.Get("/dashboard", handlers.Repo.AdminDashboard)
 | 
			
		||||
 | 
			
		||||
		mux.Get("/reservations-new", handlers.Repo.AdminNewReservations)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user