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

57 lines
1.0 KiB
Go
Raw Normal View History

2024-06-26 15:30:39 +00:00
package main
import (
"fmt"
"go-udemy-web-1/pkg/config"
2024-06-26 20:06:32 +00:00
"go-udemy-web-1/pkg/handlers"
"go-udemy-web-1/pkg/render"
"log"
2024-06-26 15:30:39 +00:00
"net/http"
"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"
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() {
// 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.Fatal("cannot create template cache")
}
app.TemplateCahce = tc
app.UseCache = false
repo := handlers.NewRepo(&app)
handlers.NewHandlers(repo)
render.NewTemplates(&app)
2024-06-26 15:41:50 +00:00
fmt.Printf("Starting application on port %s\n", portNumber)
2024-06-28 10:59:47 +00:00
srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
err = srv.ListenAndServe()
log.Fatal(err)
2024-06-26 15:30:39 +00:00
}