/
main.go
61 lines (50 loc) · 1.21 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
package main
import (
"database/sql"
"log"
"log/slog"
"net/http"
"time"
"github.com/acoshift/configfile"
"github.com/acoshift/pgsql/pgctx"
_ "github.com/lib/pq"
"github.com/moonrhythm/hime"
"github.com/moonrhythm/httpmux"
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/session"
"github.com/moonrhythm/session/store"
"github.com/acoshift/todo-hime/todoapp"
)
func main() {
configfile.LoadDotEnv()
config := configfile.NewEnvReader()
db, err := sql.Open("postgres", config.String("sql_db"))
if err != nil {
log.Fatal(err)
}
app := hime.New()
app.SetServer(parapet.NewBackend()) // optimize config to run behind reverse proxy
app.ETag = true
app.Server().UseFunc(session.Middleware(session.Config{
Rolling: true,
Proxy: true,
Secure: session.PreferSecure,
SameSite: http.SameSiteLaxMode,
Path: "/",
HTTPOnly: true,
MaxAge: 7 * 24 * time.Hour,
Store: (&store.SQL{
DB: db,
}).GeneratePostgreSQLStatement("sessions", true),
}))
app.Server().UseFunc(pgctx.Middleware(db))
mux := httpmux.New()
todoapp.Mount(mux, app)
app.Handler(mux)
app.Address(":8080")
slog.Info("starting server at :8080")
err = app.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}