udemy-go-web-1/cmd/web/main.go

81 lines
1.5 KiB
Go

package main
import (
"encoding/gob"
"fmt"
"go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/handlers"
"go-udemy-web-1/internal/helpers"
"go-udemy-web-1/internal/models"
"go-udemy-web-1/internal/render"
"log"
"net/http"
"os"
"time"
"github.com/alexedwards/scs/v2"
)
const portNumber = ":8080"
var (
app config.AppConfig
session *scs.SessionManager
infoLog *log.Logger
errorLog *log.Logger
)
// main is the main application function
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Starting application on port %s\n", portNumber)
srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
err = srv.ListenAndServe()
log.Fatal(err)
}
func run() error {
// what am I going to put in the session
gob.Register(models.Reservation{})
// change this to true when in production
app.InProduction = false
session = scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = app.InProduction
app.Session = session
tc, err := render.CreateTemplateCache()
if err != nil {
log.Fatalf("cannot create template cache: %s", err)
}
app.TemplateCahce = tc
app.UseCache = false
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
repo := handlers.NewRepo(&app)
handlers.NewHandlers(repo)
helpers.NewHelpers(&app)
render.NewTemplates(&app)
return nil
}