2024-06-26 15:30:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-01 12:19:38 +00:00
|
|
|
"encoding/gob"
|
2024-06-26 15:30:39 +00:00
|
|
|
"fmt"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/config"
|
|
|
|
"go-udemy-web-1/internal/handlers"
|
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-06-28 13:30:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/alexedwards/scs/v2"
|
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 (
|
|
|
|
app config.AppConfig
|
|
|
|
session *scs.SessionManager
|
|
|
|
)
|
|
|
|
|
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-01 20:34:16 +00:00
|
|
|
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{})
|
2024-07-01 12:19:38 +00:00
|
|
|
|
2024-06-28 13:30:00 +00:00
|
|
|
// 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
|
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-06-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
app.TemplateCahce = tc
|
|
|
|
app.UseCache = false
|
|
|
|
|
|
|
|
repo := handlers.NewRepo(&app)
|
|
|
|
handlers.NewHandlers(repo)
|
|
|
|
|
|
|
|
render.NewTemplates(&app)
|
2024-07-01 20:34:16 +00:00
|
|
|
return nil
|
2024-06-26 15:30:39 +00:00
|
|
|
}
|