-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (65 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/gob"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/alexedwards/scs/v2"
"github.com/iamsabbiralam/bookings/internal/config"
"github.com/iamsabbiralam/bookings/internal/handlers"
"github.com/iamsabbiralam/bookings/internal/helpers"
"github.com/iamsabbiralam/bookings/internal/models"
"github.com/iamsabbiralam/bookings/internal/render"
)
const portNumber = ":8080"
var app config.AppConfig
var session *scs.SessionManager
var infoLog *log.Logger
var errorLog *log.Logger
// main is the main application function
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
fmt.Println(fmt.Sprintf("starting application on port %s", portNumber))
srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
err = srv.ListenAndServe()
if err != nil {
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 mode
app.InProduction = 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
// set up the session
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")
return err
}
app.TemplateCache = tc
app.UseCache = false
repo := handlers.NewRepo(&app)
handlers.NewHandler(repo)
render.NewTemplates(&app)
helpers.NewHelpers(&app)
return nil
}