udemy-go-web-1/cmd/web/main.go
2024-06-29 21:30:06 +02:00

57 lines
1.0 KiB
Go

package main
import (
"fmt"
"go-udemy-web-1/pkg/config"
"go-udemy-web-1/pkg/handlers"
"go-udemy-web-1/pkg/render"
"log"
"net/http"
"time"
"github.com/alexedwards/scs/v2"
)
const portNumber = ":8080"
var (
app config.AppConfig
session *scs.SessionManager
)
// main is the main application function
func main() {
// 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
repo := handlers.NewRepo(&app)
handlers.NewHandlers(repo)
render.NewTemplates(&app)
fmt.Printf("Starting application on port %s\n", portNumber)
srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
err = srv.ListenAndServe()
log.Fatal(err)
}