2024-06-26 15:30:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-01 12:19:38 +00:00
|
|
|
"encoding/gob"
|
2024-07-29 12:20:51 +00:00
|
|
|
"flag"
|
2024-06-26 15:30:39 +00:00
|
|
|
"fmt"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/config"
|
2024-07-06 20:55:25 +00:00
|
|
|
"go-udemy-web-1/internal/driver"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/handlers"
|
2024-07-03 08:03:25 +00:00
|
|
|
"go-udemy-web-1/internal/helpers"
|
2024-07-01 12:19:38 +00:00
|
|
|
"go-udemy-web-1/internal/models"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/render"
|
2024-06-27 12:03:43 +00:00
|
|
|
"log"
|
2024-06-26 15:30:39 +00:00
|
|
|
"net/http"
|
2024-07-03 08:03:25 +00:00
|
|
|
"os"
|
2024-06-28 13:30:00 +00:00
|
|
|
"time"
|
|
|
|
|
2024-07-29 19:54:37 +00:00
|
|
|
"github.com/alexedwards/scs/redisstore"
|
2024-06-28 13:30:00 +00:00
|
|
|
"github.com/alexedwards/scs/v2"
|
2024-07-29 19:54:37 +00:00
|
|
|
"github.com/gomodule/redigo/redis"
|
2024-06-26 15:30:39 +00:00
|
|
|
)
|
|
|
|
|
2024-06-26 15:41:50 +00:00
|
|
|
const portNumber = ":8080"
|
|
|
|
|
2024-06-28 13:30:00 +00:00
|
|
|
var (
|
2024-07-03 08:03:25 +00:00
|
|
|
app config.AppConfig
|
|
|
|
session *scs.SessionManager
|
|
|
|
infoLog *log.Logger
|
|
|
|
errorLog *log.Logger
|
2024-06-28 13:30:00 +00:00
|
|
|
)
|
|
|
|
|
2024-06-26 15:41:50 +00:00
|
|
|
// main is the main application function
|
2024-06-26 15:30:39 +00:00
|
|
|
func main() {
|
2024-07-06 20:55:25 +00:00
|
|
|
db, err := run()
|
2024-07-01 20:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-07-06 20:55:25 +00:00
|
|
|
defer db.SQL.Close()
|
2024-07-16 11:55:35 +00:00
|
|
|
defer close(app.MailChan)
|
2024-07-01 20:34:16 +00:00
|
|
|
|
2024-07-16 11:55:35 +00:00
|
|
|
fmt.Println("Starting mail listener...")
|
2024-07-01 20:34:16 +00:00
|
|
|
fmt.Printf("Starting application on port %s\n", portNumber)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: portNumber,
|
|
|
|
Handler: routes(&app),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = srv.ListenAndServe()
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-06 20:55:25 +00:00
|
|
|
func run() (*driver.DB, error) {
|
2024-07-01 20:34:16 +00:00
|
|
|
// what am I going to put in the session
|
|
|
|
gob.Register(models.Reservation{})
|
2024-07-07 20:37:48 +00:00
|
|
|
gob.Register(models.User{})
|
|
|
|
gob.Register(models.Room{})
|
|
|
|
gob.Register(models.Restriction{})
|
2024-07-27 20:39:23 +00:00
|
|
|
gob.Register(map[string]int{})
|
2024-07-01 12:19:38 +00:00
|
|
|
|
2024-07-29 12:20:51 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-07-16 11:55:35 +00:00
|
|
|
mailChan := make(chan models.MailData)
|
|
|
|
app.MailChan = mailChan
|
|
|
|
|
|
|
|
listenForMail()
|
|
|
|
|
2024-06-28 13:30:00 +00:00
|
|
|
// change this to true when in production
|
2024-07-29 12:20:51 +00:00
|
|
|
app.InProduction = *inProduction
|
2024-06-28 13:30:00 +00:00
|
|
|
|
2024-07-29 19:54:37 +00:00
|
|
|
// Establish connection pool to Redis.
|
|
|
|
pool := &redis.Pool{
|
|
|
|
MaxIdle: 10,
|
|
|
|
Dial: func() (redis.Conn, error) {
|
|
|
|
return redis.Dial("tcp", "localhost:6379")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-28 13:30:00 +00:00
|
|
|
session = scs.New()
|
2024-07-29 19:54:37 +00:00
|
|
|
session.Store = redisstore.New(pool)
|
2024-06-28 13:30:00 +00:00
|
|
|
session.Lifetime = 24 * time.Hour
|
|
|
|
session.Cookie.Persist = true
|
|
|
|
session.Cookie.SameSite = http.SameSiteLaxMode
|
|
|
|
session.Cookie.Secure = app.InProduction
|
|
|
|
|
|
|
|
app.Session = session
|
2024-06-27 12:03:43 +00:00
|
|
|
|
2024-07-06 20:55:25 +00:00
|
|
|
// connect to database
|
|
|
|
log.Println("Connecting to database...")
|
2024-07-29 12:20:51 +00:00
|
|
|
dsn := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s",
|
|
|
|
*dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
|
2024-07-06 20:55:25 +00:00
|
|
|
db, err := driver.ConnectSQL(dsn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Cannot connect to database! Dying...")
|
|
|
|
}
|
|
|
|
log.Println("Connected to database")
|
|
|
|
|
2024-06-27 12:03:43 +00:00
|
|
|
tc, err := render.CreateTemplateCache()
|
|
|
|
if err != nil {
|
2024-06-29 19:30:06 +00:00
|
|
|
log.Fatalf("cannot create template cache: %s", err)
|
2024-07-06 20:55:25 +00:00
|
|
|
return nil, err
|
2024-06-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
app.TemplateCahce = tc
|
2024-07-29 12:20:51 +00:00
|
|
|
app.UseCache = *useCache
|
2024-06-27 12:03:43 +00:00
|
|
|
|
2024-07-03 08:03:25 +00:00
|
|
|
infoLog = log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
|
|
|
app.InfoLog = infoLog
|
|
|
|
|
|
|
|
errorLog = log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
|
|
|
app.ErrorLog = errorLog
|
|
|
|
|
2024-07-06 20:55:25 +00:00
|
|
|
repo := handlers.NewRepo(&app, db)
|
2024-06-27 12:03:43 +00:00
|
|
|
handlers.NewHandlers(repo)
|
2024-07-03 08:03:25 +00:00
|
|
|
helpers.NewHelpers(&app)
|
2024-06-27 12:03:43 +00:00
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.NewRenderer(&app)
|
2024-07-06 20:55:25 +00:00
|
|
|
return db, nil
|
2024-06-26 15:30:39 +00:00
|
|
|
}
|